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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation - dropdown and input box

Status
Not open for further replies.

Pr0ject

IS-IT--Management
Jul 8, 2004
7
0
0
US
Odd request... but need help. I have a database populated drop down menu that a user will select from. In the event the drop down does not have what they are looking for, there is an input box they can fill out before submission.

What I need is a way to validate that if someone mistakenly makes a selection AND puts something in the input box, it errors out.

Can this be done?
 
take a look at this:
Code:
<html>
<head>
<title>menu</title>
<script language=&quot;javascript&quot;>
function validate() {
 if( (document.frm.nmbr.selectedIndex != 0) && (document.frm.txt.value.length != 0)) {
  alert(&quot;you cannot do both&quot;)
 } 
}
</script>
</head>
<body>

<form name=&quot;frm&quot;>
    <select name =&quot;nmbr&quot;>
    <option>Please select a number
    <option>one
    <option>two
    <option>three
    </select>
    <br>
    <input type=&quot;text&quot; name=&quot;txt&quot;>   
    <input type=&quot;button&quot; name=&quot;valid&quot; value = &quot;Validate&quot;
    onClick=&quot;validate()&quot;>
</form>

</body>
</html>

there's just one note there. Even if a use does not choose any options from the drop down, the first one is gonna be shown to them and in JS land it means that the first option is actually selected i.e. chosen.
that's why I have the first option as a generic statement and not an actual choice. --------------------------------------------------
Goals are dreams with deadlines
 
you rock! That should work... at least steers me in the right direction. Thanks!
 
one last thing... I already have the form named something specific in order to check a field for non alphanumeric characters... how do I circumvent the name=&quot;&quot; when it's already assigned for another javascript function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top