Reputation: 40639
I am using jQuery validation plugin, it works fine for all fields
but, when I use it for select
it doesn't work
HTML
<form>
<select id="name" name="name">
<option>Select</option>
<option value='select'>Select</option>
<option value='abc'>ABC</option>
<option value='xyz'>XYZ</option>
</select>
<input type='submit' value='Submit' />
</form>
SCRIPT
var f=$("form").validate({
rules: {
name:{
required:
function(element) {
st=($(element).val() == '') || ($(element).val() == 'select');
return st;
}
}
},
messages: {
name: {required: "Please select name"}
});
Also, I want to stop submition
after validation, no matter whether the form is valid
or not
.
Note I don't want to use $.validator.addMethod
.
I've already searched
Upvotes: 0
Views: 2879
Reputation: 11862
I'm going to say it's a plugin that's slightly overcomplicating an simple error check! Then again, I found the same thing you're attempting to do on the Developer's Github Demo page.
Fiddle: http://jsfiddle.net/hAWR3/14/
<script>
$("#formsubmission").submit(function(event){
/** Cache Result **/
var tmp = $('#selectname').val();
console.log( "Selected Name: " + tmp );
if ( tmp == 'select' || tmp == 'Select' ) {
/** Prevent the submission **/
event.preventDefault();
console.log( 'Unvalidated!' );
} else {
/** Submit as normal!! **/
console.log( 'Validated!' );
}
});
</script>
<form method="post" id="formsubmission">
<select id="selectname" name="name">
<option>Select</option>
<option value='select'>Select</option>
<option value='abc'>ABC</option>
<option value='xyz'>XYZ</option>
</select>
<input type='submit' value='Submit' />
</form>
Note: if there's no value within the like in your html example, it take the encased value inside the <option></option>
's!
Upvotes: 0