validate ajax 自定义

$.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"); 
            } 
        });                      
    } 
}); 
 
  1. $("#myform").validate({
  2.   rules: {
  3.     email: {
  4.       required: true,
  5.       email: true,
  6.       remote: {
  7.         url: "check-email.php",
  8.         type: "post",
  9.         data: {
  10.           username: function() {
  11.             return $("#username").val();
  12.           }
  13.         }
  14.       }
  15.     }
  16.   }
  17. });

 

 

你可能感兴趣的:(jquery,Ajax,json,PHP,cache)