framework.js Jquery插件
http://common.cnblogs.com/script/jquery.js
http://common.cnblogs.com/script/framework.js
//创建命名空间 $.cnblogs = {}; //总基类 $.cnblogs.Extensible = function(options) { options = options || {}; this.initialize(options); }; //基类方法 $.extend($.cnblogs.Extensible, { //initialize方法用来代替构造函数 initialize: function(options) { $.extend(this, options); } }); //extend方法 //说明: // 生成当前类的子类 //参数: // overrides: 用来重写基类的内容 //附加: // 生成的子类同样有extend方法可再生成子类 // 生成的子类有baseClass成员表示其父类 // 子类的initialize方法最好先调用父类的initialize方法,调用方法如下 // 子类名.baseClass.initialize.call(this, options); //示例: // var MyClass = Extensible.extend({ // initialize: function(options) { // MyClass.baseClass.initialize.call(this, options); // alert(options.msg); // } // }); $.cnblogs.Extensible.extend = function(overrides) { var superClass = this; var subClass = function(options) { options = options || {}; $.extend(this, subClass); this.initialize(options); }; $.extend(subClass, superClass); $.extend(subClass, overrides); var p = function() { }; p.prototype = superClass.prototype; subClass.prototype = new p(); subClass.prototype.constructor = subClass; subClass.baseClass = superClass; return subClass; }; //Control类 $.cnblogs.Control = $.cnblogs.Extensible.extend({ //getContentObj方法 //说明: // 生成并返回此控件html内容的jQuery对象 //返回: // 此控件html内容的jQuery对象 getContentObj: function() { }, //cacheResult成员 //说明: // 如果此值为true,则多次show方法的调用只会执行一次getContentObj方法 // 如果为false则每次show都会调用getContentObj方法 // 默认为true cacheResult: false, initialize: function(options) { this.container = options.container; $.cnblogs.Control.baseClass.initialize.call(this, options); }, //render方法 //说明: // 将此Control对象的内容显示在container中 //参数: // container: 显示此Control对象的容器 //返回: // 无返回值 //示例: // var c = new $.cnblogs.Control(); // //c.show('#myDiv'); //可以使用ID直接传 // c.show($('#myDiv')); //推荐使用jQuery对象 render: function() { var obj; if (!this.cacheResult || !this.contentObj) { obj = this.getContentObj(); if (this.cacheResult) { this.contentObj = obj; } } else { obj = this.contentObj; } if (typeof this.container == 'string') { this.container = $('#' + this.container); } $(this.container).html('').append(obj); } }); //BindingSource类 $.cnblogs.BindingSource = $.cnblogs.Extensible.extend({ //clearHtml成员 //说明: // 为true时在绑定前清除被绑定对象的innerHTMl clearHtml: true, //getDataObj方法 //说明: // 获取每一个绑定数据经处理后的jQuery对象 //参数: // data: 待绑定的单条数据 // index: data在所有数据中的索引 //返回: // jQuery对象 //示例: // getDataObj : function(data, index) { // return '#' + index.toString() + ': ' + data.toString(); // } getDataObj: function(data, index) { return data; }, //bindTo方法 //说明: // 将内容绑定到指定的html对象 //参数: // options: 绑定数据的参数,其中可包括一个或多个待绑定的对象 //返回: // 无返回值 bindTo: function(options) { } }); $.cnblogs.AjaxBindingSource = $.cnblogs.BindingSource.extend({ initialize: function(options) { $.cnblogs.AjaxBindingSource.baseClass.initialize.call(this, options); //调用基类 this.url = options.url; this.method = options.method || 'post'; this.contentType = options.contentType || 'application/json; charset=utf-8'; this.data = options.data || {}; this.isAsmx = (options.isAsmx == null) ? true : options.isAsmx; this.wrapper = options.wrapper; this.callback = options.callback || { scope: this, handler: function(data) { } }; this.isPending = false; }, prepareData: function(options) { var data = this.data; if (data) { if (this.isAsmx) { //如果是调用ASP.NET Web Service,需要将data格式化为json串 data = JSON.stringify(data); } return data; } else { return null; } }, bindTo: function(options) { //如果已经有AJAX请求则不再重复请求 if (!this.isPending) { var target; target = options.target; if (typeof target == 'string') { target = $('#' + options.target); } var self = this; //保持this引用 //根据要求清除target的innerHTML值 if (this.clearHtml) { target.html(''); } var data = this.prepareData(options); this.isPending = true; $.ajax({ url: this.url || options.url, type: this.method, data: data, contentType: this.contentType, dataType: 'json', cache: false, success: function(data, status) { self.isPending = false; self.callback.handler.call(self.callback.scope, data); if (self.wrapper) { data = data[self.wrapper]; } if (data.length) { if (data.length > 1) { for (var i = 0; i < data.length; i++) { var obj = self.getDataObj(data[i], i); target.append(obj); } } else if (data.length == 1) { var obj = self.getDataObj(data[0], 0); target.append(obj); } } else { var obj = self.getDataObj(data[0], 0); target.append(obj); } }, error: function(xhr, status, ex) { self.isPending = false; target.append(xhr.responseText); } }); } } }); $.cnblogs.PagedAjaxBindingSource = $.cnblogs.AjaxBindingSource.extend({ prepareData: function(options) { var data = this.data || {}; var pageIndex = options.pageIndex || 1; var pageSize = options.pageSize || 20; data['pageIndex'] = pageIndex; data['pageSize'] = pageSize; if (this.isAsmx) { //如果是调用ASP.NET Web Service,需要将data格式化为json串 data = JSON.stringify(data); } return data; } }); $.cnblogs.SimpleBindingSource = $.cnblogs.BindingSource.extend({ initialize: function(options) { $.cnblogs.SimpleBindingSource.baseClass.initialize.call(this, options); //调用基类 if (options.data) { this.data = options.data; } else { //初始化绑定数据 //在子类中使用this.data.push方法添加数据 this.data = new Array(); this.initializeData(this.data); } }, initializeData: function(data) { }, bindTo: function(options) { var target = $('#' + options.target); //获取被绑定对象 var self = this; //保持对this对象的引用 //根据要求清除target的innerHTML值 if (this.clearHtml) { target.html(''); } $.each(this.data, function(index) { var obj = self.getDataObj(this, index); //this为循环中的元素值 target.append(obj); //将内容追加到被绑定对象的innerHTML }); } }); $.cnblogs.Dialog = $.cnblogs.Extensible.extend({ //getContentObj方法 //说明: // 生成并返回此控件html内容的jQuery对象 //返回: // 此控件html内容的jQuery对象 getContentObj: function() { }, //cacheResult成员 //说明: // 如果此值为true,则多次show方法的调用只会执行一次getContentObj方法 // 如果为false则每次show都会调用getContentObj方法 // 默认为true cacheResult: true, isDialogWrapped: false, show: function() { if (!this.cacheResult || !this.contentObj) { this.contentObj = this.getContentObj(); } if (this.isDialogWrapped) { this.contentObj.dialog('open'); } else { this.isDialogWrapped = true; this.contentObj.dialog(this.dialogOptions); } }, close: function(closing) { if (this.cacheResult) { if (!closing) { this.contentObj.dialog('close'); } } else { this.contentObj.dialog('destroy').remove(); this.isDialogWrapped = false; this.contentObj = null; } }, initialize: function(options) { var self = this; //保持this引用 this.dialogOptions = options.dialogOptions || {}; if (options.cacheResult != null) { this.cacheResult = options.cacheResult; } $.extend(this.dialogOptions, { bgiframe: true, close: function() { self.close(true); } }); } }); $.cnblogs.GenericDialog = $.cnblogs.Dialog.extend({ initialize: function(options) { $.cnblogs.GenericDialog.baseClass.initialize.call(this, options); this.getContentObj = options.getContentObj || function() { }; } }); //LocationList类 $.cnblogs.LocationList = $.cnblogs.BindingSource.extend({ initialize: function(options) { $.cnblogs.LocationList.baseClass.initialize.call(this, options); this.data = [{ name: "北京", cities: ["西城", "东城", "崇文", "宣武", "朝阳", "海淀", "丰台", "石景山", "门头沟", "房山", "通州", "顺义", "大兴", "昌平", "平谷", "怀柔", "密云", "延庆"] }, { name: "天津", cities: ["青羊", "河东", "河西", "南开", "河北", "红桥", "塘沽", "汉沽", "大港", "东丽", "西青", "北辰", "津南", "武清", "宝坻", "静海", "宁河", "蓟县", "开发区"] }, { name: "河北", cities: ["石家庄", "秦皇岛", "廊坊", "保定", "邯郸", "唐山", "邢台", "衡水", "张家口", "承德", "沧州", "衡水"] }, { name: "山西", cities: ["太原", "大同", "长治", "晋中", "阳泉", "朔州", "运城", "临汾"] }, { name: "内蒙古", cities: ["呼和浩特", "赤峰", "通辽", "锡林郭勒", "兴安"] }, { name: "辽宁", cities: ["大连", "沈阳", "鞍山", "抚顺", "营口", "锦州", "丹东", "朝阳", "辽阳", "阜新", "铁岭", "盘锦", "本溪", "葫芦岛"] }, { name: "吉林", cities: ["长春", "吉林", "四平", "辽源", "通化", "延吉", "白城", "辽源", "松原", "临江", "珲春"] }, { name: "黑龙江", cities: ["哈尔滨", "齐齐哈尔", "大庆", "牡丹江", "鹤岗", "佳木斯", "绥化"] }, { name: "上海", cities: ["浦东", "杨浦", "徐汇", "静安", "卢湾", "黄浦", "普陀", "闸北", "虹口", "长宁", "宝山", "闵行", "嘉定", "金山", "松江", "青浦", "崇明", "奉贤", "南汇"] }, { name: "江苏", cities: ["南京", "苏州", "无锡", "常州", "扬州", "徐州", "南通", "镇江", "泰州", "淮安", "连云港", "宿迁", "盐城", "淮阴", "沐阳", "张家港"] }, { name: "浙江", cities: ["杭州", "金华", "宁波", "温州", "嘉兴", "绍兴", "丽水", "湖州", "台州", "舟山", "衢州"] }, { name: "安徽", cities: ["合肥", "马鞍山", "蚌埠", "黄山", "芜湖", "淮南", "铜陵", "阜阳", "宣城", "安庆"] }, { name: "福建", cities: ["福州", "厦门", "泉州", "漳州", "南平", "龙岩", "莆田", "三明", "宁德"] }, { name: "江西", cities: ["南昌", "景德镇", "上饶", "萍乡", "九江", "吉安", "宜春", "鹰潭", "新余", "赣州"] }, { name: "山东", cities: ["青岛", "济南", "淄博", "烟台", "泰安", "临沂", "日照", "德州", "威海", "东营", "荷泽", "济宁", "潍坊", "枣庄", "聊城"] }, { name: "河南", cities: ["郑州", "洛阳", "开封", "平顶山", "濮阳", "安阳", "许昌", "南阳", "信阳", "周口", "新乡", "焦作", "三门峡", "商丘"] }, { name: "湖北", cities: ["武汉", "襄樊", "孝感", "十堰", "荆州", "黄石", "宜昌", "黄冈", "恩施", "鄂州", "江汉", "随枣", "荆沙", "咸宁"] }, { name: "湖南", cities: ["长沙", "湘潭", "岳阳", "株洲", "怀化", "永州", "益阳", "张家界", "常德", "衡阳", "湘西", "邵阳", "娄底", "郴州"] }, { name: "广东", cities: ["广州", "深圳", "东莞", "佛山", "珠海", "汕头", "韶关", "江门", "梅州", "揭阳", "中山", "河源", "惠州", "茂名", "湛江", "阳江", "潮州", "云浮", "汕尾", "潮阳", "肇庆", "顺德", "清远"] }, { name: "广西", cities: ["南宁", "桂林", "柳州", "梧州", "来宾", "贵港", "玉林", "贺州"] }, { name: "海南", cities: ["海口", "三亚"] }, { name: "重庆", cities: ["渝中", "大渡口", "江北", "沙坪坝", "九龙坡", "南岸", "北碚", "万盛", "双桥", "渝北", "巴南", "万州", "涪陵", "黔江", "长寿"] }, { name: "四川", cities: ["成都", "达州", "南充", "乐山", "绵阳", "德阳", "内江", "遂宁", "宜宾", "巴中", "自贡", "康定", "攀枝花"] }, { name: "贵州", cities: ["贵阳", "遵义", "安顺", "黔西南", "都匀"] }, { name: "云南", cities: ["昆明", "丽江", "昭通", "玉溪", "临沧", "文山", "红河", "楚雄", "大理"] }, { name: "西藏", cities: ["拉萨", "林芝", "日喀则", "昌都"] }, { name: "陕西", cities: ["西安", "咸阳", "延安", "汉中", "榆林", "商南", "略阳", "宜君", "麟游", "白河"] }, { name: "甘肃", cities: ["兰州", "金昌", "天水", "武威", "张掖", "平凉", "酒泉"] }, { name: "青海", cities: ["黄南", "海南", "西宁", "海东", "海西", "海北", "果洛", "玉树"] }, { name: "宁夏", cities: ["银川", "吴忠"] }, { name: "新疆", cities: ["乌鲁木齐", "哈密", "喀什", "巴音郭楞", "昌吉", "伊犁", "阿勒泰", "克拉玛依", "博尔塔拉"]}] }, getOptionHtml: function(value) { return '<option value="' + value + '">' + value + '</option>'; }, getProvinceHtml: function() { var html = ''; for (var i = 0; i < this.data.length; i++) { html += this.getOptionHtml(this.data[i].name); } return html; }, getCityHtml: function(province) { var html = '<option value="'+province+'">' + province + '</option>'; var cities = this.getCities(province); for (var i = 0; i < cities.length; i++) { html += this.getOptionHtml(cities[i]); } return html; }, getCities: function(province) { for (var i = 0; i < this.data.length; i++) { if (this.data[i].name == province) { return this.data[i].cities; } } return new Array(); }, bindTo: function(options) { options = options || {}; this.pList = $('#' + options.pList); this.cList = $('#' + options.cList); this.pList.html('') .append($(this.getOptionHtml(options.defaultProvince))) .append($(this.getProvinceHtml())); var self = this; this.pList.bind('change', function(event) { var province = this.value; var cityObj = $(self.getCityHtml(province)); self.cList.html('').append(cityObj); }); this.select(options.selected); }, select: function(value) { if (value && value.province) { this.pList.attr('value', value.province).change(); this.cList.attr('value', value.city); } } }); //debugger; $.create = function(options, parent) { if (!options || !options.tag) { return; } var element; if (options.tag.toLowerCase() == 'input') { element = '<input type="' + options.type + '" />'; } else { element = '<' + options.tag + ' />'; } element = $(element); if (parent) { parent.append(element); } if (typeof options.attributes == 'object') { element.attr(options.attributes); } if (typeof options.style == 'object') { element.css(options.style); } if (typeof options.className == 'string') { element.addClass(options.className); } if (typeof options.text == 'string') { element.html(options.text); } if (typeof options.value == 'string') { element.val(options.value); } if (options.callback) { options.callback(element); } if (options.children) { if (options.children.length) { for (var i = 0; i < options.children.length; i++) { var child = $.create(options.children[i], element); } } else { var child = $.create(options.children); element.append(child); } } return element; }; $.cnblogs.DoubleListBindingSource = $.cnblogs.BindingSource.extend({ initialize: function(options) { $.cnblogs.DoubleListBindingSource.baseClass.initialize.call(this, options); this.data = options.data || new Array(); }, bindTo: function(options) { var self = this; //保持this引用 var parent = options.parent; var child = options.child; if (typeof parent == 'string') { parent = $('#' + parent); } if (typeof child == 'string') { child = $('#' + child); } var defaultItem = options.defaultItem; //填充父列表 if (defaultItem) { parent.append( $.create({ tag: 'option', value: defaultItem.value, text: defaultItem.text }) ); child.append( $.create({ tag: 'option', value: defaultItem.value, text: defaultItem.text }) ); } $.each(this.data, function() { parent.append( $.create({ tag: 'option', value: this.value, text: this.text }) ); }); //绑定父列表事件 parent.change(function() { //值有效,绑定子列表 child.html(''); if (defaultItem) { child.append( $.create({ tag: 'option', value: defaultItem.value, text: defaultItem.text }) ); } var value = parent.val(); if (!defaultItem || value != defaultItem.value) { var subArr = $.grep(self.data, function(item) { return (item.value == value); }); if (subArr.length > 0) { var items = subArr[0].items; $.each(items, function() { child.append( $.create({ tag: 'option', value: this.value, text: this.text }) ); }); } } }); } });
http://common.cnblogs.com/script/val.js
$.cnblogs.validation = {}; //创建命名空间 //validator管理器 //用于注册validator的rule或通过rule获取validator对象 $.cnblogs.validation.ValidatorMgr = { registerRule: function(rule, type) { this[rule] = type; }, createValidator: function(rule, options) { options = options || {}; if (this[rule]) { return new this[rule](options); } return null; } }; //ValidateResult类 //说明: // 表示验证结果 $.cnblogs.validation.ValidateResult = function() { this.result = false; this.msg = ''; this.modifier = null; this.onModified = function(result) { //result is of type jQuery.ValidateResult }; this.modify = function(result, msg, modifier) { this.result = result; this.msg = msg; this.modifier = modifier; if (jQuery.isFunction(this.onModified)) { this.onModified(this); } }; }; //validator基类 //也可通过重写doValidate方法执作自定义validator使用 $.cnblogs.validation.CustomValidator = $.cnblogs.Extensible.extend({ //验证未通过时显示的消息 errorMsg: '', //下一个validator successor: null, //可验证的input类型 acceptTypes: ['text', 'password', 'checkbox', 'radio', 'select'], initialize: function(options) { $.extend(this, options); }, //可重写的验证方法 doValidate: function(input, result) { this.setError(result); }, //原则上不重写,模板方法 validate: function(input, result) { //判断input的type是否在acceptTypes中,存在则调用验证方法 //对于select之类的要判断tagName if ($.inArray($(input).prop('type').toLowerCase(), this.acceptTypes) != -1 || $.inArray($(input).prop('tagName').toLowerCase(), this.acceptTypes) != -1) { //验证 this.doValidate(input, result); } //如果验证通过,并且还有下一个validator,交给下一个validator if (result.result && this.successor) { this.successor.validate(input, result); } }, //通知验证错误 setError: function(result) { result.modify(false, this.errorMsg, this); } }); $.cnblogs.validation.ValidatorMgr.registerRule('custom', jQuery.CustomValidator); //注册规则custom //FormValidator类 $.cnblogs.validation.FormValidator = function(form, options) { jQuery.extend(this, { validators: new Array(), addValidator: function(validator) { this.validators.push(validator); }, success: function() { }, failure: function(errors) { }, validate: function() { var errors = new Array(); var result = true; var formValidator = $(this).data('validator'); //使用所有的validator进行检验 $.each(formValidator.validators, function() { if (!this.isValid()) { result = false; if (this.result.msg) { errors.push(this.result.msg); } } }); //触发事件 if (result && formValidator.success) { formValidator.success(); } else if (formValidator.failure) { formValidator.failure(errors); } return result; } }); options = options || {}; $.extend(this, options); //Add event to form $(form).bind('submit', this.validate); }; //formValidator方法 //说明: // 为form元素绑定相应的验证事件 //参数: // options: 生成$.cnblogs.validation.FormValidator的参数 //返回: // 可链式调用的jQuery对象 $.fn.formValidator = function(options) { return this.each(function() { var self = $(this); if (self.prop('tagName').toLowerCase() == 'form') { var validator = self.data('validator'); if (validator) { $.extend(validator, options); } else { self.data('validator', new $.cnblogs.validation.FormValidator(self, options)); } } }); }; $.fn.performValidation = function(group, onSuccess, onFailure) { this.doValidate = function(validators) { var errors = new Array(); var result = true; $.each(validators, function() { if (!this.isValid()) { result = false; if (this.result.msg) { errors.push(this.result.msg); } } }); //触发事件 if (result && onSuccess) { onSuccess(); } else if (onFailure) { onFailure(errors); } return result; }; var self = $(this); if (self.prop('tagName').toLowerCase() == 'form') { var validators = self.data('validator').validators; if (validators && validators.length > 0) { if (group && typeof group == 'string' && group.length > 0) { //对此组进行验证 //获取所有group中的validator validators = $.grep(validators, function(validator) { return (validator.group == group); }); } //一一验证 return this.doValidate(validators); } } //默认返回true return true; }; $.cnblogs.validation.InputValidator = $.cnblogs.Extensible.extend({ //加载时显示的消息 readyMsg: '', //获得焦点后显示的消息 focusMsg: '', //验证通过后显示的消息 validMsg: '', //控件加载时消息的css readyClass: 'validation-ready', //验证未通过时消息的css errorClass: 'validation-error', //验证通过时消息的css focusClass: 'validation-focus', //获得焦点后消息的css validClass: 'validation-valid', //验证组 group: null, //显示消息的元素的id msgTarget: '', //待验证的input input: null, //第一个validator,用以形成责任链 head: null, //最后一个validator,用以形成责任链 tail: null, //添加一个validator,标准的链表操作形式 addValidator: function(validator) { if (validator) { if (this.head) { this.tail.successor = validator; this.tail = validator; } else { this.head = validator; this.tail = validator; } } }, //验证是否通过 isValid: function() { if (!this.result.result) { this.validate(); } return this.result.result; }, onError: function(input, msgTarget, cssClass, msg) { }, onValid: function(input, msgTarget, cssClass, msg) { }, onReady: function(input, msgTarget, cssClass, msg) { }, onFocus: function(input, msgTarget, cssClass, msg) { }, //验证input validate: function() { //先设验证通过 this.result.modify(true, this.validMsg, this); if (this.head) { this.head.validate(this.input, this.result); } }, initialize: function(options) { options = options || {}; $.extend(this, options); if (this.msgTarget && this.msgTarget != '') { this.msgTarget = $('#' + this.msgTarget); } //如果没有待验证的input元素则返回 if (!this.input) { return; } //保持this指针的引用 var self = this; //初始化ValidateResult this.result = new $.cnblogs.validation.ValidateResult(); this.result.onModified = function(result) { //根据验证结果显示不同的内容 if (result.result) { self.onValid(self.input, self.msgTarget, self.validClass, self.validMsg); } else { self.onError(self.input, self.msgTarget, self.errorClass, result.msg); } } //注册事件 //这样注册会导致ajax验证的时候连续触发change和blur事件,需要2次请求服务器,很麻烦 $(this.input).bind('change', function() { self.validate(); }).bind('focus', function() { if (!self.isValid()) { self.onFocus(self.input, self.msgTarget, self.focusClass, self.focusMsg); } }).bind('blur', function() { if (self.isValid()) { self.onValid(self.input, self.msgTarget, self.validClass, self.validMsg); } else { self.onError(self.input, self.msgTarget, self.errorClass, self.result.msg); } }); this.onReady(this.input, this.msgTarget, this.readyClass, this.readyMsg); } }); //默认的消息显示方案 $.extend($.cnblogs.validation.InputValidator, { //显示消息 showMsg: function(css, msg) { this.getTargetEl().removeClass().addClass(css).html(msg); }, //获取显示消息用的元素 getTargetEl: function() { if (!this.msgTarget || this.msgTarget == '') { return $('#' + $(this.input).attr('id') + 'Tip'); } else { return this.msgTarget; } }, onError: function(input, msgTarget, cssClass, msg) { this.showMsg(cssClass, msg); }, onValid: function(input, msgTarget, cssClass, msg) { this.showMsg(cssClass, msg); }, onReady: function(input, msgTarget, cssClass, msg) { this.showMsg(cssClass, msg); }, onFocus: function(input, msgTarget, cssClass, msg) { this.showMsg(cssClass, msg); } }); jQuery.fn.initValidator = function(options) { return this.each(function() { var form = $(this.form); var formValidator = form.data('validator'); if (!formValidator) { //初始化一个空的FormValidator form.formValidator({}); } options['input'] = this; var validator = new $.cnblogs.validation.InputValidator(options); formValidator.addValidator(validator); $(this).data('validator', validator); }); }; jQuery.fn.addValidator = function(rule, options) { return this.each(function() { var validator = $(this).data('validator'); if (validator) { validator.addValidator($.cnblogs.validation.ValidatorMgr.createValidator(rule, options)); } }); } //Required $.cnblogs.validation.RequiredValidator = $.cnblogs.validation.CustomValidator.extend({ doValidate: function(input, result) { var value = $(input).val(); if (!value || !value.length > 0) { this.setError(result); } } }); $.cnblogs.validation.ValidatorMgr.registerRule('required', $.cnblogs.validation.RequiredValidator); //Length $.cnblogs.validation.LengthValidator = $.cnblogs.validation.CustomValidator.extend({ min: 0, max: 0, getMin: function() { return this.min || this.min >= 0 ? this.min : 0; }, getMax: function() { return this.max || this.max >= 0 ? this.max : 0; }, doValidate: function(input, result) { var value = $(input).val(); value = value.replace(/[^\x00-\xff]/g, 'xx'); if (!value || value.length < this.getMin() || value.length > this.getMax()) { this.setError(result); } } }); $.cnblogs.validation.ValidatorMgr.registerRule('length', $.cnblogs.validation.LengthValidator); //Regex $.cnblogs.validation.RegexValidator = $.cnblogs.validation.CustomValidator.extend({ acceptTypes: ['text', 'password'], regex: '', getRegex: function() { return new RegExp(this.regex || ''); }, doValidate: function(input, result) { var regex = this.getRegex(); if (!regex.test($(input).val())) { this.setError(result); } } }); $.cnblogs.validation.ValidatorMgr.registerRule('regex', $.cnblogs.validation.RegexValidator); //Value $.cnblogs.validation.ValueValidator = $.cnblogs.validation.CustomValidator.extend({ not: null, accept: null, doValidate: function(input, result) { var val = $(input).val(); if (this.accept && typeof this.accept == 'string' && val != this.accept) { this.setError(result); } else if (this.accept && typeof this.accept == 'object' && this.accept.length && this.accept.length > 0 && $.inArray(val, this.accept) == -1) { this.setError(result); } else if (this.not && typeof this.not == 'string' && val == this.not) { this.setError(result); } else if (this.not && typeof this.not == 'object' && this.not.length && this.not.length > 0 && $.inArray(val, this.not) != -1) { this.setError(result); } } }); $.cnblogs.validation.ValidatorMgr.registerRule('value', $.cnblogs.validation.ValueValidator); //Ajax $.cnblogs.validation.AjaxValidator = $.cnblogs.validation.CustomValidator.extend({ acceptTypes: ['text', 'password'], url: '', extraParams: {}, method: 'post', username: '', password: '', contentType: 'application/json; charset=utf-8', isPending: false, isAsmx: true, wrapper: 'd', doValidate: function(input, result) { if (this.isPending) { return; } var data = { value: $(input).val() }; $.extend(data, this.extraParams); if (this.isAsmx) { //如果是调用ASP.NET Web Service,需要将data格式化为json串 data = JSON.stringify(data); } var self = this; this.isPending = true; $.ajax({ url: this.url, type: this.method, contentType: this.contentType, username: this.username, password: this.password, cache: false, data: data, dataType: 'json', success: function(data, test) { self.isPending = false; if (self.wrapper) { data = data[self.wrapper]; } if (data) { if (result.modifier == self) { result.modify(true, '', this); } } else { self.setError(result); } }, error: function(xhr, text) { self.isPending = false; self.setError(result); } }); //服务器返回之前先设为false this.setError(result); } }); $.cnblogs.validation.ValidatorMgr.registerRule('ajax', $.cnblogs.validation.AjaxValidator);
调用方法
<script type="text/javascript">
$(function() {
$('#aspnetForm').formValidator();
$('#ctl00_holderLeft_txt_userName').initValidator({
readyMsg: '',
focusMsg: '至少4个字符,最多30个字符',
validMsg: '输入符合规则',
msgTarget: 'tip_userName'
}).addValidator('required', {
errorMsg: '不可为空'
}).addValidator('length', {
min: 4,
max: 30,
errorMsg: '不合要求,至少4个字符,最多30个字符'
});
$('#ctl00_holderLeft_txt_display_name').initValidator({
readyMsg: '',
focusMsg: '不能包含空格@:,不能以._结尾,2-30个字符',
validMsg: '输入符合规则',
msgTarget: 'tip_display_name'
}).addValidator('required', {
errorMsg: '不可为空'
}).addValidator('length', {
min: 2,
max: 20,
errorMsg: '至少2个字符,最多20个字符/10个中文字'
}).addValidator('regex', {
regex: '^(?!.*[@::\\s]).*[^._]$',
errorMsg: '不能包含空格@:,不能以._结尾'
});
$('#ctl00_holderLeft_txt_pwd').initValidator({
readyMsg: '',
focusMsg: '至少8位,必须包含字母、数字、特殊字符',
validMsg: '输入成功',
msgTarget: 'tip_pwd'
}).addValidator('required', {
errorMsg: '不可为空'
}).addValidator('length', {
min: 8,
max: 30,
errorMsg: '至少8个字符,最多30个字符'
}).addValidator('regex', {
regex: '(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}',
errorMsg: '必须包含字母、数字、特殊字符'
});
$('#ctl00_holderLeft_txt_confirm_pwd').initValidator({
readyMsg: '',
focusMsg: '请再输入一遍密码,要求与上面的一致',
validMsg: '输入成功',
msgTarget: 'tip_confirm_pwd'
}).addValidator('required', {
errorMsg: '不可为空'
}).addValidator('length', {
min: 8,
max: 30,
errorMsg: '8-30个字符'
});
$('#ctl00_holderLeft_txt_email').initValidator({
readyMsg: '',
focusMsg: '请输入有效的Email地址',
validMsg: '输入成功',
msgTarget: 'tip_email'
}).addValidator('required', {
errorMsg: '不可为空'
}).addValidator('regex', {
regex: '(\\w|\-)+@((\\w|\-)+\\.)+[a-z]{2,5}',
errorMsg: 'Email格式错误,请重新输入'
});
$('#CaptchaCodeTextBox').initValidator({
readyMsg: '',
focusMsg: '请输入验证码',
msgTarget: 'tip_authen_code'
}).addValidator('required', {
errorMsg: '不可为空'
});
});
function confirmpwd() {
p1 = $('#ctl00_holderLeft_txt_pwd').val();
p2 = $('#ctl00_holderLeft_txt_confirm_pwd').val();
if (p1 != p2) {
setTimeout(function() {
$('#ctl00_holderLeft_txt_confirm_pwd').val('');
$('#tip_confirm_pwd').removeClass('validation-valid');
$('#tip_confirm_pwd').addClass('validation-error');
$('#tip_confirm_pwd').html('密码与确认密码输入不一致,请重新输入');
}, 10)
}
}
function UserNameExist() {
var loginName = $('#ctl00_holderLeft_txt_userName').val();
if (loginName == "" || loginName.length < 2) {
return;
}
$('#tip_userName').append("正在检查用户名是否存在....");
$.ajax({
url: '/ws/UserService.asmx/LoginNameExist',
data: '{loginName:"' + loginName + '"}',
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf8',
cache: false,
success: function(data) {
if (data.d) {
setTimeout(function() {
//$('#ctl00_holderLeft_txt_userName').val('');
$('#tip_userName').removeClass('validation-valid');
$('#tip_userName').addClass('validation-error');
$('#tip_userName').html('<strong>"' + loginName + '</strong>"已存在,请使用其它登录用户名');
}, 10);
}
//else
//$('#tip_userName').html("登录用户名输入成功");
},
error: function(xhr) {
$('#tip_userName').html(xhr.responseText);
}
});
}
function DisplayExist() {
displayName = $('#ctl00_holderLeft_txt_display_name').val();
if (displayName == "") {
$('#tip_display_name').removeClass('validation-valid');
$('#tip_display_name').html();
return;
}
$('#tip_display_name').addClass("validation-valid");
$('#tip_display_name').append("正在检查显示名称是否存在....");
$.ajax({
url: '/ws/UserService.asmx/DisplayNameExist',
data: '{displayName:"' + displayName + '"}',
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf8',
cache: false,
success: function(data) {
if (data.d) {
setTimeout(function() {
//$('#ctl00_holderLeft_txt_display_name').val('');
$('#tip_display_name').removeClass('validation-valid');
$('#tip_display_name').addClass('validation-error');
$('#tip_display_name').html('<strong>"' + displayName + '</strong>"已存在,请使用其他显示名称');
}, 10);
}
//else
//$('#tip_display_name').html("显示名称输入成功");
},
error: function(xhr) {
$('#tip_display_name').html(xhr.responseText);
}
});
}
function EmailExist() {
email = $('#ctl00_holderLeft_txt_email').val();
if (email == "") {
$('#tip_email').removeClass('validation-valid');
$('#tip_email').html();
return;
}
$('#tip_email').addClass("validation-valid");
$('#tip_email').append("正在检查显示名称是否存在....");
$.ajax({
url: '/ws/UserService.asmx/EmailExist',
data: '{email:"' + email + '"}',
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf8',
cache: false,
success: function(data) {
if (data.d) {
setTimeout(function() {
//$('#ctl00_holderLeft_txt_email').val('');
$('#tip_email').removeClass('validation-valid');
$('#tip_email').addClass('validation-error');
$('#tip_email').html('<strong>"' + email + '</strong>"已存在,请使用其它邮箱');
}, 10);
}
//else
//$('#tip_email').html("邮箱输入成功");
},
error: function(xhr) {
$('#tip_email').html(xhr.responseText);
}
});
}
</script>
<style type="text/css">
.validation-ready{
background:none;
padding-left:0px;
}
.validation-ready, .validation-focus, .validation-error, .validation-valid { width: 300px; padding-left: 25px; height: 20px; line-height: 20px; } .validation-ready { background: url(http://common.cnblogs.com/images/validate/reg1.gif) no-repeat 0px -1px; /*display:none;#E9FFEB*/ } .validation-focus { /*background: #E9F0FF url(http://common.cnblogs.com/images/validate/reg2.gif) no-repeat 0px -1px;*/ background:url(http://common.cnblogs.com/images/validate/reg2.gif) no-repeat 0px -1px; } .validation-error { /*background: #FFF2E9 url(http://common.cnblogs.com/images/validate/reg3.gif) no-repeat 0px -1px;*/ background: url(http://common.cnblogs.com/images/validate/reg3.gif) no-repeat 0px -1px; } .validation-valid { /*background: #E9FFEB url(http://common.cnblogs.com/images/validate/reg4.gif) no-repeat 0px -1px;*/ background:url(http://common.cnblogs.com/images/validate/reg4.gif) no-repeat 0px -1px; }
</style>