NxTIC/js/lib/min/input-checker.js

8 lines
2.3 KiB
JavaScript
Raw Normal View History

function formatChecker(a,c,b){this.value=a;this.pattern=c;this.rules=2<arguments.length?b:[]}
formatChecker.prototype={value:this.value,pattern:this.pattern,rules:this.rules,regexp:null,default_rules:{i:"[0-9]",a:"[a-z]",A:"[A-Z]",x:"[a-zA-Z]"},compile:function(){for(var a="^",c=[],b=0;b<this.pattern.length;b++)this.rules.hasOwnProperty(this.pattern[b])?(a+=this.rules[this.pattern[b]],c.push(this.rules[this.pattern[b]])):this.default_rules.hasOwnProperty(this.pattern[b])?(a+=this.default_rules[this.pattern[b]],c.push(this.default_rules[this.pattern[b]])):(a+=this.pattern[b],c.push(this.pattern[b]));
this.regexp=new RegExp(a+"$");this.regexp.patternDecomposition=c},check:function(a){null==this.regexp&&this.compile();if(0<arguments.length&&a instanceof Array)for(var c=0;c<this.pattern.length;c++){var b="^",b=this.rules.hasOwnProperty(this.pattern[c])?b+this.rules[this.pattern[c]]:this.default_rules.hasOwnProperty(this.pattern[c])?b+this.default_rules[this.pattern[c]]:b+this.pattern[c],b=b+"$";a.push(null!=this.value[c].match(new RegExp(b)))}return null!=this.value.match(this.regexp)}};
function inputChecker(){}
inputChecker.prototype={input:[],defval:[],checker:[],append:function(a,c,b){if(!(a instanceof HTMLInputElement&&c instanceof formatChecker))return!1;var d=this.input.push(a);if(d!=this.checker.push(c)||d!=this.defval.push(2<arguments.length?b:null))return!1},check:function(a){a=this.input.indexOf(a);if(0>a)return!1;this.checker[a].value=this.input[a].value;return this.checker[a].check()},checkAll:function(){for(var a=!0,c=0;c<this.input.length;c++)a=a&&this.check(this.input[c]);return a},correct:function(a,
c){if(0>(index=this.input.indexOf(a)))return null;c=1<arguments.length?c:!0;this.checker[index].value=this.input[index].value;this.checker[index].compile();var b=this.checker[index].regexp.patternDecomposition,d=this.input[index].value;if(!this.check(a)){for(var e=0;e<b.length&&(c||!(e>=this.input[index].value.length));e++){var f=new RegExp("^"+b[e]+"$");0==d.length||null==d[e]?d=d.slice(0,e).concat(this.defval[index][e]).concat(d.slice(e)):null==d[e].match(f)&&(d=null!=d[e].match(new RegExp("^"+
b[e+1]+"$"))?d.slice(0,e).concat(this.defval[index][e]).concat(d.slice(e)):d.slice(0,e).concat(this.defval[index][e]).concat(d.slice(e+1)))}d=d.slice(0,b.length);this.input[index].value=d}}};