简体   繁体   中英

How do I validate a password using regular expressions?

Can someone help with the following JavaScript. I am new to coding, and am currently practising password validation using regular expressions. The following validations just aren't working and I can't see why, as I have previously practised pw validation without using re and it's worked fine.

For example: -If the passwords DON'T match, then no error message shows, form is accepted. -If the username only contains letters, the form is still accepted.

Any help/tips would be amazing. (ps this js format was used by an online tutor...not sure why it's not working for me..but I'm also a bit confused as to why he has written the js in this order!)

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <!--<link rel="stylesheet" type="text/css" href="stylesheet.css" > -->
    <link type="text/css" rel="stylesheet" href="stylesheet.css">  
    </head>

<body>
    <h1>Using regular expressions</h1>
    
    <form onsubmit="return checkForm(this);">
        <p> Username: <input type="text" name="username"/></p>
        <p> Password: <input type="password" name="pwd1"/></p>
        <p> Confirm Password: <input type="password" name="pwd2"/></p>
        <p> <input type="submit"/></p>
    
    </form>
    
    <script>
    
    function checkPassword(str) {
            var re = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8}$/;
            return re.test(str);
    
        }
    function checkForm(form) {
        if (form.username.value == "") {
            alert("Username cannot be blank!");
            form.username.focus();
            return false;        
        }
        re = /^\w+$/;
        if (!re.test(form.username.value)) {
            alert("Username must contain letters, numbers and underscores only");
            form.username.focus();
            return false;
        }
        if (form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) 
            if(!checkPassword(form.pwd1.value)){
               alert("Incorrect");
                form.pwd1.focus();
                return false;
            }
           alert("You have successfully registered!");
            return true;
        }    
            
    
    </script>
    
    
    </body>
    
    
    
</html>

To check a password between 7 to 15 characters which contain at least one numeric digit and a special character

function CheckPassword(inputtxt) {
    var paswd = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
    if (inputtxt.value.match(paswd)) {
        alert('Correct, try another...')
        return true;
    } else {
        alert('Wrong...!')
        return false;
    }
}

To check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character

function CheckPassword(inputtxt) {
    var decimal = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
    if (inputtxt.value.match(decimal)) {
        alert('Correct, try another...')
        return true;
    } else {
        alert('Wrong...!')
        return false;
    }
}

You're code is allowing invalid passwords because of this line:

if (form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) 
  if (!checkPassword(form.pwd1.value)) {
    alert("Incorrect");

It's basically saying:

  • If password 1 is not blank and both passwords are the same, validate the password.

Which doesn't make sense. Why do you only want to validate the password only if it's not blank, or if they are both the same? This allows one to input invalid passwords and get away with it as long as they put different values in the 2 password boxes, or even if they input no password at all.

Change the above code to this:

  if (form.pwd1.value != form.pwd2.value || !checkPassword(form.pwd1.value || '')) {
    alert("Incorrect");

This will show the error message if they user tries to input a blank password, both passwords are not equal, or if either password doesn't pass the validation test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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