Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    LCM
    LCM is offline
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile JS multiple validation form does not check

    Hello,

    Hope someone can shed some light on this! Been trying to figure out this for 2 days now, I don't know why it's not running the validation js first upon "submit", it goes straight to the "thankyou" page specified by the php (let me know if php needs to be posted). I've check my syntax over and over, I can't see anything. I'm not sure what's going wrong?!

    I'm trying to have a "tell-a-friend" form where one requires to enter his/her first name, surname, email and a friend's email – with option of forward to to other 4 friends (total 5).

    Here are the fields I need to validate:
    1. Required Fields: First name, Surname, Email, and Friend's email 1
    2. First name: Check for alphabets only
    3. Surname: Check for alphabets only
    4. Email & Friend's Email 1 (plus the optional 4 emails): email address validation


    My js (validationB.js) file:

    Code:
    function checkForm() {
       
       // Make quick references to fields
       var fname = document.tellafriend('fname'); 
       var sname = document.tellafriend('sname'); 
       var email = document.tellafriend('email'); 
       var fmail1 = document.tellafriend('fmail1'); 
       var fmail2 = document.tellafriend('fmail2'); 
       var fmail3 = document.tellafriend('fmail3'); 
       var fmail4 = document.tellafriend('fmail4'); 
       var fmail5 = document.tellafriend('fmail5'); 
       
       // Check input fields
       if(isEmpty(fname, "Please enter your first name")) {
       if(isEmpty(sname, "Please enter your surname")) {
       if(isEmpty(email, "Please enter your email address")) {
       if(isEmpty(fmail1, "Please enter your friend's email address")) {
       if(isAlphabet(fname, "Please enter only letters for your first name")) {
       if(isAlphabet(sname, "Please enter only letters for your surname")) {
       if(emailValidator(email, "Please enter a valid email address")) {
       if(emailValidator(fmail1, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail2, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail3, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail4, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail5, "Please enter your friend's valid email address")) {
        return true; 
                                           }
                                        }
                                     }
                                  }
                               }
                            }
                         }
                      }
                   }
                }
             }
          }
       return false; 
       }
    
    function isEmpty(elem, helperMsg) {
       if(elem.value.value == ""){
             return true; 
             }
          else {
             alert(helperMsg); 
             elem.focus(); 
             return false; 
             }
          }
    
    function isAlphabet(elem, helperMsg) {
          var alphaExp = /^[a-zA-Z]+$/; 
          if(elem.value.match(alphaExp)) {
             return true; 
             }
          else {
             alert(helperMsg); 
             elem.focus(); 
             return false; 
             }
          }
    
    function emailValidator(elem, helperMsg) {
          var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; 
          if(elem.value.match(emailExp)) {
             return true; 
             }
          else {
             alert(helperMsg); 
             elem.focus(); 
             return false; 
             }
          }
    my html form part:

    Code:
    <script type="text/javascript" src="js/validationB.js"></script>
    
    <form name="tellafriend" action="tellafriend.php" method="post" onsubmit="return checkForm();">
    
    *First name: <input size="30" type="text" id="fname" name="fname" />
    *Surname: <input size="30" type="text" id="sname" name="sname" />
    *Email: <input size="30" type="text" id="email" name="email" />
    *Friend's Email 1: <input size="30" type="text" id="fmail1" name="fmail1" />
    Friend's Email 2: <input size="30" type="text" id="fmail2" name="fmail2" />
    Friend's Email 3: <input size="30" type="text" id="fmail3" name="fmail3" />
    Friend's Email 4: <input size="30" type="text" id="fmail4" name="fmail4" />
    Friend's Email 5: <input size="30" type="text" id="fmail5" name="fmail5" />
    <input type="submit" title="send" value="" class="sendForm" />
    <input type="reset" title="clear form" value="" class="resetForm" />
    </form>
    I'm not sure what's going wrong. Any guidance much appreciated!
    Last edited by LCM; 04-26-2010 at 05:31 PM. Reason: It works now. This is now resolved.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    if(elem.value.value == ""){

    should be if(elem.value.value != ""){


    Delete the following:-
    Code:
      if(emailValidator(fmail2, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail3, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail4, "Please enter your friend's valid email address")) {
       if(emailValidator(fmail5, "Please enter your friend's valid email address")) {
    and the corresponding number (4) of closing braces } after "return true".

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


    NEW! FREE DELIVERY when you collect from store. - Debenhams' advertisement

  • #3
    LCM
    LCM is offline
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by LCM View Post
    Code:
    function checkForm() {
       
       // Make quick references to fields
       var fname = document.tellafriend('fname'); 
       var sname = document.tellafriend('sname'); 
       var email = document.tellafriend('email'); 
       var fmail1 = document.tellafriend('fmail1'); 
       var fmail2 = document.tellafriend('fmail2'); 
       var fmail3 = document.tellafriend('fmail3'); 
       var fmail4 = document.tellafriend('fmail4'); 
       var fmail5 = document.tellafriend('fmail5');
    Oh i've also changed this the "tellafriend" part to "getElementById" and seems to be working.

    Philip, thanks for your response but now that i've deleted the 4 lines, it doesn't check for the email format any more for fmail2, fmail3, fmail4, fmail5.

    Also, should

    Code:
    if(elem.value.value != ""){
    has only 1 value i.e.

    Code:
    if(elem.value != ""){
    ?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by LCM View Post
    Philip, thanks for your response but now that i've deleted the 4 lines, it doesn't check for the email format any more for fmail2, fmail3, fmail4, fmail5.
    Do you require five friends' email addesses? If not wrap each of the 2nd - 5th as follows:-

    Code:
    if (fmail2.value.length > 0) {
    if(emailValidator(fmail2, "Please enter your friend's valid email address")) {
    }
    Yes, how silly. if(elem.value != ""){ I did not notice the repeated value.

    These all-purpose all-singing all-dancing validation routines are a pain in the undercarriage!
    Last edited by Philip M; 04-26-2010 at 04:46 PM.

  • Users who have thanked Philip M for this post:

    LCM (04-26-2010)

  • #5
    LCM
    LCM is offline
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Do you require five friends' email addesses? If not wrap each of the 2nd - 5th as follows:-

    Code:
    if (fmail2.value.length > 0) {
    if(emailValidator(fmail2, "Please enter your friend's valid email address")) {
    }
    Yes, how silly. if(elem.value != ""){ I did not notice the repeated value.

    These all-purpose all-singing all-dancing validation routines are a pain in the undercarriage!
    Argh, tell me about it!!!

    Yes, fmail2-5 are optional not required fields. When I added your new code, the script checks for invalid format but once enter correct format and press submit, it does nothing.

    I ended up adding another function:

    Code:
    function isFilled(elem, helperMsg) {
    	if(elem.value.length > 0){
    		return true;
    	}
    	else {
    		alert(helpterMsg);
    		elem.focus();
    		return false;
    		}
    	}
    then called it on top in "function checkForm()"

    Code:
    if(isFilled(fmail2, "Please enter your friend's valid email address")) {
                               if(emailValidator(fmail2, "Please enter your friend's valid email address")) {
    It seems to all work now! Thank you for your help!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •