PDA

View Full Version : Validation to allow numbers letters only (Regular Expression)



aakinn
12-03-2009, 03:05 PM
function usernameValidate(usernameString) {
var valid = true;

if ( usernameString == "" ) {
feedback('userMess','Enter your username here',7);
valid = false;
}
else if ( usernameString.length <= 5 ){
feedback('userMess','Username too short',7);
valid = false;
}
else if ( !validUsernameString(usernameString) ) {
feedback('userMess','Username contails illegal characters',7);
valid = false;
}
else feedback('userMess', '✔',1);

if ( valid ) return true;
else return false;
}


function validUsernameString(usernameString) {
// regular expression
var filter = /^[a-z]|[0-9]/;
if (filter.test(usernameString)) return true;
else return false;
}

Im pretty sure the regular expression used is incorrect. The field only requires the text box to allow only numbers and letters and no other characters. At the moment you are able to enter any character in the textbox and it says it is valid.

What would be the correct regular expression to use for this type of validation?

Regards
Akin

jscheuer1
12-03-2009, 03:46 PM
/^[A-Za-z0-9]+$/

aakinn
12-03-2009, 04:17 PM
Cheers mate works now

Much appreciated
Akin