validate ajax 自定义

本文介绍了如何使用jQuery和AJAX实现远程验证用户名的合法性及是否已存在于系统中,确保用户输入的数据符合预设规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$.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. });

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值