Reputation: 99
I'm validating an initially hidden form using jquery, but the form is submitted even though the fields are empty
$(document).ready(function () {
jQuery("#name").validate({
expression: "if (VAL) return true; else return false;",
message: "* Please enter name"
});
});
I'm doing this for ajax submition of the form ..
$("#send").click(function (e) {
e.preventDefault();
var name = $('#name').val();
$.ajax({
// ajax submition
)};
});
Upvotes: 0
Views: 103
Reputation: 2306
Try this... and use the <form>
id instead for the validate function.
$(document).ready(function () {
jQuery("#formid").validate({
ignore:'',
rules:{
name:{
required:true
}
},
messages:{
name:{
required:'* Please enter name'
}
}
});
});
I think you are using the newer version of jQuery validator plugin which ignores hidden elements by default.
In reply to your updated question you can just do this
$("#send").click(function (e) {
e.preventDefault();
var name = $('#name').val();
if(name.length > 0){ //if length is greater than 0 then only fire ajax else show the error
$.ajax({
// ajax submition
)};
}else
//show error
alert('Name not found');
}
}
Upvotes: 1