简体   繁体   中英

Password Regular expression validation?

I got 2 password fields called password and confirm password. I managed to validate it to make sure that the passwords must match. However, I do not know how to add regular expressions to the password field to allow it 6 characters long, containing at least an uppercase and a number.

This is the javascript i got so far

<script type="text/javascript" language="javascript">

function validatepw() {
  if ( document.register.user_password.value != document.register.user_password_confirm.value)
    {
    alert('Passwords did not match!');
        return false;
        }else{
        document.register.submit();
        return true;
    }
    }
</script>

and this is my form

<form name="register" action="signup.php" onsubmit="return validate_reg()" 
enctype="multipart/form-data" method="post" >

<table width="600" border="0">
<tr><td width="210" height="45">Username*:</td><td>
<input type="text" size="40" name="userUsername" id="user_username" /></td></tr>
<tr><td width="210" height="45">Password*:</td><td>
<input type="password" size="40" name="userPassword" id="user_password"/></td></tr> 
    <tr><td width="210" height="45">Re-type Password:</td><td>
<input type="password" size="40" name="userPasswordConfirm" 
id="user_password_confirm" onchange="javascript:validatepw()"/></td></tr>
</table>
<center><input type="submit" class="button" name="submit" value="register"></center>    
</form>

Can anyone show how to apply the regular expression of 6 characters, an uppercase and a number to the password field. I have searched a lot and cant find anything that works with what i already got.

无需尝试将所有这些压缩到一个正则表达式中。

password.match(/[A-Z]/) && password.match(/[0-9]/) && (password.length >= 6)

You certainly can use a single regex for this if you'd like to:

var isValid = /(?=.*\d)(?=.*[A-Z]).{6,}/.test(password)

(Lookahead for at least one digit, lookahead for at least one letter, then match anything, 6 or more times)

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