📄 full-validator.js
字号:
(function(ns){ Request.XML = new Class({ Extends: Request, success: function(text, xml){ if(Browser.Features.xpath) { xml.selectNodes = function(xpath){ var nodes = [], node; var result = xml.evaluate(xpath, this, this.createNSResolver(xml.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ; if (result) while(node = result.iterateNext()) nodes.push(node); return nodes; }; xml.selectSingleNode = function(xpath){ var result = xml.evaluate(xpath, this, this.createNSResolver(xml.documentElement), 9, null); return (result && result.singleNodeValue) ? result.singleNodeValue : []; }; } this.onSuccess(xml, text); } }); ns.contains = function(v, or){ v = $splat(v); or = $pick(or, true); for(var result = false, i = 0, l = v.length; i < l; i ++){ result = ((this & v[i]) == v[i]); if(result && or || !result && !or) break; } return result; }; ns.trim = { all : function(txt){return (txt || this).replace(/(^[ \s]+)|([ \s]+$)/g, '');}, left : function(txt){return (txt || this).replace(/^[ \s]+/, '');}, right : function(txt){return (txt || this).replace(/[ \s]+$/, '');} }; ns.isDateTime = function(format, reObj){ format = format || 'yyyy-MM-dd'; var input = this, o = {}, d = new Date(); var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g); var len = f1.length, len1 = f3.length; if(len != f2.length || len1 != f4.length) return false; for(var i = 0; i < len1; i ++)if(f3[i] != f4[i]) return false; for(var i = 0; i < len; i ++) o[f1[i]] = f2[i]; o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4); o.MM = s(o.MM, o.M, d.getMonth() + 1, 12); o.dd = s(o.dd, o.d, d.getDate(), 31); o.hh = s(o.hh, o.h, d.getHours(), 24); o.mm = s(o.mm, o.m, d.getMinutes()); o.ss = s(o.ss, o.s, d.getSeconds()); o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3); if(o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false; if(o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000); d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms); var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms; return reVal && reObj ? d : reVal; function s(s1, s2, s3, s4, s5){ s4 = s4 || 60, s5 = s5 || 2; var reVal = s3; if(s1 != undefined && s1 != '' || !isNaN(s1)) reVal = s1 * 1; if(s2 != undefined && s2 != '' && !isNaN(s2)) reVal = s2 * 1; return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal; } }; var $properties = "accept,action,as,code,depend,elements,empty,expression,for,format,max,maxElement,min,minElement,operator,options,pass,pattern,prop,regex,require,rule,tips,to,toElement,trim,warn".split(','); var $triggers = { submit : 1, blur : 2, any : 3 }; var $regexs = { require : /.+/, email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/, mobile : /^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15[389]\d{8}))$/, url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/, ip : /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/, currency : /^\d+(\.\d+)?$/, number : /^\d+$/, zip : /^[1-9]\d{5}$/, qq : /^[1-9]\d{4,8}$/, english : /^[A-Za-z]+$/, chinese : /^[\u0391-\uFFE5]+$/, username : /^[a-z]\w{3,19}$/i, integer : /^[-\+]?\d+$/, 'double' : /^[-\+]?\d+(\.\d+)?$/ }; var util = { numberFilter : function(input){ return input.replace(/\D/g, ''); }, ipFix : function(input){ return $regexs.ip.test(input)?input.replace(/([^\d])(\d{1,2})(?=\.|\b)/g, '$100$2') : false; }, comma : /\s*[,,]\s*/ }; var AbstractMessenger = new Class({ Implements : Options, options : { warn : null, tips : null, pass : null, remote : null }, initialize : function(form, options){ this.form = form; this.setOptions(options); this.bound = { 'onSuccess' : this.showPass.bind(this), 'onFailure' : this.showWarn.bind(this), 'onTips' : this.showTips.bind(this) }; if (this.options.initialize) this.options.initialize.call(this); }, showWarn : function(o){this.process(o, 'warn');}, showTips : function(o){o.element.title = this.chk(o, 'tips') ? o.options.tips : '';}, showPass : function(o){if(o !== this.form) this.process(o, 'pass');}, process : function(o, key){}, chk : function(o, key){return o.options && o.options[key] && o.options[key] != ''} }); ns.FollowMessenger = new Class({ Extends : AbstractMessenger, options : { warn : 'msg block', tips : 'msg tips', remote : 'msg loading', pass : 'msg pass', blur : 'msg' }, initialize : function(form, options){ arguments.callee.parent(form, options); this.bound.onRemote = this.showRemote.bind(this); }, showTips : function(o, isBlur){if(o !== this.form && this.chk(o, 'tips')) o.getMessagePanel().setProperties({html : o.options.tips, 'class' : this.options[isBlur ? 'blur' : 'tips']});}, showRemote : function(o){o.getMessagePanel().setProperties({html : '\u6b63\u5728\u8fdc\u7a0b\u9a8c\u8bc1\uff0c\u8bf7\u7a0d\u5019...', 'class' : this.options.remote}).setStyle('visibility', 'visible');}, process : function(o, key){ $splat(o).each(function(e){ var msg = e.getMessage(); e.getMessagePanel().setProperties({html : msg, 'class' : this.options[key]}).setStyle('visibility', msg && msg != '' ? 'visible' : 'hidden'); }.bind(this)); } }); ns.ColorMessenger = new Class({ Extends : AbstractMessenger, options : { warn : 'red', pass : 'green' }, process : function(o, key){ $splat(o).each(function(e){ e.element.setStyle('color', this.options[key]).title = e.getMessage(); }.bind(this)); } }); ns.ClassMessenger = new Class({ Extends : AbstractMessenger, options : { warn : 'style-warn', pass : 'style-pass' }, process : function(o, key){ var ops = this.options; $splat(o).each(function(e){ e.element.removeClass(ops.warn).removeClass(ops.pass).addClass(ops[key]).title = e.getMessage(); }); } }); ns.TipsMessenger = new Class({ Extends : AbstractMessenger, options : { offset : {x : 0, y : 0}, 'class' : 'tooltips' }, showTips : function(o, isBlur){ if(o === this.form || !this.chk(o, 'tips')) return; if(isBlur) {this.getDialog().setStyle('visibility', 'hidden');return;} var ops = this.options, pos = o.element.getPosition(); var height = this.getDialog().set('html', o.options.tips).offsetHeight; this.dialog.setStyles({left : pos.x + ops.offset.x, top : pos.y + ops.offset.y - height, visibility : 'visible'}).addClass(ops.tips || ''); }, showPass : function(){this.getDialog().setStyle('visibility', 'hidden');}, getDialog : function(){ return this.dialog = this.dialog || new Element('div', {'class' : this.options['class']}).inject(this.form.getForm()); }, process : function(o, key){ var ops = this.options,o = $splat(o)[0]; var pos = o.element.getPosition(); var height = this.getDialog().set('html', o.getMessage()).offsetHeight; this.dialog.setStyles({left : pos.x + ops.offset.x, top : pos.y + ops.offset.y - height, visibility : 'visible'}).addClass(ops[key] || ''); } }); ns.AlertMessenger = new Class({ Extends : AbstractMessenger, options : { showOne : false}, showPass : function(o){ if(o !== this.form) o.element.title = this.chk(o, 'pass') ? o.options.pass : ''; }, process : function(o, key){ if(o.length){ var warns = [this.form.options.title]; o.each(function(e, i){ warns.push((i + 1) + '.' + e.getMessage()); e.element.title = e.getMessage(); }); alert(warns.join('\n')); } else { if(this.options.showOne) alert(o.getMessage()); o.element.title = o.getMessage(); } } }); ns.SummaryMessenger = new Class({ Extends : AbstractMessenger, options : {id : ''}, showPass : function(o){ if(o !== this.form) {if(this.chk(o, 'pass')) o.element.title = o.options.pass;} else $(o.options.summary).setStyle('display', ''); }, process : function(o){ if(o.length){ var warns = ['<div class="title">' + this.form.options.title + '</div><ol>']; o.each(function(e, i){ warns.push('<li>' + e.getMessage() + '</li>'); e.element.title = e.getMessage(); }); $(this.options.id).setStyle('display', 'block').set('html', warns.join('') + '</ol>'); } } }); var AbstractReader = new Class({ Implements : [Events, Options], options : { wait : true }, initialize : function(form, options){ this.form = form; this.setOptions(options); if (this.options.initialize) this.options.initialize.call(this); if(this.options.wait) window.addEvent('domready', this.read.bind(this)); else this.read(); }, read : function(){}, fill : function(){} }); ns.AttributeReader = new Class({ Extends : AbstractReader, read : function(){ this.form.getForm().getElements('input[rule], select[rule], textarea[rule]', true).each(function(el){ if (!el.name || el.disabled) return; this.fill(el); }.bind(this)); }, fill : function(el){ this.form.addElement(ns.Factory.Element.build(ns.Factory.Element.getAttributes(el))); } }); ns.TagReader = new Class({ Extends : AbstractReader, options : { ns : 'ui', name : 'v' }, initialize : function(form, options){ this.setNS(options.ns || this.options.ns); arguments.callee.parent(form, options); }, getTag : function(){ var ops = this.options; return (ops.ns == '' || Browser.Engine.trident) ? ops.name : ops.ns + ':' + ops.name; }, setNS : function(ns){ if(Browser.Engine.trident) document.namespaces.add(ns, 'http://wfsr.net'); else document.documentElement.setAttribute('xmlns:' + ns, 'http://wfsr.net'); }, read : function(){ var ops = this.options; $A(this.form.getForm().getElementsByTagName(this.getTag())).each(function(el, i){ if (!el.getAttribute('for')) return; this.fill(el); }.bind(this)); }, fill : function(el){ var options = ns.Factory.Element.getAttributes(el); options.holder = el; options.element = this.form.getFormItem(options['for']); this.form.addElement(ns.Factory.Element.build(options)); } }); ns.JsonReader = new Class({ Extends : AbstractReader, options : { wait : false, url : 'rules/validation.js', as : 'JSON' }, read : function(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -