I\'m using the jQuery plugin Validation to validate a form. I have a select list looking like this:
you want to make sure that the user selects anything but "Choose an option" (which is the default one). So that it won't validate if you choose the first option. How can this be done?
You can do this by simple adding attribute required = "required" in the select tag. you can see it in below code
<select id="select" required="required">
<option value="">Choose an option</option>
<option value="option1">Option1</option>
<option value="option2">Option2</option>
<option value="option3">Option3</option>
</select>
It worked fine for me at chorme, firefox and internet explorer. Thanks
if (select == "") {
alert("Please select a selection");
return false;
That should work for you. It just did for me.
Perhaps there is a shorter way but this works for me.
<script language='JavaScript' type='text/javascript'>
function validateThisFrom(thisForm) {
if (thisForm.FIELDNAME.value == "") {
alert("Please make a selection");
thisForm.FIELDNAME.focus();
return false;
}
if (thisForm.FIELDNAME2.value == "") {
alert("Please make a selection");
thisForm.FIELDNAME2.focus();
return false;
}
}
</script>
<form onSubmit="return validateThisFrom (this);">
<select name="FIELDNAME" class="form-control">
<option value="">- select -</option>
<option value="value 1">Visible info of Value 1</option>
<option value="value 2">Visible info of Value 2</option>
</select>
<select name="FIELDNAME2" class="form-control">
<option value="">- select -</option>
<option value="value 1">Visible info of Value 1</option>
<option value="value 2">Visible info of Value 2</option>
</select>
</form>
<script type="text/JavaScript">
function validate()
{
if( document.form1.quali.value == "-1" )
{
alert( "Please select qualification!" );
return false;
}
}
</script>
<form name="form1" method="post" action="" onsubmit="return validate(this);">
<select name="quali" id="quali" ">
<option value="-1" selected="selected">select</option>
<option value="1">Graduate</option>
<option value="2">Post Graduate</option>
</select>
</form>
// this code works 110% tested by me after many complex jquery method validation but it is simple javascript method plz try this if u fail in drop down required validation//
Just add a class of required to the select
<select id="select" class="required">
<select id='bookcategory' class="form-control" required="">
<option value="" disabled="disabled">Category</option>
<option value="1">LITERATURE & FICTION</option>
<option value="2">NON FICTION</option>
<option value="3">ACADEMIC</option>
<option value="4">CHILDREN & TEENS</option>
</select>
HTML form validation can be performed automatically by the browser.
Try the above code:
The rest all will be done automatically, no need to create any js functions just this dropdown and a submit button.