#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Omaha, Nebraska
    Posts
    63
    Rep Power
    12

    Question A choice between three choices, how do I require at least one?


    SET-UP

    I have three fields, which are made up of two select menus and one text field.

    1st select menu name: material1l
    2nd select menu name: material2l
    text field name: Specmatinfo

    ISSUE

    How do I require at least one of the fields to be selected/entered and not all of them?

    Right now my code reads, if "material1l" index equals zero OR "material2l" index equals zero OR "Specmatinfo" with a value less than or equal to zero OR "Specmatinfo" with a value of zero then show alert and put focus on first select menu.

    This is not working since I still check EACH field, basically requiring the user to select an option out of the first two select menus and enter a valid value in the text field. Any suggestions or examples would be very much welcomed.

    Code:
    if (form.material1l.selectedIndex == 0 || form.material2l.selectedIndex == 0 || form.Specmatinfo.value.length <= 0 || form.Specmatinfo.value == 0){
    alert ("Please select or enter at least one material.");
    form.material1l.focus();
    return false;
    }

    Thank you ahead of time to look at this post.
    --Todd
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2003
    Location
    Italy, EU
    Posts
    517
    Rep Power
    12
    I'm not sure I've fully understood what you need... how many and which fields could be selected at the same time??
    If you need to check for the selection of JUST ONE field you can use the exclusive OR operator (the symbol is ^)... if not, post a more explicative description.
    Cheers, Dave
  4. #3
  5. HTML/CSS/Photoshop maven
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    CA
    Posts
    184
    Rep Power
    13
    No fewer than 1, no more than 2, basically, if I read you right.

    Why don't you check to see if no field is blank (if X and if Y and if Z etc.), then check to see if all 3 are selected?

    What exactly are you trying to do?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Omaha, Nebraska
    Posts
    63
    Rep Power
    12
    I appreciate the responses already.

    As noted, I have three choices made up of two select menus and one text field.

    In my form, I only want to make sure that at least one of them is selected/entered, but right now my code throws the alert and returns false, if all three are not selected/entered.

    For example, what I would like to have happen:

    1.) User one might pick a select out of the first select menu, then that should return true.

    2.) User two might just enter in a value in the text field, thus skipping the two select menus, and that should return true as well.

    Thank you again for looking at this post.
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Are all the values numerical?
  10. #6
  11. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    1,614
    Rep Power
    14
    Try this:
    Code:
    <script type="text/javascript"><!-- webmasta
    
    function testVals(f,n,v) {
    var fields =new Array('s1','s2','t1'); var passed =0;
    	for(var i=0; i< fields.length; i++) {
    		if (n =='validate' && f[fields[i]].value) {
    			passed =1; return true; }
    
    		if(v && n !=fields[i]) { f[fields[i]].value =""; }
    	} if(!passed && n =='validate') {
    	alert('At least one value is required'); return false; }
    };
    //--></script>
    
    <form name="form1"
    onsubmit="return testVals(this, 'validate')"
    method="post" action="sendItAway.cgi">
    <select name="s1"
    	onchange="testVals(this.form, this.name, this.value)">
    <option value="">select</option>
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3">c</option>
    </select>
    <select name="s2"
    	onchange="testVals(this.form, this.name, this.value)">
    <option value="">select</option>
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3">c</option>
    </select>
    <input type="text" name="t1"
    	onkeyup="testVals(this.form, this.name, this.value)">
    <input type="submit" value="submit">
    </form>
    Substitute your fieldnames and select values accordingly.
    webM

    for $i(0..20){for($j=0;$j<=$i;$j+=2){print pack(qq{H2},
    substr(qq{5f5745424d415354415f},$j,2));}print qq{\n};};

IMN logo majestic logo threadwatch logo seochat tools logo