//JS各种校验的使用方法
<textarea check="{maxlength:512}" id="description" name="description" ></textarea>
$("#form1").FValidate({errorModel:'qtip'});
if ($("#form1").valid()) {
$I("form1").FForm('submit');
}
//需引入下面的JS
(function($) {
$.extend({
metadata: {
defaults: {
type: 'class',
name: 'metadata',
cre: /({.*})/,
single: 'metadata'
},
setType: function(type, name) {
this.defaults.type = type;
this.defaults.name = name;
},
get: function(elem, opts) {
var settings = $.extend({}, this.defaults, opts);
// check for empty string in single property
if (!settings.single.length)
settings.single = 'metadata';
var data = $.data(elem, settings.single);
// returned cached data if it already exists
if (data)
return data;
data = "{}";
if (settings.type == "class") {
var m = settings.cre.exec($(elem).attr('check'));
if (m)
data = m[1];
}
else if (settings.type == "elem") {
if (!elem.getElementsByTagName)
return undefined;
var e = elem.getElementsByTagName(settings.name);
if (e.length)
data = $.trim(e[0].innerHTML);
}
else if (elem.getAttribute != undefined) {
var attr = elem.getAttribute(settings.name);
if (attr)
data = attr;
}
if (data.indexOf('{') < 0)
data = "{" + data + "}";
data = eval("(" + data + ")");
$.data(elem, settings.single, data);
return data;
}
}
});
$.fn.metadata = function(opts) {
return $.metadata.get(this[0], opts);
};
})(jQuery);
/**
* 基于jquery validate进行扩展.
* 使用方法:
* 1、在输入框中配置check属性定义校验规则.
* 2、在提交表单前调用如下方法.
* $("#formid").FValidate({errorModel:'qtip'});
* if ($("#formid").valid()) {
* //校验通过.
* }
*/
(function($) {
$.extend($.validator.messages, {
required: "必填字段",
remote: "请修正该字段",
email: "请输入正确格式的电子邮件",
url: "请输入合法的网址",
date: "请输入合法的日期",
dateISO: "请输入合法的日期 (ISO).",
number: "请输入合法的数字",
digits: "只能输入整数",
creditcard: "请输入合法的信用卡号",
equalTo: "请再次输入相同的值",
accept: "请输入拥有合法后缀名的字符串",
maxlength: $.validator.format("请输入一个长度最多是 {0} 的字符串"),
minlength: $.validator.format("请输入一个长度最少是 {0} 的字符串"),
rangelength: $.validator.format("请输入一个长度介于 {0} 和 {1} 之间的字符串"),
range: $.validator.format("请输入一个介于 {0} 和 {1} 之间的值"),
max: $.validator.format("请输入一个最大为 {0} 的值"),
min: $.validator.format("请输入一个最小为 {0} 的值")
});
$.extend($.validator, {
classRules: function(element) {
var rules = {
};
if
($(element).attr("name"))
{
rules
= {
// isSpecialChar: true
};
}
var classes = $(element).attr('check');
classes && $.each(classes.split(' '), function() {
if (this in $.validator.classRuleSettings) {
$.extend(rules, $.validator.classRuleSettings[this]);
}
});
return rules;
}
});
}(jQuery));
//手机号码验证
jQuery.validator.addMethod("ismobile", function(value, element) {
var length = value.length;
var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
return (length == 11 && mobile.exec(value)) ? true : false;
}, "请正确填写您的手机号码");
//邮编验证
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return (tel.exec(value)) ? true : false;
}, "请正确填写您的邮编");
//特殊字符验证
jQuery.validator.addMethod("isSpecialChar", function(value, element) {
var sChar = new Array('&', '/', '<', '>', '"', '//');
for (i = 0; i < sChar.length; i++) {
if (value.indexOf(sChar[i]) >= 0) {
return false;
}
}
return true;
}, "内容含有特殊字符");
基于jquery-validate实现的校验功能
最新推荐文章于 2022-03-17 17:46:07 发布