0

I'm wondering, what would be a short/good way of performing form validation in JavaScript by looping through all input-text as well as <select>, however, the condition on the select is that out of the 2 selects, ONLY one needs to be selected.

<form id="daform">
<input type="text" value="" name="name" id="name" />
<input type="text" value="" name="last" id="last" />

<select id="choice1" name="choice1">
 <option>Bye</option>
 <option>Hello</option>
</select>

<select id="choice2" name="choice2">
 <option>Bye</option>
 <option>Hello</option>
</select>

<input type="submit" />
</form>

3 Answers 3

1

Look into,document.getElementsByTagName().

Sign up to request clarification or add additional context in comments.

Comments

0

You really should use some javascript toolkit for help with this, but if not this might help:

validateSelectChoices = function(){
  return document.getElementById('choice1').selectedIndex || document.getElementById('choice2').selectedIndex;
}

This will check to see if one of the select boxes has the 'hello' value selected (keep in mind that dropdowns will always default to the first option in the list, in your case 'bye'.

Comments

0

Have you tried the jQuery validation plugin?
You can see a demo here.

2 Comments

Thanks, the thing is that I'm not using jQuery.
I think your life will be more easier if you use jQuery ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.