SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    USA
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Requiring a selection from a drop down box

    The "Please choose..." is selected by default. I want the user to be forced to choose one of the four below it.

    <select name="product">
    <option selected>Please choose...</option>
    <option>product 01</option>
    <option>product 02</option>
    <option>product 03</option>
    <option>product 04</option>
    </select>


    The caveat is that I'm already using two onsubmits and an onclick:

    onSubmit="return (Require(this,'Name') && submitonce(this))"

    onclick="return checkit()"

    Here is the js for the them:

    Code:
    // Check for a valid email address
    
    var testresults
    function checkemail(){
    var str=document.formCheck.Email.value
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
    testresults=true
    else{
    alert("Please input a valid email address.")
    testresults=false
    }
    return (testresults)
    }
    
    function checkit(){
    if (document.layers||document.getElementById||document.all)
    return checkemail()
    else
    return true
    }
    
    // Check for required fields
    
    function Require(obForm,szFields)
        {
          var fields = szFields.split(",")
          var szMissing= new Array();
          for (x=0;x<fields.length;x++) {
            if (obForm.elements[fields[x]].value.length==0) {
               szMissing[szMissing.length]=new String(fields[x]);
            }
          }
          if (szMissing.length) {
            alert("The"+((szMissing.length>1)?"s ":" ")+szMissing.join(",")+" field must be completed.");
            return false
          }
          return true;
        }
    
    // Check for multiple submissions
    
    function submitonce(theform){
    //if IE 4+ or NS 6+
    if (document.all||document.getElementById){
    //screen thru every element in the form, and hunt down "submit" and "reset"
    for (i=0;i<theform.length;i++){
    var tempobj=theform.elements[i]
    if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
    //disable em
    tempobj.disabled=true
    }
    }
    }

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to check that the selectedIndex of your select isn't equal to 0 - which is the first element.

    document.forms['formname'].selectBoxName.selectedIndex

    or if your select element has an id:

    document.getElementById('selectBoxId').selectedIndex

    You're doing server-side checking of your form, too?


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
  •