Forum Moderators: open

Message Too Old, No Replies

Form Validation

         

WittyJezzy 83

1:37 am on Feb 26, 2008 (gmt 0)

10+ Year Member



function formValidator(){
// Make quick references to our fields
var firstname = document.getElementById("firstname");
var middlename = document.getElementById("middlename");
var lastname = document.getElementById("lastname");
var gender = document.getElementById("gender");
var username = document.getElementById("username");
var s_question = document.getElementById("s_question");
var s_answer = document.getElementById("s_answer");
var p_email = document.getElementById("p_email");
var s_email = document.getElementById("s_email");
var dob = document.getElementById("dob");
var street = document.getElementById("street");
var suburb = document.getElementById("suburb");
var zip = document.getElementById("zip");
var state = document.getElementById("state");
var country = document.getElementById("country");

// 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..

eelixduppy

5:44 pm on Feb 26, 2008 (gmt 0)



Welcome to WebmasterWorld!

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.

WittyJezzy 83

2:19 am on Feb 27, 2008 (gmt 0)

10+ Year Member



Thx..^_^
i already define all those functions properly..maybe theres a bit mistake..i will check it.actually i want to embed this code into PHP and HTML..

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..

eelixduppy

7:47 pm on Feb 27, 2008 (gmt 0)



It would follow this format:

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...]