Reputation: 449
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.
Upvotes: 0
Views: 1418
Reputation: 7351
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)
Upvotes: 2
Reputation: 943089
There's no need to try to squeeze all of this into one regex.
password.match(/[A-Z]/) && password.match(/[0-9]/) && (password.length >= 6)
Upvotes: 7