Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Nested IFs (Javascript)

    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 <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  2. #2
    2 Star Lounger
    Join Date
    Aug 2001
    Location
    USA
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested IFs (Javascript)

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

  3. #3
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    Coppell, Texas, USA
    Posts
    168
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Nested IFs (Javascript)-MULTI-Re: Nested IFs (Ja-MULTI-Re: Nested IFs -MULTI-Re: Nested IFs (Javascript)

    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:
    <pre><INPUT ... onchange="return IsAlpha(this.value, true)"></pre>

    The return is important because now you can validate the form as follows:
    <pre>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;
    }
    </pre>

    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!

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Nested IFs (Javascript)-MULTI-Re: Nested IFs (

    Hi Charlie,

    This is MOST helpful! Thanks for taking the time to post it! <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

Posting Permissions

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