jQuery validate 自定義樣式、規則

tiantangqiu發表於2014-09-12
jquery validate 常用的找Api 就好, 但有一些需要自己擴充套件,比如規則、樣式:

1. 自定義規則:

jQuery.validator.addMethod("test", function(value, element) {
return this.optional(element) || 1==1);
}, "錯誤提示");

$('#submitForm').validate({
rules: {
testAttr: {
required:true,
stringCheck:true,
test:[]
}
},
message:{
required:"必填",
test:"測試"
}
});


2.自定義樣式:
模擬easy ui 自定義樣式
[img]http://dl2.iteye.com/upload/attachment/0101/0842/5ddd3dcd-27c1-351b-884a-0b34aee08305.png[/img]


input.error {
background: url("images/validatebox_warning.png") no-repeat scroll right 1px #FFFFEE;
border: 1px solid red;
}

重寫錯誤提示 js:
$.validator.setDefaults({
showErrors: function(map, list) {
// there's probably a way to simplify this
/*var focussed = document.activeElement;
if (focussed && $(focussed).is("input, textarea")) {
$(this.currentForm).tooltip("close", { currentTarget: focussed }, true);
}*/
this.currentElements.removeAttr("title").removeClass("ui-state-highlight").removeClass("error");

$.each(list, function(index, error) {
$(error.element)
.attr("title", error.message).addClass("ui-state-highlight").addClass("error");
$(error.element).tooltip({
openn:true,
position: {
my: "left top",
at: "right+5 top-5"
}
});
//$(error.element).after('<div class="validatebox-tip" style="display:block;left:164px;top:165px;"><span class="validatebox-tip-content" >test<span></div>');
});
/*if (focussed && $(focussed).is("input, textarea")) {
$(this.currentForm).tooltip("open", { target: focussed });
}*/
}
});


相關文章