--------------asynchronously determines whether the data exists----------------------------
$.validator.addmethod ("Isinfoexist", function (value, Element) {
var id= $ ("#id"). Val ();
var isexist = false;
$.ajax ({
Url:web_root + '/nihao/nihao.do ',
Data: {
' Ren.name ': Name
},
Async:false,
DataType: ' JSON ',
Success:function (response) {
if (response)
Isexist = response.isexist;//Backstagereturns a Boolean variable
}
});
return isexist;
}, "already exists!");
----------------------Add a new judging method -----------------------------
$.validator.addmethod ("Isexist", function (value, Element) {
var name = $ ("#name"). Val ();
var isexist = false;
if (name== "") {
Isexist=false;
}else{isexist = true;};
return isexist;
}, "quote the error message here!" ");
Form submission via Ajax---------------Validator----------------------
var options = {
url:$ ("#form"). attr (' action '),
Type: ' Post ',
DataType: ' JSON ',
Success:function (data) {
$ (document.body). Popmsg ({
Msg: "added successfully! ",//Pop-up tip information
Confirm:function () {
Window.location.href = Web_root + "/Nihao/nihao.jsp ";//Jump JSP page
},
Close:function () {
Window.location.href = Web_root + "nihao.jsp";
}
});
},
Error:function (data) {
$ (document.body). Popmsg ({
Msg: "Add failed! ",
Confirm:function () {
}
});
}
};
var validator = $ ("#form"). Validate ({ //Here is the form ID of the binding form
------------------------Implement the form Ajax submission--------------------------------------------
Submithandler:function (form) {
$ (form). Ajaxsubmit (options);
},
------------------------------------------------------------------------------------
' Rules ': { //define validation rules
' Ren.id ': { //control JSP in Entity name
Required:true, //validator has a lot of self-brought properties required represents non-empty
Isinfoexist:true //Custom method
},
' Ren.name ': {
Required:true,
Isexist:true
},
},
--------------------Information------------------------------
Messages: {
' ren.id ': { //control JSP entity name
required: "Please fill in id",
},
' ren.name ': {
required: "Please fill in name",
},
},
----------------------Tip information seat------------------------------
errorplacement:function (Error, Element) {
if (element.is (": Radio "))
error.appendto (Element.parent ());
else if (element.is (": CheckBox"))
error.appendto (Element.parent ());
else if (element.is ("Input[name=captcha)")
error.appendto (Element.parent ());
else
Error.insertafter (Element);
},
After----------------------successful, empty the prompt to append the right style (you can add a picture)------------------------------
Success:function (label) {
Label.html (" "). AddClass ("right");
},
Error:function () {
}
});
Validator Inspection (code)