简体   繁体   English

单引号的javascript验证

[英]javascript validation of single quotes

I have a password field in my registration form. 我的注册表中有一个密码字段。 The following is the code for javascript validation. 以下是用于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;
       }

I need to check for single quotes. 我需要检查单引号。 If the user enters single quotes in the password field and hit enter the error "Avoid single quotes in this filed" should popup. 如果用户在密码字段中输入单引号并点击输入,则会弹出错误“在此字段中避免单引号”。

How to do that? 怎么做?

else if (document.getElementById('txtPassword').value.indexOf("'") != -1) {
    alert("Avoid single quotes in this field");
    return false;
}

here is a simple test case. 这是一个简单的测试用例。 If you make an html page, put this in the head tag, open it in a browser, you'll see it works: 如果创建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 '");

Use this regex for password check 使用此正则表达式进行密码检查

^(?=.*\d)(?=.*[a-zA-Z]).{8,}

Currently you are doing lot of validation checks, to avoid those just use above regex. 当前,您正在做很多验证检查,以避免仅在正则表达式上使用那些检查。

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");

Refer LIVE DEMO 请参考现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM