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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript formchecking recognizing error but letting you threw

    I have this code and when you make a mistake in the input, it will give you the error message, but when you click OK, then you can go to the next page. Also, if there is no error, then it wont let you submit it. here is the code:
    Code:
    <script Language="JavaScript">
    
    function IsEmpty(aTextField) {
    
       if ((aTextField.value.length==0) ||
    
       (aTextField.value==null)) {
    
          return true;
    
       }
    
       else { return false; }
    
    }
    
    function IsNumeric(sText)
    
    {
    
       var ValidChars = "0123456789";
    
       var IsNumber=true;
    
       var Char;
    
       for (i = 0; i < sText.length && IsNumber == true; i++) 
    
          { 
    
          Char = sText.charAt(i); 
    
          if (ValidChars.indexOf(Char) == -1) 
    
             {
    
             IsNumber = false;
    
             }
    
          }
    
       return IsNumber;   
    
    }
    
    function ValidateForm(form)
    
    {
    
       if(IsEmpty(form.adult_number || form.children_number)) 
    
       { 
    
          alert('You need to enter the amount of people that are attending this vacation.') 
    
          form.account_number.focus(); 
    
          return false; 
    
       }
    
       if(form.adult_number && form.children_number < 1) 
    
       { 
    
          alert('You need to enter the amount of people that are attending this vacation.') 
    
          form.account_number.focus(); 
    
          return false; 
    
       }
    
      
       if (!IsNumeric(form.adult_number.value || form.children_number.value || form.infant_number.value)) 
    
       { 
    
          alert('Please enter only numbers the fields') 
    
          form.account_number.focus(); 
    
          return false; 
    
       } 
    
    return false;
    
    } 
    
    </script>
    Last edited by shortkid422; 01-03-2004 at 02:40 AM.

  • #2
    New Coder
    Join Date
    Jan 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when you are calling it in the <form name"whatever" Action="whereEver.htm" onSubmit="return yourValadationFunction()" >

    it is key that you have a return false if there is a problem with a blank field

    there is also a form validation script in the download scripts area that i made and you can tell the name of the field that is null it is quite simple to use

    best of luck

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function IsEmpty(aTextField) {

    if ((aTextField.value.length==0) ||

    (aTextField.value==null)) {

    return true;

    }

    else { return false; }

    }


    you have the returns in here backwards

    should be

    function IsEmpty(aTextField) {

    if ((aTextField.value.length==0) ||

    (aTextField.value==null)) {

    return false;

    }

    else { return true; }

    }


  •  

    Posting Permissions

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