function monorail_formhelper_numberonly(e, exceptions, forbidalso) { exceptions = exceptions.concat([0,8,9,13,46]); var code = navigator.appName == "Netscape" ? e.charCode : e.keyCode; if ((e.ctrlKey && code == 118) || (e.ctrlKey && code == 122)) { return false; } if (e.ctrlKey || e.altKey) return true; for(var i=0; i < forbidalso.length; i++) if (forbidalso[i] == code) return false; for(var i=0; i < exceptions.length; i++) if (exceptions[i] == code) return true; if (code <= 47 || code > 57) return false; return true; } function monorail_formhelper_inputfilter(e, forbid) { var code = navigator.appName == "Netscape" ? e.charCode : e.keyCode; for(var i=0; i < forbid.length; i++) if (forbid[i] == code) return false; } /* The scripts on this page was produced by mordechai Sandhaus - 52action.com, and is copyrighted . If you like this script, we encourage you to use it, provided that include this note, and link to 52action.com. */ function monorail_formhelper_getkeycode(e) { if( typeof( e.keyCode ) == 'number' ) { return e.keyCode; } else if( typeof( e.which ) == 'number' ) { return e.which; } else if( typeof( e.charCode ) == 'number' ) { return e.charCode; } else { return null; } } function monorail_formhelper_getevent(e) { if (!e) { if (window.event) { return window.event; } else { return null; } } else { return e; } } function monorail_formhelper_mask(e, elem,loc,delim) { e = monorail_formhelper_getevent(e); var keycode = monorail_formhelper_getkeycode(e); var locs = loc.split(','); var str = elem.value; for (var i = 0; i <= locs.length; i++) { for (var k = 0; k <= str.length; k++) { if (k == locs[i]) { if (str.substring(k, k+1) != delim) { if (keycode != 8) { str = str.substring(0,k) + delim + str.substring(k,str.length); } } } } } elem.value = str } /* * Really easy field validation with Prototype * http://tetlaw.id.au/view/javascript/really-easy-field-validation * Andrew Tetlaw * Version 1.5.4.1 (2007-01-05) * * Copyright (c) 2007 Andrew Tetlaw * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ var Validator = Class.create(); Validator.prototype = { initialize : function(className, error, test, options) { if(typeof test == 'function'){ this.options = $H(options); this._test = test; } else { this.options = $H(test); this._test = function(){return true}; } this.error = error || 'Validation failed.'; this.className = className; }, test : function(v, elm) { return (this._test(v,elm) && this.options.all(function(p){ return Validator.methods[p.key] ? Validator.methods[p.key](v,elm,p.value) : true; })); } } Validator.methods = { pattern : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || opt.test(v)}, minLength : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || v.length >= opt}, maxLength : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || v.length <= opt}, min : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || v >= parseFloat(opt)}, max : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || v <= parseFloat(opt)}, notOneOf : function(v,elm,opt) {return $A(opt).all(function(value) { return v != value; })}, oneOf : function(v,elm,opt) {return $A(opt).any(function(value) { return v == value; })}, is : function(v,elm,opt) {return v == opt}, isNot : function(v,elm,opt) {return v != opt}, equalToField : function(v,elm,opt) {return v == $F(opt)}, notEqualToField : function(v,elm,opt) {return v != $F(opt)}, include : function(v,elm,opt) {return $A(opt).all(function(value) { return Validation.get(value).test(v,elm); })} } var Validation = Class.create(); Validation.prototype = { initialize : function(form, options){ this.options = Object.extend({ onSubmit : true, stopOnFirst : false, immediate : false, focusOnError : true, useTitles : false, onFormValidate : function(result, form) {}, onElementValidate : function(result, elm) {} }, options || {}); this.form = $(form); if(this.options.onSubmit) Event.observe(this.form,'submit',this.onSubmit.bind(this),false); if(this.options.immediate) { var useTitles = this.options.useTitles; var callback = this.options.onElementValidate; Form.getElements(this.form).each(function(input) { // Thanks Mike! Event.observe(input, 'blur', function(ev) { Validation.validate(Event.element(ev),{useTitle : useTitles, onElementValidate : callback}); }); }); } }, onSubmit : function(ev){ if(!this.validate()) Event.stop(ev); }, validate : function() { var result = false; var useTitles = this.options.useTitles; var callback = this.options.onElementValidate; if(this.options.stopOnFirst) { result = Form.getElements(this.form).all(function(elm) { return Validation.validate(elm,{useTitle : useTitles, onElementValidate : callback}); }); } else { result = Form.getElements(this.form).collect(function(elm) { return Validation.validate(elm,{useTitle : useTitles, onElementValidate : callback}); }).all(); } if(!result && this.options.focusOnError) { Form.getElements(this.form).findAll(function(elm){return $(elm).hasClassName('validation-failed')}).first().focus() } this.options.onFormValidate(result, this.form); return result; }, reset : function() { Form.getElements(this.form).each(Validation.reset); } } Object.extend(Validation, { validate : function(elm, options){ options = Object.extend({ useTitle : false, onElementValidate : function(result, elm) {} }, options || {}); elm = $(elm); var cn = elm.classNames(); return result = cn.all(function(value) { var test = Validation.test(value,elm,options.useTitle); options.onElementValidate(test, elm); return test; }); }, test : function(name, elm, useTitle) { var v = Validation.get(name); var prop = '__advice'+name.camelize(); try { if(Validation.isVisible(elm) && !v.test($F(elm), elm)) { if(!elm[prop]) { var advice = Validation.getAdvice(name, elm); if(advice == null) { var errorMsg = useTitle ? ((elm && elm.title) ? elm.title : v.error) : v.error; advice = '' switch (elm.type.toLowerCase()) { case 'checkbox': case 'radio': var p = elm.parentNode; if(p) { new Insertion.Bottom(p, advice); } else { new Insertion.After(elm, advice); } break; default: new Insertion.After(elm, advice); } advice = Validation.getAdvice(name, elm); } if(typeof Effect == 'undefined') { advice.style.display = 'block'; } else { new Effect.Appear(advice, {duration : 1 }); } } elm[prop] = true; elm.removeClassName('validation-passed'); elm.addClassName('validation-failed'); return false; } else { var advice = Validation.getAdvice(name, elm); if(advice != null) advice.hide(); elm[prop] = ''; elm.removeClassName('validation-failed'); elm.addClassName('validation-passed'); return true; } } catch(e) { throw(e) } }, isVisible : function(elm) { if(elm!=null){ while(elm.tagName != 'BODY') { if(!$(elm).visible()) return false; elm = elm.parentNode; } } return true; }, getAdvice : function(name, elm) { return $('advice-' + name + '-' + Validation.getElmID(elm)) || $('advice-' + Validation.getElmID(elm)); }, getElmID : function(elm) { return elm.id ? elm.id : elm.name; }, reset : function(elm) { elm = $(elm); var cn = elm.classNames(); cn.each(function(value) { var prop = '__advice'+value.camelize(); if(elm[prop]) { var advice = Validation.getAdvice(value, elm); advice.hide(); elm[prop] = ''; } elm.removeClassName('validation-failed'); elm.removeClassName('validation-passed'); }); }, add : function(className, error, test, options) { var nv = {}; nv[className] = new Validator(className, error, test, options); Object.extend(Validation.methods, nv); }, addAllThese : function(validators) { var nv = {}; $A(validators).each(function(value) { nv[value[0]] = new Validator(value[0], value[1], value[2], (value.length > 3 ? value[3] : {})); }); Object.extend(Validation.methods, nv); }, get : function(name) { return Validation.methods[name] ? Validation.methods[name] : Validation.methods['_LikeNoIDIEverSaw_']; }, methods : { '_LikeNoIDIEverSaw_' : new Validator('_LikeNoIDIEverSaw_','',{}) } }); String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); } Validation.add('IsEmpty', '', function(v) { return ((v == null) || (v.length == 0)); // || /^\s+$/.test(v)); }); Validation.addAllThese([ ['required', 'Required Field', function(v) { if(v != null) v = v.trim(); return !Validation.get('IsEmpty').test(v); }], ['validate-number', 'Invalid Number', function(v) { return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v)); }], ['validate-digits', 'InvalidDigit', function(v) { return Validation.get('IsEmpty').test(v) || !/[^\d]/.test(v); }], ['validate-alpha', 'Invalid Alpha', function (v) { return Validation.get('IsEmpty').test(v) || /^[a-zA-Z]+$/.test(v) }], ['validate-alphanum', 'Invalid', function(v) { return Validation.get('IsEmpty').test(v) || !/\W/.test(v) }], ['validate-date', 'Invalid Date', function(v,item) { try{ valid = true //If it's not required and the string is empty let it through. if ((v == null || v.length == 0) && !$(item.id).hasClassName('required')) return true; //In order for this to work on form post we need a synchronous check.... new Ajax.Request('IsValidDate.aspx?date='+v, { asynchronous:false, onSuccess: function(transport) { if(transport.responseText=='true') { $(item.id).removeClassName("validation-failed") $(item.id).addClassName("validation-passed") } else { $(item.id).removeClassName("validation-passed") $(item.id).addClassName("validation-failed") $(item.id).value = ''; valid = false; } } }); }catch(e){ return false; } return valid; }], ['validate-datetime', 'Invalid Date Time', function(v,item) { try{ valid = true //If it's not required and the string is empty let it through. if ((v == null || v.length == 0) && !$(item.id).hasClassName('required')) return true; //In order for this to work on form post we need a synchronous check.... new Ajax.Request('IsValidDate.aspx?date='+v, { asynchronous:false, onSuccess: function(transport) { if(transport.responseText=='true') { $(item.id).removeClassName("validation-failed") $(item.id).addClassName("validation-passed") } else { $(item.id).removeClassName("validation-passed") $(item.id).addClassName("validation-failed") $(item.id).value = ''; valid = false; } } }); }catch(e){ return false; } return valid; }], ['validate-email', 'Invalid Email Address', function (v) { return Validation.get('IsEmpty').test(v) || /^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})+/.test(v) }], ['validate-emails', 'Invalid Email Address', function (v) { var emails = ""; if(v.endsWith(";")) v = v.substring(0,v.length-1); if(v.indexOf(";")>-1){ var isOK = true; emails = v.split(";"); emails.each(function(item) { if(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(item) == false){ isOK = false; alert("This email address '"+item+"' is not a valid one.\r\n Please remove it from the list or correct it."); } }); return isOK; }else{ return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v) } }], ['validate-url', 'Invalid URL', function (v) { return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v) }], ['validate-date-au', 'Invalid Date', function(v) { if(Validation.get('IsEmpty').test(v)) return true; var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/; if(!regex.test(v)) return false; var d = new Date(v.replace(regex, '$2/$1/$3')); return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) && (parseInt(RegExp.$1, 10) == d.getDate()) && (parseInt(RegExp.$3, 10) == d.getFullYear() ); }], ['validate-currency', 'Invalid Currency', function(v,item) { try{ valid = true //If it's not required and the string is empty let it through. if ((v == null || v.length == 0) && !$(item.id).hasClassName('required')) return true; if ($(item.id).hasClassName('required')) $(item.id).title = 'Invalid Currency' + ' ' + 'Required Field' else $(item.id).title = 'Invalid Currency' //In order for this to work on form post we need a synchronous check.... new Ajax.Request('IsValidCurrency.aspx?number='+v, { asynchronous:false, onSuccess: function(transport) { if(transport.responseText=='true') { $(item.id).removeClassName("validation-failed") $(item.id).addClassName("validation-passed") } else { $(item.id).removeClassName("validation-passed") $(item.id).addClassName("validation-failed") valid = false; } } }); }catch(e){ return false; } return valid; }], ['validate-currency-dollar', 'Invalid Currency Dollar', function(v) { // [$]1[ // [$]1 // [$]0. // [$]. return Validation.get('IsEmpty').test(v) || /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v) }], ['validate-selection', 'Required Selection', function(v,elm){ return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v); }], ['validate-one-required', 'Please select an option.', function (v,elm) { var p = elm.parentNode; var options = p.getElementsByTagName('INPUT'); return $A(options).any(function(elm) { return $F(elm); }); }], ['myvalidate-one-required', 'Please select an option.', function (v,elm) { var tableId = elm.getAttribute('table_id'); var options = new Array(); options = document.getElementsByClassName('table_'+tableId); return $A(options).any(function(elm) { return $F(elm); }); }], ['table-validate-one-required', 'Please select an option.', function (v,elm) { var p = elm; var options = new Array(); while (options.length < 2) { p = p.parentNode; options = p.getElementsByTagName('INPUT'); } return $A(options).any(function(elm) { return $F(elm); }); }], ['validate-one-checkbox-required', 'Please select an option.', function (v,elm) { var p = elm.parentNode; while(p.tagName != "TABLE"){ p = p.parentNode; } var options = $A(p.getElementsByTagName('INPUT')); if(options.length>2){ var isChecked = false; options.each(function(item){ if(item.checked) isChecked = true; }); } return isChecked; }], ['validate-postal-code', 'Invalid Postal Code', function(v) { return Validation.get('IsEmpty').test(v) || /^\D{1}\d{1}\D{1}(\ |\-)?\d{1}\D{1}\d{1}$/.test(v); }], ['validate-zip-code', 'Invalid Zip Code', function(v) { return Validation.get('IsEmpty').test(v) || /^\d{5}([\-]\d{4})?$/.test(v); }], ['validate-phone', 'Invalid Phone Number', function(v, item) { //If it's not required and the string is empty let it through. if ((v == null || v.length == 0) && !$(item.id).hasClassName('required')) return true; return Validation.get('validate-number').test(v) && v.length >= 10; }] ]);