修改jquery.validate.js的验证规则

本文介绍如何将jQuery Validation Plugin的验证方式从name属性改为id属性,并提供了具体修改方法及示例代码。

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

jquery.validate.js默认验证的是元素的name.

例如:<input name="username" id="username" size="25" />

------------------------------------------------------------------------------------

我们可以通过修改jquery.validate.js让他验证元素的id.

我们把jquery.validate.js中的element.name,全部都替换成element.id就可以了,注意要完全匹配element.name别把其他的也替换了产生错误。

------------------------------------------------------------------------------------

以下方法中也要替换:

onclick: function( element, event ) {

// click on selects, radiobuttons and checkboxes

if ( element.id in this.submitted ) {

this.element(element);

}

// or option elements, check parent select in that case

else if ( element.parentNode.name in this.submitted ) {

this.element(element.parentNode);

}

},

findLastActive: function() {

var lastActive = this.lastActive;

return lastActive && $.grep(this.errorList, function( n ) {

return n.element.id === lastActive.name;

}).length === 1 && lastActive;

},

findByName: function( name ) {

return $(this.currentForm).find("[name='" + name + "']");

},

elements: function() {

var validator = this,

rulesCache = {};


// select all valid inputs inside the form (no submit or reset buttons)

return $(this.currentForm)

.find("input, select, textarea")

.not(":submit, :reset, :image, [disabled]")

.not( this.settings.ignore )

.filter(function() {

if ( !this.name && validator.settings.debug && window.console ) {

console.error( "%o has no name assigned", this);

}


// select only the first element for each name, and only those with rules specified

if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {

return false;

}


rulesCache[this.id] = true;

return true;

});

},

修改后

onclick: function( element, event ) {

// click on selects, radiobuttons and checkboxes

if ( element.id in this.submitted ) {

this.element(element);

}

// or option elements, check parent select in that case

else if ( element.parentNode.id in this.submitted ) {

this.element(element.parentNode);

}

},

findByName: function( name ) {

return $(this.currentForm).find("[id='" + name + "']");

},

findLastActive: function() {

var lastActive = this.lastActive;

return lastActive && $.grep(this.errorList, function( n ) {

return n.element.id === lastActive.id;

}).length === 1 && lastActive;

},

elements: function() {

var validator = this,

rulesCache = {};


// select all valid inputs inside the form (no submit or reset buttons)

return $(this.currentForm)

.find("input, select, textarea")

.not(":submit, :reset, :image, [disabled]")

.not( this.settings.ignore )

.filter(function() {

if ( !this.id && validator.settings.debug && window.console ) {

console.error( "%o has no name assigned", this);

}


// select only the first element for each name, and only those with rules specified

if ( this.id in rulesCache || !validator.objectLength($(this).rules()) ) {

return false;

}


rulesCache[this.id] = true;

return true;

});

},

------------------------------------------------------------------------------------

插件地址:

http://jqueryvalidation.org/

以上使用的是jQuery Validation Plugin 1.11.1

转载于:https://my.oschina.net/bigyuan/blog/272320

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值