$.validator.addMethod("checkName", function(value, element) { var result; var userId = document.getElementById("user.id").value; $.ajax({ url : "/user/checkName.do?userId="+userId+"&name="+document.getElementById("name").value, //data : "user.userId=" + userId, dataType : "json", type : "post", async : false, cache : false, timeout : 10000, success : function(response) { result = response.IsSuccess; } }); return this.optional(element) || result; }, "用户名不合法或已经存在!");
remote的validator方式:
$('form#changePasswordForm').validate({ rules: { repeat_new_password: { equalTo: "#new_password" }, password : { // This will invoke ./json/authenticate.do?password=THEVALUE_OF_THE_FIELD // and all you need to do is return "true" or "false" from this server script remote: './json/authenticate.do' } }, messages: { password: { remote: jQuery.format("Wrong password") } }, submitHandler: function(form) { $(form).ajaxSubmit({ dataType: "json", success: function(json) { alert("foo"); } }); } });
- $("#myform").validate({
- rules: {
- email: {
- required: true,
- email: true,
- remote: {
- url: "check-email.php",
- type: "post",
- data: {
- username: function() {
- return $("#username").val();
- }
- }
- }
- }
- }
- });