admin管理员组

文章数量:1419223

I have a password field in my registration form. The following is the code for javascript validation.

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?

I have a password field in my registration form. The following is the code for javascript validation.

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?

Share Improve this question asked Aug 4, 2012 at 9:28 designersvsoftdesignersvsoft 1,85910 gold badges39 silver badges66 bronze badges 2
  • Instead of looking up the element and its value in each if statement, just do it once at the beginning and store it in a variable. – Felix Kling Commented Aug 4, 2012 at 9:32
  • possible duplicate of How to tell if a string contains a certain character in javascript? – Felix Kling Commented Aug 4, 2012 at 9:33
Add a ment  | 

2 Answers 2

Reset to default 4
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:

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

本文标签: javascript validation of single quotesStack Overflow