表单验证jq.validate.js
源代码--demo
Validate:function(){
var me=this;
var $form = $('#form');
//添加自定义方法: 同时验证手机和座机电话
jQuery.validator.addMethod('availblePhone',function(value,element){
var regx = /(^1\d{10}$|^(0\d{2,3}-?|0\d{2,3})?[1-9]\d{4,7}(-\d{1,8})?$)/;
if(value){
return this.optional(element) || regx.test(value);
}else{
return true;
}
},$.validator.format("请输入正确的联系人电话"));
//添加自定义方法
jQuery.validator.addMethod('availbleMoney',function(value,element){
if(parseInt(me.availableMoney) > 20 && parseInt(me.availableMoney) < 50000){
return this.optional(element) || parseInt(value) <= parseInt(me.availableMoney);
}else{
return true; //这里必须返回true
}
},$.validator.format("提现金额不能大于当前账户可用余额"));
$form.validate({
messages: {
alipay_number: {
required:'请输入支付宝账号'
},
alipay_name: {
required:'请输入支付宝账号姓名'
},
money: {
required:'请输入提现金额',
regex:'请输入20元-5万元的整数金额',
max:'请输入不大于50000(五万)元的整数金额',
min: '请输入不小于20元的整数金额',
}
},
rules: {
alipay_number:{
required: true
},
alipay_name:{
required:true
},
money:{
required:true,
regex:'^[0-9]*[1-9][0-9]*$',
min:20,
max:50000,
availbleMoney:true
}
},
errorPlacement: function(error, element) {
error.appendTo(element.next('.warn-error')); //错误提示
}
});
},
1.表单数据的获取var postData = $('#form').serializeArray();
2.密码加密处理:postData.user_pwd= new Base64().encode(postData.user_pwd).split('').reverse().join('');
3.单独验证某个元素:$(ele).valid();
例如:
var $Input = $('#form').find('input[name=telphone]');
$Input.valid();
4. 表单提交数据:
$.ajax({
url: '/user/pass/register',
type: 'POST',
data: postData,
before:function(){
//$target.addClass('loading').html('注册中');
},
complete: function () {
$target.removeClass('loading').html('立即注册');
},
success: function (obj) {
}
})
demo2:
$('.login-form-personal').validate({
messages: {
'user_name': {
required:'请输入手机号',
regex:'请输入正确的手机号'
},
'user_pwd': {
required:'请输入密码',
minlength: '密码需满足6-20位数字、字母或下划线的组合',
maxlength: '密码需满足6-20位数字、字母或下划线的组合',
regex:'密码必须6-20位非纯数字,字母、数字、下划线的组合'
}
},
rules: {
user_name: {
regex: /^1[34578]\d{9}$/
},
user_pwd: {
minlength: 6,
maxlength: 20,
regex: /^(?![0-9]*$)[a-zA-Z0-9_]{6,20}$/
}
},
errorPlacement: function(error, element) {
error.appendTo(element.parents('.login-form-item'));
}
});