// JS validator,Copyright (c) 2008 Arian Stolwijk, , MIT Style License. function jsValidator(){ var Validator = { /** * Version of the jsValidator */ varsion : '0.5a', /** * Settings */ settings: { /** * The standard message */ msg: 'This field is not correctly filled in', /** * The style of the message, any css/javascript style (eg. color, paddingLeft) */ msgStyle: { paddingLeft: '5px', color: 'red', maxWidth: '300px' } }, elmts : { // The Elements }, elmt: { // The field objects }, /** * Initializes the validator */ init: function(){ for (var elem in this.elmts) { //for each element.... // Skip to the next if the field doesn't extists if(document.getElementById(elem) == null){ continue; } if(document.getElementById(elem).tagName == 'input' || document.getElementById(elem).tagName == 'select' || document.getElementById(elem).tagName == 'textarea'){ continue; } this.elmt[elem] = document.getElementById(elem); if (typeof this.elmts[elem].validators != 'undefined') { this.addValidators(elem); }else { this.addValidator(elem); } } }, /** * Add a validator to a field, checks if the field is correct and calls the message functions * @param {Object} elmt */ addValidator: function(elem){ this.elmt[elem].onblur = function(){ if (typeof Validator.elmts[elem].validator != 'undefined' ) { // Check if the field is correctly set var result = Validator.validate(elem, Validator.elmts[elem].validator); if (result == false) { // check if the message is set if (Validator.elmts[elem].msg) { var msg = Validator.elmts[elem].msg; }else { var msg = Validator.settings.msg; } Validator.inlineMsg(elem, msg); return; // Stop the function, one error is enough }else if (result == 'undefined'){ }else{ Validator.hideMsg1(elem); } } if (typeof Validator.elmts[elem].regex != 'undefined') { // Validate the regex Validator.validateRegex(elem); } } }, /** * Add more than one validators to a field. * @param {Object} elem */ addValidators: function(elem){ if(typeof this.elmts[elem].validators == 'object'){ this.elmt[elem].onblur = function(){ for (var validator in Validator.elmts[elem].validators) { var result = Validator.validate(elem,validator); if (result == false) { // Validator.elmts[elem].validators[validator] if (Validator.elmts[elem].validators[validator].msg) { var msg = Validator.elmts[elem].validators[validator].msg; } else if (Validator.elmts[elem].msg) { var msg = Validator.elmts[elem].msg; } else { var msg = Validator.settings.msg; } Validator.inlineMsg(elem, msg); return; // Stop the function, one error is enough }else if(result == 'undefined'){ }else{ // Everything went well Validator.hideMsg1(elem); } } // Validate the regex Validator.validateRegex(elem); } } }, /** * * @param {Object} elem * @param {Object} validator * @param {Object} regex */ validate: function(elem,validator,regex){ if (this.elmt[elem].tagName == 'SELECT') { value = this.elmt[elem].options[Validator.elmt[elem].selectedIndex].value; } else { value = this.elmt[elem].value; } // Set valid to false var valid = false; if (regex && validator == 'regex') { // If there's a self made regex value = value+''; if (value.match(regex)) { // if the field is correct, set the valid var to true valid = true; } }else if(typeof Validator.elmts[elem].validator == 'function'){ // There's a self made funtion if(Validator.elmts[elem].validator(value)){ // if the field is correct, set the valid var to true valid = true; } }else { // The included validate methods if (typeof Validators[validator] == 'function') { if (Validators[validator](value + '')) { // if the field is correct, set the valid var to true valid = true; } }else{ valid = 'undefined'; } } return valid; }, validateRegex: function(elem){ // When there's a regex set for this validator, // then check if there's a regex set for the field // When it isn't, set it to null msg = true; if (typeof this.elmts[elem].regex[0] == 'object') { var regexObj = this.elmts[elem].regex; for(var i=0;i]+)>)/ig,"") == value && value.length > 0; }, /** * same as NoHTML, only now the value is also valid if it is empty * * @param string value * @return bool */ _NoHTML: function(value){ return value.length == 0 || this.NoHTML(value); }, /** * is a valid dutch postcode (eg. 9999 AA) * * @param {string} value * @return bool */ IsPostcode: function(value) { return value.match(/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/); }, /** * same as IsPostcode, only now the value is also valid if it is empty * * @param string value * @return bool */ _IsPostcode: function(value) { return value.length == 0 || this.IsPostcode(value); }, /** * is a valid dutch phone-number * * @param {string} value * @return bool */ IsPhone: function(value) { regex = /^[0-9]{2,4}[-]?[0-9]{6,8}$/; value = value.replace(' ',''); value = value.replace('-',''); return value.length == 10 && value.match(regex); }, /** * same as IsPhone, only now the value is also valid if it is empty * * @param string value * @return bool */ _IsPhone: function(value) { return value.length == 0 || this.IsPhone(value); }, /** * check if it's a valid ip adres * * @param {string} value * @return bool */ IsIp: function(ip ) { return ip.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}:?\d*$/); }, /** * same as IsIp, only now the value is also valid if it is empty * * @param string value * @return bool */ _IsIp: function(ip ) { return value.length == 0 || this.IsIp(value); } }