SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Philippines
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Select Box Validation for N boxes

    Hello Sir/Ma'am, I am hoping somebody can help with this: I have a form with several select boxes which all have the same options (the options represent nba team). I would like to validate this form to require that the same option is not selected on more than one select box?
    Example:

    <Select name=nbateam1>
    <option>Lakers</option>
    <option>Celtics</option>
    <option>Spurs</option>
    </Select>

    <Select name=nbateam2>
    <option>Lakers</option>
    <option>Celtics</option>
    <option>Spurs</option>
    </Select>

    <Select name=nbateam3>
    <option>Lakers</option>
    <option>Celtics</option>
    <option>Spurs</option>
    </Select>

    Thank u very much..

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">
    
    function no_dupes(oSelect)
    {
    	var oForm = oSelect.form, selval = oSelect.options[oSelect.selectedIndex].value;
    	var el, i = 1, opt, j;
    	while (el = oForm.elements['nbateam' + (i++)])
    		if (el != oSelect)
    		{
    			j = 0;
    			while (opt = el.options[j++])
    				if (!opt.defaultSelected && (opt.selected && opt.value == selval))
    				{
    					alert('\nPlease! No duplicate values.\nThanks.');
    					oSelect.selectedIndex = 0;
    					return false;
    				}
    		}
    }
    
    </script>
    </head>
    <body>
    <form>
    <select name="nbateam1" onchange="return no_dupes(this)">
    <option value="" selected="selected">&laquo; Pick Team &raquo;</option>
    <option value="Lakers">Lakers</option>
    <option value="Celtics">Celtics</option>
    <option value="Spurs">Spurs</option>
    </select>
    
    <select name="nbateam2" onchange="return no_dupes(this)">
    <option value="" selected="selected">&laquo; Pick Team &raquo;</option>
    <option value="Lakers">Lakers</option>
    <option value="Celtics">Celtics</option>
    <option value="Spurs">Spurs</option>
    </select>
    
    <select name="nbateam3" onchange="return no_dupes(this)">
    <option value="" selected="selected">&laquo; Pick Team &raquo;</option>
    <option value="Lakers">Lakers</option>
    <option value="Celtics">Celtics</option>
    <option value="Spurs">Spurs</option>
    </select>
    </form>
    </body>
    </html>
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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