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

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

你可能感兴趣的:(修改jquery.validate.js的验证规则)