w3schoon Posted February 22, 2013 Share Posted February 22, 2013 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 More sharing options...
OTTO Posted February 22, 2013 Share Posted February 22, 2013 It is possible but it will take a little more work than the text box. You will have to do this through Javascript. Link to comment Share on other sites More sharing options...
w3schoon Posted February 23, 2013 Author Share Posted February 23, 2013 Can you give more idea or any codes? Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2013 Share Posted February 23, 2013 You would want to search for how to get select elements selectedIndex property Link to comment Share on other sites More sharing options...
dsonesuk Posted February 23, 2013 Share Posted February 23, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now