I have a text field with the following validation rules:
- not null or empty;
- alphanumeric;
- 8 characters
- no space between or before/after
I wrote some Javascript functions to do the rules 1,2,3, but don't know how to do with 4:
1. check if null or empty
function isEmpty(str) {
if(str == null || str.length == 0) {
return true;
}
return false;
}
2. check if alphanumeric
function validateAlphaNumeric( strVal ) {
var regExp = /^[a-zA-Z0-9 ]*$/;
return regExp.test(strVal);
}
3. check if 8 characters
function validateInputLength( str, length ) {
if(str.length == length ){
return true;
}
return false;
}
Is there a way that I update check 2 with a regular expression including check 4 (check if any space bar input)? Or shall I use a for loop to check the whole string, if so, how? Or any other solutions? Sorry, I am not very good in Javascripts and regular expressions.
Thanks
Sam