1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Javascript Form Validation

Discussion in 'Web Design & Development' started by JiminSA, Apr 22, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,350
    First Name:
    Jim
    I sometimes use a form generator in order to handle javascript validation, as my js knowledge is so poor. The code below shows generated js validation for a form I'm currently doing plus my attempt to do an extra validation (in red) ...
    Code:
    <script type="text/javascript">
    function LL_Booking_Form_CF(){
     var errormessage = new String();
    var vF = document.forms["LL_Booking_Form"];
    if(LL_Booking_Form_WithoutContent(vF["fname"].value)){errormessage += "\n\nPlease fill in the \"Your Full Name\" edit field";}
    if(LL_Booking_Form_WithoutContent(vF["email"].value)){errormessage += "\n\nPlease fill in the \"Your email\" edit field";}
    if(LL_Booking_Form_NotEmail(vF["email"].value)){errormessage += "\n\nThe entry for \"Your email\" does not appear to be a valid email address";}
    if(LL_Booking_Form_WithoutContent(vF["date1"].value)){errormessage += "\n\nPlease fill in the \"Stay From\" edit field";}
    if(LL_Booking_Form_WithoutContent(vF["date2"].value)){errormessage += "\n\nPlease fill in the \"Stay To\" edit field";}
    if(LL_Booking_Form_WithoutSelectionValue(vF["guests"])){errormessage += "\n\nPlease select something from the\"Number of Guests\" drop-down list";}
    
    [COLOR="Red"]if((vF["guests"].value)=">4";)
    {
    	if(LL_Booking_Form_WithoutContent(vF["over4"].value)){errormessage += "\n\nPlease fill in the \"Over 4 Guests\" edit field";}
    }[/COLOR]
    
    if(LL_Booking_Form_NotNumeric(vF["over4"].value)){errormessage += "\n\n \"Over 4 Guests\" can only contain numerical digits";}
    
    if (errormessage.length > 2){
        alert("There are errors or ommissions in the form" + errormessage + "\n");
        return false;
        }
    return true;
    } // end of LL_Booking_Form_CF()
    
    function LL_Booking_Form_WithoutContent(ss){
      if (ss.length>0){return false;}
    return true;
    }
    
    function LL_Booking_Form_WithoutCheck(ss){
      if(ss.checked){return false;}
    return true;
    }
    
    function LL_Booking_Form_NotEmail(ss){
      var splitted = ss.match("^(.+)@(.+)$");
      if (ss.length == 0){return false;}
      if(splitted == null) return true;
      if(splitted[1] != null){
        var regexp_user=/^\"?[\w-_\.]*\"?$/;
        if(splitted[1].match(regexp_user) == null) return true;
      }
      if(splitted[2] != null){
        var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if(splitted[2].match(regexp_domain) == null){
    	 var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
          if(splitted[2].match(regexp_ip) == null) return true;
        }
      return false;
      }
    return true;
    }
    
    function LL_Booking_Form_NotNumeric(ss){
      var charpos = ss.search("[^0-9]");
      if(ss.length > 0 &&  charpos >= 0){return true;}
    return false;
    }
    
    function LL_Booking_Form_WithoutSelectionValue(ss){
       for(var i = 0; i < ss.length; i++){
         if(ss[i].selected){
           if(ss[i].value.length){return false;}
         }
       }
    return true;
    }
    </script>
    <noscript><strong>Your browser either does not support JavaScript or it is turned off.<br>Without Javascript enabled, this form will not function correctly</strong></noscript>
    
    
    The problem being my addition invalidates the script (i.e, it works fine without it, but does nothing with it included)...
    I am simply trying to check for user input in the 'over4' field element when 'guests' contains a value of '>4' from a dropdown selection. But my code's not cutting it - can anyone show me where my code is failing, please?
     
  2. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    I'm not sure if this is the entire problem, but you have a semicolon in the condition of your if statement.
    Semicolons are like full stops in English, so if you have them in the wrong place. the interpreter thinks you've finished.
     
  3. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,350
    First Name:
    Jim
    Brilliant:D - Thanks Josiah, I wouldn't have found that in a month of Sundays(y)
     
  4. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,350
    First Name:
    Jim
    Guess I was a bit hasty, marking it solved:eek: Another problem from my code has just surfaced... it's actually setting 'guests' to '>4' as opposed to just checking for the value '>4' possibly selected by the user???
     
  5. JiminSA

    JiminSA Thread Starter

    Joined:
    Dec 15, 2011
    Messages:
    3,350
    First Name:
    Jim
    More by luck than judgement I realised my error and amended the code with these additions, having taken out the error lines ...
    Code:
    if(LL_Booking_Form_GuestsValueOver4(vF["guests"].value))
    {
    	if(LL_Booking_Form_WithoutContent(vF["over4"].value)){errormessage += "\n\nPlease fill in the \"Over 4 Guests\" edit field";}
    }
    function LL_Booking_Form_GuestsValueOver4(ss) {
      if(ss == ">4") {return true;}
    return false;
    }
    
    and it's now working properly - thanks Josiah for the lead...:)
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Solved Javascript Form
  1. darshit
    Replies:
    1
    Views:
    438
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1096804

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice