www.webdeveloper.com
Results 1 to 4 of 4

Thread: Validating Input against defined list

  1. #1
    Join Date
    Dec 2004
    Posts
    47

    Validating Input against defined list

    Hi,

    I have a selection list that users can choose one value from about 300 different values. I wish to add some cross value vaidations to ensure some extra data gets entered should the user choose one of 50 of these values. Now I know I can write a JS validation individully fo each of the 50 as follows:

    if theForm.field1.value=="25" && theorm.field2.value =="" {
    alert("Enter field2 value");
    }

    Then repeat this alidation 50 times.

    My question - is there a way to shorten the code to someting like this:

    var fieldx = "1, 2, 3, 4, 5"; etc with all 50 values defined here, then something like:

    if theForm.field1.value==fieldx && theForm.field2.value =="" {
    alert("Enter field2 value");
    }

    Can anyone explain to me the correct way to go about this with the correct Javascript syntax?? Any help at all would be greatly appreciated.

    Paulb

  2. #2
    Join Date
    Oct 2004
    Location
    BC, Canada
    Posts
    8
    I don't know if I quite understand your question, but maybe if you set up an array of the values:

    Code:
    var fieldx = [];
    fieldx.push("1");
    fieldx.push("2");
    fieldx.push("3");
    // ...
    fieldx.push("50");
    
    // this would give you: fieldx["1", "2", "3", ..., "50"]
    
    // Note that you can enter in numbers directly by leaving out the ""
    // fieldx.push(1);
    // fieldx.push(2);
    // fieldx.push(3);
    // ...
    // fieldx.push(50);
    
    // gives you
    // fieldx[1, 2, 3, ..., 50]
    // I commented out the above in case you are copy pasting the code...
    
    // then grab the form value and check it against all the values in the array
    var theFormValue = theForm.field1.value;
    for(var index=0; index<fieldx.langth; index++){
        if(theFormValue == fieldx[index] && theForm.field2.value ==""){
            alert("Enter field2 value");
        }
    }
    Hope that helps out!
    -Dave.

  3. #3
    Join Date
    Dec 2004
    Posts
    47
    Thanks so much Crocket - I managed to get this to work form me and has solved my problem!!

  4. #4
    Join Date
    Oct 2004
    Location
    BC, Canada
    Posts
    8
    No Worries - I just noticed a spelling error... don't know what other errors may have slipped into my code at 5am (could not sleep)...

    Looking at it today, I did think of a way you could improve the efficiency a bit though...

    Code:
    if(theForm.field2.value ==""){
        var theFormValue = theForm.field1.value;
        for(var index=0; index<fieldx.length; index++){
            if(theFormValue == fieldx[index]){
                alert("Enter field2 value");
            }
        }
    }
    This way, if they have already entered in field2 you don't even have to check

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles