Select Box Validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Kramer

    Select Box Validation

    I am having issues with making sure users select an option from a select
    box... here is my code

    function checkit(){
    if (document.postd ata.phonenumber == ""){
    alert("Please enter a Phone Number");
    return false;}
    if (document.postd ata.request[document.postda ta.request.sele ctedIndex].value
    == ""){
    alert("Please Select a Request");
    return false;}
    }




    <form name=postdata action=?action= add method=post onsubmit='retur n
    checkit()'>
    <input type=text name=phonenumbe r>
    <select name=request>
    <option value=1>Send manual</option>
    <option value=2>Duplica te Product</option>
    <option value=3>Did not order</option>
    </select>
    <input type=submit>
    </form>

    can someone help me figure this out. I believe it is because
    document.postda ta.request is undefined until an option is chosen but I don't
    know how to fix that.





  • Dave Vick

    #2
    Re: Select Box Validation

    Brian

    Check for a selectedIndex value of -1. If no item is selected in a
    Select object then:
    document.formNa me.selectObject Name.selectedIn dex
    will return -1.

    Hope that helps

    Comment

    • Lee

      #3
      Re: Select Box Validation

      Brian Kramer said:[color=blue]
      >
      >I am having issues with making sure users select an option from a select
      >box... here is my code
      >
      >function checkit(){
      >if (document.postd ata.phonenumber == ""){
      >alert("Pleas e enter a Phone Number");
      >return false;}
      >if (document.postd ata.request[document.postda ta.request.sele ctedIndex].value
      >== ""){
      >alert("Pleas e Select a Request");
      >return false;}
      >}
      >[/color]
      [color=blue]
      >can someone help me figure this out. I believe it is because
      >document.postd ata.request is undefined until an option is chosen but I don't
      >know how to fix that.[/color]

      No, the problem is that the value of selectedIndex is -1 until an option
      is chosen. and document.postda ta.request[-1].value is undefined.
      That should be all you need to know to fix it.

      Comment

      Working...