PDA

View Full Version : Field validation Problem (using Reg Exp)



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 :)

Kor
Sep 6th, 2007, 01:33 PM
try this:


var lettermask = /^[A-z]*$/;
var numbermask = /^\d*$/;

Philip M
Sep 6th, 2007, 02:10 PM
try this:


var lettermask = /^[A-z]*$/;
var numbermask = /^\d*$/;


* means zero or more occurrences. ^ means occurs at the start of the string and $ occurs at the end of it. Neither of these are required.

In fact what is asked for is at least one letter and at least one number.

So the correct regex is simply

var lettermask = /[A-z]/; // a letter A - z
var numbermask = /\d/; // a digit

You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html;

kwalker
Sep 6th, 2007, 02:48 PM
The problem that I had:

The statement:
var password = document.getElementById('password');

should have been:
var password = document.getElementById('password').value;

I was trying to pass the object to the matchPassword instead of the value to compare to the regular expression.

All works now - thanks for helping me out! :thumbsup: