N
.Net Sports
I posed this problem last month here, but need more of a
clarification, as I still cannot correctly flag a bad password. I need
users to submit a passowrd that has at least one numeric character,
along with any amount of string-alpha characters (8 numeric characters
and 1 string character is fine, for example), but having trouble with
the correct regex, as the below still doesnt raise the error alert
when all strings with no numbers are entered:
<script language="JavaScript" type="text/javascript">
function validatePassword(password) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if (password.value == "") {
password.style.background = 'Yellow';
error = "You didn't enter a password.\n";
} else if ((password.value.length < 7) || (password.value.length >
15)) {
error = "The password is the wrong length. \n";
password.style.background = 'Yellow';
} else if (illegalChars.test(password.value)) {
error = "The password contains illegal characters.\n";
password.style.background = 'Yellow';
} else if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
(password.value.search(/(0-9)+/)))) {
error = "The password must contain at least one numeral.\n";
password.style.background = 'Yellow';
} else {
password.style.background = 'White';
}
return error;
}
</script>
Thanks in advance
netsports
clarification, as I still cannot correctly flag a bad password. I need
users to submit a passowrd that has at least one numeric character,
along with any amount of string-alpha characters (8 numeric characters
and 1 string character is fine, for example), but having trouble with
the correct regex, as the below still doesnt raise the error alert
when all strings with no numbers are entered:
<script language="JavaScript" type="text/javascript">
function validatePassword(password) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if (password.value == "") {
password.style.background = 'Yellow';
error = "You didn't enter a password.\n";
} else if ((password.value.length < 7) || (password.value.length >
15)) {
error = "The password is the wrong length. \n";
password.style.background = 'Yellow';
} else if (illegalChars.test(password.value)) {
error = "The password contains illegal characters.\n";
password.style.background = 'Yellow';
} else if (!((password.value.search(/[A-Z]+[a-z]+[0-9]/)))){ // &&
(password.value.search(/(0-9)+/)))) {
error = "The password must contain at least one numeral.\n";
password.style.background = 'Yellow';
} else {
password.style.background = 'White';
}
return error;
}
</script>
Thanks in advance
netsports