Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript验证检查的密码部分工作不正常_Javascript_Validation - Fatal编程技术网

Javascript验证检查的密码部分工作不正常

Javascript验证检查的密码部分工作不正常,javascript,validation,Javascript,Validation,我正在编译一个可重用的完整javascript表单验证。直到我的密码只检查了一个条件,而不是所有条件,一切都很顺利 这是我的密码: function validate() { //var emailID = document.myForm.EMail.value; //atpos = emailID.indexOf("@"); //dotpos = emailID.lastIndexOf("."); var alphanumb = /^[0-9a-zA-Z

我正在编译一个可重用的完整javascript表单验证。直到我的密码只检查了一个条件,而不是所有条件,一切都很顺利

这是我的密码:

function validate() {

    //var emailID = document.myForm.EMail.value;
     //atpos = emailID.indexOf("@");
     //dotpos = emailID.lastIndexOf(".");
     var alphanumb = /^[0-9a-zA-Z]+$/;
     var alphaExp = /^[a-zA-Z]+$/;
     var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

     if( document.myForm.Name.value == "" || document.myForm.Name.value.trim()==null || document.myForm.Name.value.trim()=="" || document.myForm.Name.value===" ")
     {
        //alert( "Please provide your name!" );
        document.getElementById('p1').innerText = "* All fields are mandatory *";
        document.myForm.Name.focus() ;
        return false;
     }else if(!(document.myForm.Name.value.length >=3 && document.myForm.Name.value.length <= 20)) {
        document.getElementById('p1').innerText = "* Please enter 3 -20 letters *";
        document.myForm.Name.focus() ;
        return false;
     }else if(!(document.myForm.Name.value.match(alphaExp))) {
       document.getElementById('p1').innerText = "* Please only alphabets are allowed *";
        document.myForm.Name.focus() ;
        return false;
     }
     <!----------------First Name Ends Here----------------->



     if( document.myForm.lastname.value == "" || document.myForm.lastname.value.trim()==null || document.myForm.lastname.value.trim()=="" || document.myForm.lastname.value===" ")
     {
        //alert( "Please provide your name!" );
        document.getElementById('p2').innerText = "* All fields are mandatory *";
        document.myForm.lastname.focus() ;
        return false;
     }else if(!(document.myForm.lastname.value.length >=3 &&   document.getElementById('p2').innerText = "* Please only alphabets are allowed *";
        document.myForm.lastname.focus() ;
        return false;
     }document.myForm.lastname.value.length <= 20)) {
        document.getElementById('p2').innerText = "* Please enter 3 -20 letters *";
        document.myForm.lastname.focus() ;
        return false;
     }else if(!(document.myForm.lastname.value.match(alphaExp))) {

     <!----------------Last Name Ends Here----------------->



     if( document.myForm.EMail.value == "" )
     {
        //alert( "Please provide your Email!" );
        document.getElementById('p3').innerText = "* Please provide your Email! *";
        document.myForm.EMail.focus() ;
        return false;
     }else if(!(document.myForm.EMail.value.match(emailExp)))
     {
        //alert("Please enter correct email ID")
        document.getElementById('p3').innerText = "* Please enter correct email ID *";
        document.myForm.EMail.focus() ;
        return false;
     }
     <!----------------Email Ends Here----------------->



     if( document.myForm.confirm_email.value !== document.myForm.EMail.value )
     {
        //alert( "Please provide your Email!" );
        document.getElementById('p4').innerText = "* Emails does not match, pls check well *";
        document.myForm.confirm_email.focus() ;
        return false;
     }
     <!----------------Email Confirm Ends Here----------------->



     if( document.myForm.Password.value == "" || document.myForm.Password.value.trim()==null || document.myForm.Password.value.trim()=="" || document.myForm.Password.value===" ")
     {
        document.getElementById('p5').innerText = "* Password cannot be empty or just empty spaces *";
        document.myForm.Password.focus() ;
        return false;
    }else if(!(document.getElementById('Password').value.match(alphanumb)))    {
     document.getElementById('p5').innerText = "* Please your entry must include numbers and alphabets *";
        document.myForm.Password.focus() ;
        return false;
     }else if(document.myForm.Password.value.length < 8 && document.myForm.Password.value.length > 30)
     {
        document.getElementById('p5').innerText = "* Password can only be from 8 - 30 letters *";
        document.myForm.Password.focus() ;
        return false;
     }
     <!----------------Password Ends Here----------------->



     if( document.myForm.confirm_password.value !== document.myForm.Password.value )
     {
        //alert( "Please provide your Email!" );
        document.getElementById('p6').innerText = "* Passwords does not match, pls check well *";
        document.myForm.confirm_password.focus() ;
        return false;
     }
     <!----------------Confirm Password Ends Here----------------->


     if( document.myForm.Zip.value == "" ||
     isNaN( document.myForm.Zip.value ) ||
     document.myForm.Zip.value.length != 5 )
     {
        alert( "Please provide a zip in the format #####." );
        document.myForm.Zip.focus() ;
        return false;
     }

     if( document.myForm.Country.value == "-1" )
     {
        alert( "Please provide your country!" );
        return false;
     }

     return( true );
  }
函数验证(){
//var emailID=document.myForm.EMail.value;
//atpos=emailID.indexOf(“@”);
//dotpos=emailID.lastIndexOf(“.”);
var alphanumb=/^[0-9a-zA-Z]+$/;
var alphaExp=/^[a-zA-Z]+$/;
var emailExp=/^[\w\-\.\+]+\@[a-zA-Z0-9\.-]+\.[a-zA-Z0-9]{2,4}$/;
如果(document.myForm.Name.value==“document.myForm.Name.value.trim()==null |•document.myForm.Name.value.trim()=”=“document.myForm.Name.value==”)
{
//警告(“请提供您的姓名!”);
document.getElementById('p1').innerText=“*所有字段都是必需的*”;
document.myForm.Name.focus();
返回false;
}如果(!(document.myForm.Name.value.length>=3&&document.myForm.Name.value.length
如果(document.myForm.EMail.value==“”)
{
//提醒(“请提供您的电子邮件!”);
document.getElementById('p3').innerText=“*请提供您的电子邮件!*”;
document.myForm.EMail.focus();
返回false;
}else if(!(document.myForm.EMail.value.match(emailExp)))
{
//警报(“请输入正确的电子邮件ID”)
document.getElementById('p3')。innerText=“*请输入正确的电子邮件ID*”;
document.myForm.EMail.focus();
返回false;
}
if(document.myForm.confirm_email.value!==document.myForm.email.value)
{
//提醒(“请提供您的电子邮件!”);
document.getElementById('p4').innerText=“*电子邮件不匹配,请仔细检查*”;
document.myForm.confirm_email.focus();
返回false;
}
if(document.myForm.Password.value==“document.myForm.Password.value.trim()==null |•document.myForm.Password.value.trim()==“document.myForm.Password.value==”)
{
document.getElementById('p5').innerText=“*密码不能为空或仅为空空格*”;
document.myForm.Password.focus();
返回false;
}else if(!(document.getElementById('Password').value.match(alphanumb))){
document.getElementById('p5').innerText=“*请输入必须包含数字和字母*”;
document.myForm.Password.focus();
返回false;
}else if(document.myForm.Password.value.length<8&&document.myForm.Password.value.length>30)
{
document.getElementById('p5').innerText=“*密码只能是8-30个字母*”;
document.myForm.Password.focus();
返回false;
}
if(document.myForm.confirm_password.value!==document.myForm.password.value)
{
//提醒(“请提供您的电子邮件!”);
document.getElementById('p6').innerText=“*密码不匹配,请仔细检查*”;
document.myForm.confirm_password.focus();
返回false;
}
如果(document.myForm.Zip.value==“”||
isNaN(document.myForm.Zip.value)||
document.myForm.Zip.value.length!=5)
{
警告(“请提供格式为#######的邮政编码”);
document.myForm.Zip.focus();
返回false;
}
如果(document.myForm.Country.value==“-1”)
{
警告(“请提供您的国家!”);
返回false;
}
返回(真);
}

名字
姓
电子邮件
确认电子邮件
输入密码
确认密码
邮政编码
国家
[选择你的]
美国
英国
印度

您正在使用
else
语句,并在其中使用
return false
,这两种语句都会阻止进行额外的检查

此外,您正在重写错误消息输出元素的
内部文本
,因此需要将其与
+=
相加,而不是
=
,以允许它附加额外消息

考虑使用一个变量在验证脚本结束时返回,而不是使用多个
returnfalse
语句

 var return_flag = true;
 document.getElementById('p5').innerText = '';

 if( document.myForm.Password.value == "" || document.myForm.Password.value.trim()==null || document.myForm.Password.value.trim()=="" || document.myForm.Password.value===" ")
 {
    document.getElementById('p5').innerText += "* Password cannot be empty or just empty spaces *";
    document.myForm.Password.focus() ;
    return_flag = false;
}
if(!(document.getElementById('Password').value.match(alphanumb)))    {
 document.getElementById('p5').innerText += "* Please your entry must include numbers and alphabets *";
    document.myForm.Password.focus() ;
    return_flag = false;
 }
if(document.myForm.Password.value.length < 8 && document.myForm.Password.value.length > 30)
 {
    document.getElementById('p5').innerText += "* Password can only be from 8 - 30 letters *";
    document.myForm.Password.focus() ;
    return_flag = false;
 }

 ...

return return_flag;
var return\u flag=true;
document.getElementById('p5')。innerText='';
if(document.myForm.Password.value==“document.myForm.Password.value.trim()==null |•document.myForm.Password.value.trim()==“document.myForm.Password.value==”)
{
document.getElementById('p5')。innerText+=“*密码不能为空或仅为空空格*”;
document.myForm.Password.focus();
return_flag=false;
}
if(!(document.getElementById('Password').value.match(alphanumb))){
document.getElementById('p5')。innerText+=“*请输入必须包含数字和字母*”;
document.myForm.Password.focus();
return_flag=false;
}
if(document.myForm.Password.value.length<8&&document.myForm.Password.value.length>30)
{
document.getElementById('p5')。innerText+=“*密码只能是8-30个字母*”;
document.myForm.Password.focus();
return_flag=false;
}
...
返回标志;

我不认为在
内使用
返回false
否则
条件在所有肯定条件下都是一个问题scenario@ManivannanSadasivam“return语句结束函数执行并指定要返回给函数调用方的值。”一旦被告知retur,它将立即停止对其余代码的求值