Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I've not found anyone who can figure this out...

Status
Not open for further replies.

jruiseco

Programmer
Oct 10, 2001
52
0
0
US
I have a basic form that collects user mailing address info. I am using a dropdown box for state selection. How do I setup validation so that when a user does not select a state, they get an error on submit?

Right now it lets the user roll on by without selecting a state.

Thanks...!

<!--- Code / Some states removed for this example --->

<CFSELECT NAME=&quot;ShipState&quot; SIZE=&quot;1&quot; REQUIRED=&quot;Yes&quot; MESSAGE=&quot;Please select your state...&quot;>
<OPTION>Please select your state</OPTION>
<OPTION VALUE=&quot;AL&quot;>Alabama</OPTION>
<OPTION VALUE=&quot;AK&quot;>Alaska</OPTION>
<OPTION VALUE=&quot;AZ&quot;>Arizona</OPTION>
<OPTION VALUE=&quot;AR&quot;>Arkansas</OPTION>
<OPTION VALUE=&quot;CA&quot;>California</OPTION>
</CFSELECT>
 
Hey,

Not sure what you mean, but try this:

Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

function validate(form)
{
    var option = frm.ShipState.options[frm.ShipState.options.selectedIndex].value;

    if (option == &quot;Please select your state&quot;)
    {
        alert(&quot;Error: Please select your state&quot;);
    }
    else
    {
        document.frm.submit(form);
    }
}

//-->
</SCRIPT>

<FORM NAME=&quot;frm&quot; METHOD=&quot;POST&quot; ACTION=&quot;.asp&quot;>
    <SELECT NAME=&quot;ShipState&quot;>
        <OPTION>Please select your state</OPTION>
        <OPTION VALUE=&quot;AL&quot;>Alabama</OPTION>
        <OPTION VALUE=&quot;AK&quot;>Alaska</OPTION>
        <OPTION VALUE=&quot;AZ&quot;>Arizona</OPTION>
        <OPTION VALUE=&quot;AR&quot;>Arkansas</OPTION>
        <OPTION VALUE=&quot;CA&quot;>California</OPTION>
    </SELECT> 
    <INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;Submit&quot; onClick=&quot;validate this.form)&quot;>
</FORM>

karmafree
 
Another option:
Code:
function validate()
{
    if (frm.ShipState.selectedIndex < 1)
    {
        alert(&quot;Error: Please select your state&quot;);
        return false;
    }
    else
    {
        document.frm.submit(form);
        return true;
    }
}
and change the button to:
Code:
<INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit&quot; onClick=&quot;return validate()&quot;>
 
Thanks guys,

That did the trick.

Cheers,

Josen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top