表单电子邮件验证工作不正常

Form email validation not working properly

本文关键字:不正常 工作 验证 电子邮件 表单      更新时间:2023-09-26

我有一个表单正在尝试验证,但电子邮件、选择国家/地区字段似乎没有得到验证。有人能告诉我出了什么问题吗?

这是我的javascript:

<script type="text/javascript">
function validateEmail()
{
   var emailID = document.form1.EMail.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email ID")
       document.form1.EMail.focus() ;
       return false;
   }
   return( true );
}
function validate()
{
//username password First_n Last_n Company Mobile Email Residence

   if( document.form1.First_n.value == "" )
   {
     alert( "Please enter your first name!" );
     document.form1.First_n.focus() ;
     return false;
   }
   if( document.form1.Last_n.value == "" )
   {
     alert( "Please enter your last name!" );
     document.form1.Last_n.focus() ;
     return false;
   }
   if( document.form1.username.value == "" )
   {
     alert( "Please enter your username!" );
     document.form1.username.focus() ;
     return false;
   }
   if( document.form1.password.value == "" )
   {
     alert( "Please enter your password!" );
     document.form1.password.focus() ;
     return false;
   }
   if( document.form1.Company.value == "" )
   {
     alert( "Please provide us your company name!" );
     document.form1.Company.focus() ;
     return false;
   }
   if( document.form1.Mobile.value == "" )
   {
     alert( "Please provide us your mobile number!" );
     document.form1.Mobile.focus() ;
     return false;
   }
   if( document.form1.Email.value == "" )
   {
     alert( "Please provide us your Email!" );
     document.form1.Email.focus() ;
     return false;
   }
   if( document.form1.Email.value != "" )
   {
     // Put extra check for data format
     var ret = validateEmail();
     if( ret == false )
     {
          return false;
     }else 
          return true;
   }
   if( document.form1.Zip.value == "" ||
           isNaN( document.form1.Zip.value ) ||
           document.form1.Zip.value.length != 5 )
   {
     alert( "Please provide a zip in the format #####." );
     document.form1.Zip.focus() ;
     return false;
   }
   if( document.form1.Residence.value == "-1" )
   {
     alert( "Please provide your country!" );
     return false;
   }
   return( true );
}
</script>

我的表单html:

<form name="form1" method="post" action="add_new.php" onsubmit="return(validate());">
<div style="clear:both;padding:0px 10px 0 10px;margin-bottom:20px;">
<h5>Interested in</h5>
<input id="toggle-on" class="toggle toggle-left" name="toggle" value="false" type="radio" checked>
<label for="toggle-on" class="btn">Hosting</label>
<input id="toggle-off" class="toggle toggle-right" name="toggle" value="true" type="radio"
><label for="toggle-off" class="btn">Email accounts</label>
</div> 
<div style="clear-both;">
<input name="First_n" type="text" placeholder="First Name" class="login-text-lbl-pink-no-width" id="First_n">     
<input name="Last_n" type="text" placeholder="Last Name" class="login-text-lbl-pink-no-width" id="Last_n">              
</div>
<input name="username" type="text" placeholder="Username" class="login-text-lbl-pink-no-width" id="username"><br/>
<input name="password" type="password" placeholder="Password" class="login-text-lbl-pink-no-width" id="password"><br/>
<input name="Company" type="text" placeholder="Company" class="login-text-lbl-pink-odd" id="Company">              <br/>
<input name="Mobile" type="text" placeholder="Mobile Phone" id="login-text-lbl" class="pink-transparent-item" id="Mobile">              <br/>
<input name="Email" type="text"  placeholder="Email" class="login-text-lbl-pink-odd" class="pink-transparent-item" id="Email">              <br/>
<select name="Residence" id="Residence" id="login-text-lbl" style="
background-color: rgba(240, 96, 96, 0.62);
border: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
height: 30px;
margin: 5px;
font-style: italic;
width: 90%;
padding: 5px;
color: #34584b;
float: none;"
>
<option value="-1" selected>[choose country]</option>
<option value="US">America</option>
<option value="DE">Germany</option>
<option value="IT">Italy</option>
<option value="HK">Hong Kong</option><br/>
<input name="domain" class="pink-transparent-item" type="text"  placeholder="Existing domain" id="login-text-lbl" id="domain">              <br/>
<input type="submit" name="Submit" value="Submit" style='font-family: "Neo Sans Light", Verdana, Tahoma;' class="login-button-pink">
</form>

您的表单的电子邮件id为:"email",而您的验证代码为email(var emailID=document.form1.email.value;)?使用正确的控制id。

当然,您可以使用regex进行验证,这甚至更好。

这里有一些错误,但让我们尝试具体回答您的问题-

电子邮件在chrome和firefox中对我来说很好,但不能代表safari、IE、seamonkey等。

你的validateEmail()方法有一个名为EMail的输入-你不会得到它。JS是区分大小写的。

即使在您进行了验证之后,您也永远不会访问任何超过电子邮件的内容,因为在恢复validateEmail()的值之后,您拥有return true。此时您已离开validate()

至于其他事情,您应该真正通过JSLint运行代码来检查它是否有错误。我看到一对未闭合的牙套。我之所以选择JSLint,是因为你似乎是JS的相对初学者,所以你可能需要Crockford为代码带来的更为原教旨主义的方法,至少在一段时间内,直到你擅长它(无意冒犯;我们都从一开始就开始)。

只使用regex进行电子邮件验证可能会容易得多。看看这个StackExchange的答案,可以找到一个好的例子。

答案的代码片段:

function validateEmail(email) { 
    var re = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('
".+'"))@(('[[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA
-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

做几件事:

1) 在"validate()"answers"validateEmail()"函数中放入alert语句,并确保收到所有期望的值。

2) 如果您使用IE,请尝试在Chrome或FireFox中加载应用程序,并通过内置调试器在JS函数中设置断点。

您实际上可以使用Fuzzley答案中的正则表达式方法,对于国家字段部分,请尝试使用此代码,

var country= document.form1.Residence.value ;
if(country != "America"|| country != "Germany"||country != "Italy"||country != "Hong Kong") {
            alert("Please provide your country!");
            return false;
        }