单引号的javascript验证

单引号的javascript验证,javascript,Javascript,我的注册表中有一个密码字段。下面是javascript验证的代码 if(document.getElementById('txtPassword').value == '' || document.getElementById('txtPassword').value == 'Password' ){ alert("Please enter Password"); document.getElementById('txtPassword').foc

我的注册表中有一个密码字段。下面是javascript验证的代码

if(document.getElementById('txtPassword').value == '' || document.getElementById('txtPassword').value == 'Password' ){
            alert("Please enter Password");
            document.getElementById('txtPassword').focus();
            return false;
        }


      else if (document.getElementById('txtPassword').value.length < 8)
       {
               alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
               return false;
       }


      else if ( (! document.getElementById('txtPassword').value.match(/[a-z]/) ) || (! document.getElementById('txtPassword').value.match(/[A-Z]/) ) ) {
               alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
               return false;
       }


      else if (!document.getElementById('txtPassword').value.match(/\d+/)) {
               alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
               return false;
       }

        else if (document.getElementById('txtPassword').length < 8)
       {
               alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
               return false;
       }
if(document.getElementById('txtPassword')。value=''|| document.getElementById('txtPassword')。value='Password'){
警报(“请输入密码”);
document.getElementById('txtPassword').focus();
返回false;
}
else if(document.getElementById('txtPassword').value.length<8)
{
警报('请确保您的密码至少包含八个字符,包括大小写和至少一个数字');
返回false;
}
如果(!document.getElementById('txtPassword').value.match(/[a-z]/)| |(!document.getElementById('txtPassword').value.match(/[a-z]/)){
警报('请确保您的密码至少包含八个字符,包括大小写和至少一个数字');
返回false;
}
如果(!document.getElementById('txtPassword').value.match(/\d+/),则为else{
警报('请确保您的密码至少包含八个字符,包括大小写和至少一个数字');
返回false;
}
else if(document.getElementById('txtPassword')。长度<8)
{
警报('请确保您的密码至少包含八个字符,包括大小写和至少一个数字');
返回false;
}
我需要检查单引号。如果用户在密码字段中输入单引号并点击输入,则应弹出错误“避免在此字段中使用单引号”

怎么做

else if (document.getElementById('txtPassword').value.indexOf("'") != -1) {
    alert("Avoid single quotes in this field");
    return false;
}
下面是一个简单的测试用例。如果您创建一个html页面,将其放在head标记中,然后在浏览器中打开,您将看到它可以工作:

var value1 = "abc'def";
var value2 = "abcdef";
if(value1.indexOf("'") != -1)
    alert(value1 + " contains a '");
else
    alert(value1 + " does not contain a '");
if(value2.indexOf("'") != -1)
    alert(value2 + " contains a '");
else
    alert(value2 + " does not contain a '");
下面是一个简单的测试用例。如果您创建一个html页面,将其放在head标记中,然后在浏览器中打开,您将看到它可以工作:

var value1 = "abc'def";
var value2 = "abcdef";
if(value1.indexOf("'") != -1)
    alert(value1 + " contains a '");
else
    alert(value1 + " does not contain a '");
if(value2.indexOf("'") != -1)
    alert(value2 + " contains a '");
else
    alert(value2 + " does not contain a '");

使用此正则表达式进行密码检查

^(?=.*\d)(?=.*[a-zA-Z]).{8,}
目前,您正在进行大量验证检查,以避免仅使用上述正则表达式

if (! /^(?=.*\d)(?=.*[a-zA-Z]).{8,}/.test(txtPass)) {
    flag = false;
}
else if(txtPass.indexOf("'") != -1) {
    flag = false;
}

if (!flag)
    alert("Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number");

请参阅使用此正则表达式进行密码检查

^(?=.*\d)(?=.*[a-zA-Z]).{8,}
目前,您正在进行大量验证检查,以避免仅使用上述正则表达式

if (! /^(?=.*\d)(?=.*[a-zA-Z]).{8,}/.test(txtPass)) {
    flag = false;
}
else if(txtPass.indexOf("'") != -1) {
    flag = false;
}

if (!flag)
    alert("Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number");

请参考

而不是在每个
if
语句中查找元素及其值,只需在开始时执行一次并将其存储在变量中。可能的重复项,而不是在每个
if
语句中查找元素及其值,只需在开始时执行一次,并将其存储在一个变量中