kwalker
Sep 6th, 2007, 01:06 PM
I am having a problem with matching a string to a regular expression, well in fact the opposite happens to what i expect, heres my code:
function matchPassword(value, mask) {
alert("The mask: " + mask);
var matches = mask.test(value);
alert(value.value + " meets the mask: " + matches);
return matches;
}
function validatePassword(){
var password = document.getElementById('password');
var passwordconfirm = document.getElementById('passwordconfirm');
var lettermask = new RegExp(".*[a-zA-Z].*");
var numbermask = new RegExp(".*[0-9].*");
//need to do checks for the following
//check that password is longer then 8
if (password.value.length < 8 ) {
alert("Password can not be less then 8 alphanumeric characters.");
return false;
}
//check that the password contains one letter and one number
if (!matchPassword(password, lettermask)){
alert("No letter: " + password.value);
alert("Password must contain at least one letter.");
return false;
}
if (!matchPassword(password, numbermask)){
alert("No number: " + password.value);
alert("Password must contain at least one number");
return false;
}
//check that password and passwordconfirm match
if (password.value != passwordconfirm.value){
alert("The confirm password does not match the password provided.");
return false;
}
return true;
}
(ignoring the debug alert statements) so basically if i was to enter a password "12345678" then it should be false for matching with the regular expression .*[a-zA-Z].* as there are no letters in the password. However its returning true with that mask from the match password function and false for the .*[0-9].*
Could be me.... the regular expressions but they seem to be correct according to tons of online checkers such as http://www.nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp
a hack to me would be just to change the if statements removing the !'s, but that doesn't seem right to me
Any ideas would be great :)
function matchPassword(value, mask) {
alert("The mask: " + mask);
var matches = mask.test(value);
alert(value.value + " meets the mask: " + matches);
return matches;
}
function validatePassword(){
var password = document.getElementById('password');
var passwordconfirm = document.getElementById('passwordconfirm');
var lettermask = new RegExp(".*[a-zA-Z].*");
var numbermask = new RegExp(".*[0-9].*");
//need to do checks for the following
//check that password is longer then 8
if (password.value.length < 8 ) {
alert("Password can not be less then 8 alphanumeric characters.");
return false;
}
//check that the password contains one letter and one number
if (!matchPassword(password, lettermask)){
alert("No letter: " + password.value);
alert("Password must contain at least one letter.");
return false;
}
if (!matchPassword(password, numbermask)){
alert("No number: " + password.value);
alert("Password must contain at least one number");
return false;
}
//check that password and passwordconfirm match
if (password.value != passwordconfirm.value){
alert("The confirm password does not match the password provided.");
return false;
}
return true;
}
(ignoring the debug alert statements) so basically if i was to enter a password "12345678" then it should be false for matching with the regular expression .*[a-zA-Z].* as there are no letters in the password. However its returning true with that mask from the match password function and false for the .*[0-9].*
Could be me.... the regular expressions but they seem to be correct according to tons of online checkers such as http://www.nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp
a hack to me would be just to change the if statements removing the !'s, but that doesn't seem right to me
Any ideas would be great :)