I am validating a text box using jquery validate plug-in, but when I submit the button it is not validating. It is showing an error at submit Handler in firebug:
Error
TypeError:$(...)validate is not a function
Code
function saveData() {
$(document).ready(function () {
$("#form1").validate({
rules: {
txtName: {
required: true
}
},
message: {
txtName: {
required: "Field should not be empty"
}
},
submitHandler: function (form) {
var txtName = $("#txtName").val();
var txtEmail = $("#txtEmail").val();
var txtSurName = $("#txtSurName").val();
var txtMobile = $("#txtMobile").val();
var txtAddress = $("#txtAddress").val();
$.ajax({
type: "POST",
url: location.pathname + "/saveData",
data: "{Name:'" + txtName + "',SurName:'" + txtSurName + "',Email:'" + txtEmail + "',Mobile:'" + txtMobile + "',Address:'" + txtAddress + "'}",
contentType: "application/json; charset=utf-8",
datatype: "jsondata",
async: "true",
success: function (response) {
$(".errMsg ul").remove();
var myObject = eval('(' + response.d + ')');
if (myObject > 0) {
bindData();
$(".errMsg").append("<ul><li>Data saved successfully</li></ul>");
}
else {
$(".errMsg").append("<ul><li>Opppps something went wrong.</li></ul>");
}
$(".errMsg").show("slow");
clear();
},
error: function (response) {
alert(response.status + ' ' + response.statusText);
}
});
}
});
$("#btnSave").click(function () {
$("#form1").submit()
});
});
}
I am calling this function in a button click. Please help me with this.
my design page of button fields is
dataTypefrom any one (xml, json, script, or html)$("some-css-selector")validateinstead of$("some-css-selector").validate?.validate()would be recognized. And 2).validate()is the plugin's initialization method and therefore would not belong inside a function,saveData, that presumably gets triggered every time the submit button is clicked. Just put.validate()in your DOM ready event handler.