Forum Moderators: open
// Check each input in the order that it appears in the form!
if(isAlphabet(firstname, 'Please enter only letters for your first name')){
if(isAlphanumeric(street, 'Numbers and Letters Only for Street')){
if(isNumeric(zip, 'Please enter a valid Zip Code')){
if(madeSelection(s_question, 'Please Choose a Question')){
if(lengthRestriction(username, 6, 8)){
if(emailValidator(p_email, 'Please enter a valid email address')){
return true;
}
}
}
}
}
}
return false;
}
i tried to validate this form but only 'firstname' and 'street' are works..whats wrong with this?
pls help me..
Are you sure that you have all of those functions properly defined and implemented? I do not know if you simply don't have them in your code or you just omitted them for simplicity in the code example. Make sure that they are defined properly first. Next, I would check to see if you are getting any javascript errors in the error console because this is usually the best start when debugging a script that isn't working properly.
One last thing, javascript validation, while a very nice feature for a form because the form does not have to be submitted before the user gets notified of improper data, it is not the only thing that should be used to validate the information being sent to the server. Make sure you also validate this data server-side and send back the appropriate error messages in case of failure.
next problem..
if(isAlphabet(firstname, 'Please enter only letters for your first name')){
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
above os only focus on "firstname", how about if im going to validate "middlename" and "lastname", is it i have tp put ";" or make a full statement if/else one by one?
another thing, can you explain to me about the function above, cos i'll be more clear if i understand all the meanings..
..pls guide me...Thx..
if(isAlphabet(firstname, 'msg') && isAlphabet(middlename, 'msg') && isAlphabet(lastname, 'msg')){ //then do something
As far as what the function is actually doing, it is checking the string for the name to match a specific regular expression pattern for correctness. The pattern:
/^[a-zA-Z]+$/ allows only lowercase and uppercase letters as the string, and nothing else. The syntax for regular expressions can be tricky, so you might want to read up on them if you are still unsure on what they are about: [regular-expressions.info...]