Jump to content

Input Select Required Attribute


w3schoon

Recommended Posts

Nice be with you everyone! I found out that input required attribute requires "that an input field must be filled out before submitting the form." like this...

<form action="demo_form.asp">Username: <input type="text" name="usrname" required><input type="submit"></form>

My question is: Is it possible also to use input required attribute in select? like this...

<select name="gender" required><option value="" selected="selected">Choose...</option><option value="male">Male</option><option value="female">Female</option></select>

I want to validate gender field if it has already value (male or female)otherwise will not submit like what input required attribute can do Thank you.

Link to comment
Share on other sites

you would just check the current selected value

<select name="gender" id="gender"><option value="" selected="selected">Choose...</option><option value="male">Male</option><option value="female">Female</option></select>

when the submit form is submitted using onsubmit="return validate();"[/code]function validate(){var valid = true;var gender = document.getElementById('gender');if(gender.selectedIndex ==0) //the very first option to see if it is still set as being currently selected{valid = false;}return valid;}




			
		
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...