• Nested IFs (Javascript)

    Author
    Topic
    #371404

    I’m not an expert with JavaScript, but this is the way I’ve done it before. It’s not pretty, but it gets the job done.

    By the way, do you have the numeric, phone, alpha, and email validation functions handy? If possible, could you post them (maybe as an attachment)?

    HTH and Thanks bow

    Viewing 0 reply threads
    Author
    Replies
    • #590244

      Does Javascript have a limit to the number of nested IFs? (I think Excel has a limit of 7.)

      Besides validating each input as the user enters data, I want the submit button to perform a final validation before submitting the form.

      I saw an example in a Javascript book for testing 3 input fields. Adapting the example to my form with fourteen input fields would generate code similar to the following:

      function CheckAll(form)
      {
      if (IsAlpha(form.FirstName,True)){
      (IsAlpha(form.MiddleInitial,False)){
      (IsAlpha(form.LastName,True)){
      (IsEmailValid(form.EmailAddress,False)){
      (IsYear(form.JoinedYear)){
      (IsAlpha(form.WorkCity,False)){
      (IsState(form.WorkState,False)){
      (IsZip(form.WorkPostalCode,False)){
      (IsPhone(form.WorkPhone,False)){
      (IsPhone(form.WorkFaxNumber,False)){
      (IsAlpha(form.HomeCity,False)){
      (IsState(form.HomeState,False)){
      (IsZip(form.HomePostalCode,False)){
      (IsPhone(form.HomePhone,False)){
      return true;
      }
      }
      }
      }
      }
      }
      }
      }
      }
      }
      }
      }
      }
      }
      return false;
      }

      NOTE: the True or False indicates to the validating function whether the field is required.

      Is there any easier way to accomplish this?

      The attachment has some of the actual validation routines.

      • #593448

        You don’t really need to nest the if’s in your example. It sounds like if any condition fails, you want the user to correct it – either immediately when entering data or when submitting the whole form. There is a simple trick to re-validate a form with onsubmit if you’re already doing the validation of elements using onchange:

        For each form element requiring validation, make sure you set the onchange attribute to return the result of the validation like this:

        
        

        The return is important because now you can validate the form as follows:

        function validateForm()
        {
          var f = document.theForm;
          for (var n = 0; n < f.length; n++)
          {
            if (f.elements[n].onchange)
            {
              if (!f.elements[n].onchange())
              {
                f.focus();
                return false;
              }
            }
          }
          return true;
        }
        

        Note that onchange looks for the element’s event handler, and if found, onchange() executes it (including any parameters that appear in the event handler). That allows the function to loop through your form looking for things to validate, and stop if something is not right.

        Good Luck!

        • #593463

          Hi Charlie,

          This is MOST helpful! Thanks for taking the time to post it! bravo

    Viewing 0 reply threads
    Reply To: Nested IFs (Javascript)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: