📄 validator.js.svn-base
字号:
//=====================================================================================//===//=== Validator//=== //=== Validates the content of text fields against a set of rules//===//=== Needs : geonetwork-ajax.js//===//=====================================================================================/* The 'add' accepts an array of rules. Each rule is a map like this: { id:'...', type:'length', minSize: ..., maxSize: ...} { id:'...', type:'alphanum'} { id:'...', type:'integer', minValue: ..., maxValue: ..., empty:true} Supports the following validators: - length : the field must be a string which length must be [minSize..maxSize]. Both minSize and maxSize can be omitted. - alphanum: the string must start with a letter and must be formed by letters or digits. - integer : the field must be an integer in the range [minValue..maxValue]. Both minValue and maxValue can be omitted. 'empty' means that the field can be empty. - hostname: the string can contains only letters, digits and '.','-' - url : the string must conform to the URL specification (i.e. must be a valid URL). Notice that this validator is incomplete. This method expects to find some strings into the xmlLoader object. This is a suggested layout for the string properties. Notice that this is not required: the element structure can be flat, without the 'validator' element. <validator> <string> <cannotBeEmpty>{NAME} cannot be mpty</cannotBeEmpty> <invalidMinSize>{NAME} must be at least {VALUE} character(s) long</invalidMinSize> <invalidMaxSize>{NAME} must be maximum {VALUE} character(s) long</invalidMaxSize> <notAlphaNumeric>{NAME} must be alphanumeric</notAlphaNumeric> </string> <integer> <notInt>{NAME} must be an integer</notInt> <invalidMinValue>{NAME} must be >= {VALUE}</invalidMinValue> <invalidMaxValue>{NAME} must be <= {VALUE}</invalidMaxValue> </integer> <hostname> <notHostName>{NAME} is not a valid host name</notHostName> </hostname> <url> <notURL>{NAME} is not a valid URL</notURL> </url> </validator> */function Validator(xmlLoader){ this.xmlLoader = xmlLoader; this.rules = []; }//=====================================================================================Validator.prototype.add = function(rules, parentId){ for (var i=0; i<rules.length; i++) { var rule = rules[i]; if (!parentId) rule.ctrl = $(rule.id); else { rule.parentId = parentId; rule.ctrl = gn.getElementById($(parentId), rule.id); } if (rule.type == 'length') rule.validator = gn.wrap(this, this.lengthVal); else if (rule.type == 'alphanum') rule.validator = gn.wrap(this, this.alphanumVal); else if (rule.type == 'integer') rule.validator = gn.wrap(this, this.integerVal); else if (rule.type == 'hostname') rule.validator = gn.wrap(this, this.hostnameVal); else if (rule.type == 'url') rule.validator = gn.wrap(this, this.urlVal); else throw 'Unknown validator type : '+ rule.type; this.rules.push(rule); }}//=====================================================================================Validator.prototype.removeByParent = function(parentId){ var rules = this.rules; this.rules = []; for(var i=0; i<rules.length; i++) if ((!rules[i].parentId) || (rules[i].parentId != parentId && parentId)) this.rules.push(rules[i]);}//=====================================================================================//=== Private validators//=====================================================================================Validator.prototype.lengthVal = function(rule){ var text = rule.ctrl.value; var len = text.length; var result= null; if (rule.minSize && len == 0 && rule.minSize != 0) result = ['cannotBeEmpty']; else if (rule.minSize && len < rule.minSize) result = ['invalidMinSize', rule.minSize]; else if (rule.maxSize && len > rule.maxSize) result = ['invalidMaxSize', rule.maxSize]; return result;}//=====================================================================================Validator.prototype.alphanumVal = function(rule){ var text = rule.ctrl.value; var result= null; if (!this.isAlphaNumeric(text)) result = ['notAlphaNumeric']; return result;}//=====================================================================================Validator.prototype.hostnameVal = function(rule){ var text = rule.ctrl.value; var result= null; if (!this.isHostName(text)) result = ['notHostName']; return result;}//=====================================================================================Validator.prototype.urlVal = function(rule){ var text = rule.ctrl.value; var result= null; if (!this.isURL(text)) result = ['notURL']; return result;}//=====================================================================================Validator.prototype.integerVal = function(rule){ var text = rule.ctrl.value; var len = text.length; var value = parseInt(text); var result= null; if (text == '' && rule.empty) return null; if (!this.isInteger(text)) result = ['notInt']; else if (rule.minValue && value < rule.minValue) result = ['invalidMinValue', rule.minValue]; else if (rule.maxValue && value > rule.maxValue) result = ['invalidMaxValue', rule.maxValue]; return result;}//=====================================================================================Validator.prototype.validate = function(){ for (var i=0; i<this.rules.length; i++) { var rule = this.rules[i]; var result = rule.validator(rule); if (result != null) { this.showError(rule.ctrl, result); return false; } } return true;}//=====================================================================================//=== Private methods//=====================================================================================Validator.prototype.showError = function(ctrl, result){ var msg = this.xmlLoader.getText(result[0]); var name = ctrl.getAttribute('id'); var value = (result.length == 1) ? '' : ''+ result[1]; var pos = name.lastIndexOf('.'); if (pos != -1) name = name.substring(pos +1); name = this.xmlLoader.getText(name).toLowerCase(); msg = gn.replace(msg, '{NAME}', name); msg = gn.replace(msg, '{VALUE}', value); alert(msg); ctrl.focus();// ctrl.select();}//=====================================================================================//=== Functions that implements the validation rules//=====================================================================================Validator.prototype.isInteger = function(text){ for (var i=0; i<text.length; i++) { var c = text.charAt(i); if (this.isDigit(c)) continue; if (c == '-' && i == 0) continue; return false; } return (text.length != 0);}//=====================================================================================Validator.prototype.isAlphaNumeric = function(text){ for (var i=0; i<text.length; i++) { var c = text.charAt(i); if (this.isLetter(c)) continue; if (i != 0 && this.isDigit(c)) continue; return false; } return true;}//=====================================================================================Validator.prototype.isHostName = function(text){ for (var i=0; i<text.length; i++) { var c = text.charAt(i); if (this.isLetter(c) || this.isDigit(c)) continue; if (c == '.' || c == '-') continue; return false; } return true;}//=====================================================================================Validator.prototype.isURL = function(text){ var http = (text.indexOf('http://' ) == 0); var https = (text.indexOf('https://') == 0); var ftp = (text.indexOf('ftp://' ) == 0); var ftps = (text.indexOf('ftps://' ) == 0); if (!http && !https && !ftp && !ftps) return false; for (var i=0; i<text.length; i++) { var c = text.charAt(i); if (this.isLetter(c) || this.isDigit(c)) continue; if ('.-:/_%'.indexOf(c) != -1) continue; return false; } return true;}//=====================================================================================Validator.prototype.isLetter = function(c){ return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ); }//=====================================================================================Validator.prototype.isDigit = function(c){ return ((c >= "0") && (c <= "9"));}//=====================================================================================Validator.prototype.isLetterOrDigit = function(c){ return (isLetter(c) || isDigit(c)); }//=====================================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -