Jump to content
w3schoon

Input Select Required Attribute

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.

Share this post


Link to post
Share on other sites

You would want to search for how to get select elements selectedIndex property

Share this post


Link to post
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;}




			
		

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...