From efcc1890fbd9f0687c6d653ffbefec7a526bba17 Mon Sep 17 00:00:00 2001 From: Benjamin Keen Date: Sat, 1 Nov 2014 10:18:47 -0700 Subject: [PATCH] 2.5.3 - bug fix --- gruntfile.js | 3 ++- jquery/example_7_custom_error_handler.html | 9 +-------- jquery/jquery.rsv.js | 9 ++++++--- jquery/jquery.rsv.min.js | 5 ++++- prototype/prototype.rsv.js | 9 +++++---- prototype/prototype.rsv.min.js | 5 ++++- standalone/rsv.js | 13 ++++++++----- standalone/rsv.min.js | 5 ++++- 8 files changed, 34 insertions(+), 24 deletions(-) diff --git a/gruntfile.js b/gruntfile.js index 976f1ab..df883d3 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -14,7 +14,8 @@ module.exports = function(grunt) { }, options: { report: "min", - compress: true + compress: true, + banner: '// rsv.js - Really Simple Validation\n// v2.5.3, Nov 1 2014\n// https://github.com/benkeen/rsv\n' } } } diff --git a/jquery/example_7_custom_error_handler.html b/jquery/example_7_custom_error_handler.html index ab7418c..5fda6e6 100644 --- a/jquery/example_7_custom_error_handler.html +++ b/jquery/example_7_custom_error_handler.html @@ -52,14 +52,7 @@ document.getElementById(fieldName + "_error").innerHTML = errorInfo[i][1]; } - // normally, we'd do something like this: only return TRUE if there were no errors. - // but this is just a demo. so it's commented out. - //return (errorInfo.length == 0) ? true : false; - - if (errorInfo.length == 0) - alert("Form submitted here!"); - - return false; + return (errorInfo.length == 0); } var myRules = [ diff --git a/jquery/jquery.rsv.js b/jquery/jquery.rsv.js index ac11302..602a02a 100644 --- a/jquery/jquery.rsv.js +++ b/jquery/jquery.rsv.js @@ -2,7 +2,7 @@ * rsv.js - Really Simple Validation * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * - * v2.5.1, Nov 14 2008 + * v2.5.3, Nov 1 2014 * * This powerful little script lets you add client-side validation to any webform with very little * work. It includes a number of pre-existing routines for common tasks like validating email @@ -558,8 +558,11 @@ // if the user has defined a custom event handler, pass the information to it - if (typeof options.customErrorHandler == 'function') - return options.customErrorHandler(form, returnHash); + if (typeof options.customErrorHandler == 'function') { + if (!options.customErrorHandler(form, returnHash)) { + return false; + } + } // if the user has chosen "alert-all" or "return-errors", perform the appropriate action else if (options.displayType == "alert-all") diff --git a/jquery/jquery.rsv.min.js b/jquery/jquery.rsv.min.js index 044404e..90cdade 100644 --- a/jquery/jquery.rsv.min.js +++ b/jquery/jquery.rsv.min.js @@ -1 +1,4 @@ -!function($){function processError(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(options.displayType){case"alert-one":alert(b),styleField(a,!0),c=!1;break;case"alert-all":case"display-html":returnHash.push([a,b])}return c}function displayHTMLErrors(a,b){for(var c=options.errorTextIntro+"

",d=0;d",styleField(b[d][0],0==d);return b.length>0?($("#"+options.errorTargetElementId).css("display","block"),$("#"+options.errorTargetElementId).html(c),!1):!0}function styleField(a,b){if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0}var options={},returnHash=[];$.fn.RSV=function(a){return options=$.extend({},$.fn.RSV.defaults,a),this.each(function(){$(this).bind("submit",{currForm:this,options:options},$(this).RSV.validate)})},$.fn.RSV.defaults={rules:[],displayType:"alert-all",errorFieldClass:null,errorTextIntro:"Please fix the following error(s) and resubmit:",errorJSItemBullet:"* ",errorHTMLItemBullet:"• ",errorTargetElementId:"rsvErrors",customErrorHandler:null,onCompleteHandler:null},$.fn.RSV.validate=function(event){options=event.data.options;var form=event.data.currForm,rules=options.rules;returnHash=[],"display-html"==options.displayType&&($("#"+options.errorTargetElementId).text("").hide(),null!=options.errorFieldClass&&$("."+options.errorFieldClass).removeClass(options.errorFieldClass));for(var i=0;i=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!isValidEmail(form[fieldName].value)&&!processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==options.displayType){var success=displayHTMLErrors(form,returnHash);if(!success)return!1}return"function"==typeof options.onCompleteHandler?options.onCompleteHandler():!0}}(jQuery); \ No newline at end of file +// rsv.js - Really Simple Validation +// v2.5.3, Nov 1 2014 +// https://github.com/benkeen/rsv +!function($){function processError(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(options.displayType){case"alert-one":alert(b),styleField(a,!0),c=!1;break;case"alert-all":case"display-html":returnHash.push([a,b])}return c}function displayHTMLErrors(a,b){for(var c=options.errorTextIntro+"

",d=0;d",styleField(b[d][0],0==d);return b.length>0?($("#"+options.errorTargetElementId).css("display","block"),$("#"+options.errorTargetElementId).html(c),!1):!0}function styleField(a,b){if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0}var options={},returnHash=[];$.fn.RSV=function(a){return options=$.extend({},$.fn.RSV.defaults,a),this.each(function(){$(this).bind("submit",{currForm:this,options:options},$(this).RSV.validate)})},$.fn.RSV.defaults={rules:[],displayType:"alert-all",errorFieldClass:null,errorTextIntro:"Please fix the following error(s) and resubmit:",errorJSItemBullet:"* ",errorHTMLItemBullet:"• ",errorTargetElementId:"rsvErrors",customErrorHandler:null,onCompleteHandler:null},$.fn.RSV.validate=function(event){options=event.data.options;var form=event.data.currForm,rules=options.rules;returnHash=[],"display-html"==options.displayType&&($("#"+options.errorTargetElementId).text("").hide(),null!=options.errorFieldClass&&$("."+options.errorFieldClass).removeClass(options.errorFieldClass));for(var i=0;i=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!isValidEmail(form[fieldName].value)&&!processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==options.displayType){var success=displayHTMLErrors(form,returnHash);if(!success)return!1}return"function"==typeof options.onCompleteHandler?options.onCompleteHandler():!0}}(jQuery); \ No newline at end of file diff --git a/prototype/prototype.rsv.js b/prototype/prototype.rsv.js index 8b9fafa..215f8d7 100644 --- a/prototype/prototype.rsv.js +++ b/prototype/prototype.rsv.js @@ -2,7 +2,7 @@ * rsv.js - Really Simple Validation * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * - * v2.5.1, Nov 14 2008 + * v2.5.3, Nov 1 2014 * * This powerful little script lets you add client-side validation to any webform with very little * work. It includes a number of pre-existing routines for common tasks like validating email @@ -546,9 +546,10 @@ RSV.prototype = { // if the user has defined a custom event handler, pass the information to it - if (typeof this.options.customErrorHandler == 'function') - { - return this.options.customErrorHandler(form, this.returnHash); + if (typeof this.options.customErrorHandler == 'function') { + if (!this.options.customErrorHandler(form, this.returnHash)) { + return false; + } } // if the user has chosen "alert-all" or "return-errors", perform the appropriate action diff --git a/prototype/prototype.rsv.min.js b/prototype/prototype.rsv.min.js index 46e640c..1685158 100644 --- a/prototype/prototype.rsv.min.js +++ b/prototype/prototype.rsv.min.js @@ -1 +1,4 @@ -var RSV=Class.create();RSV.prototype={options:{},returnHash:[],initialize:function(){var a=Object.extend({formID:"",rules:[],displayType:"alert-all",errorFieldClass:null,errorTextIntro:"Please fix the following error(s) and resubmit:",errorJSItemBullet:"* ",errorHTMLItemBullet:"• ",errorTargetElementId:"rsvErrors",onCompleteHandler:null,customErrorHandler:null},arguments[0]||{});return a.formID?($(a.formID)&&($(a.formID).onsubmit=this.validate.bind(this)),void(this.options=a)):(alert("The formID configuration setting is required."),!1)},validate:function(){var form=$(this.options.formID),rules=this.options.rules;this.returnHash=[];for(var i=0;i=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!this.processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!this.processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!this.isValidEmail(form[fieldName].value)&&!this.processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!this.isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!this.processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!this.processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!this.processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==this.options.displayType){var success=this.displayHTMLErrors(form,this.returnHash);if(!success)return!1}return"function"==typeof this.options.onCompleteHandler?this.options.onCompleteHandler():!0},processError:function(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(this.options.displayType){case"alert-one":alert(b),this.styleField(a,!0),c=!1;break;case"alert-all":case"display-html":this.returnHash.push([a,b])}return c},displayHTMLErrors:function(a,b){for(var c=this.options.errorTextIntro+"

",d=0;d",this.styleField(b[d][0],0==d);return b.length>0?($(this.options.errorTargetElementId).style.display="block",$(this.options.errorTargetElementId).innerHTML=c,!1):!0},styleField:function(a,b){if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0}}; \ No newline at end of file +// rsv.js - Really Simple Validation +// v2.5.3, Nov 1 2014 +// https://github.com/benkeen/rsv +var RSV=Class.create();RSV.prototype={options:{},returnHash:[],initialize:function(){var a=Object.extend({formID:"",rules:[],displayType:"alert-all",errorFieldClass:null,errorTextIntro:"Please fix the following error(s) and resubmit:",errorJSItemBullet:"* ",errorHTMLItemBullet:"• ",errorTargetElementId:"rsvErrors",onCompleteHandler:null,customErrorHandler:null},arguments[0]||{});return a.formID?($(a.formID)&&($(a.formID).onsubmit=this.validate.bind(this)),void(this.options=a)):(alert("The formID configuration setting is required."),!1)},validate:function(){var form=$(this.options.formID),rules=this.options.rules;this.returnHash=[];for(var i=0;i=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!this.processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!this.processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!this.isValidEmail(form[fieldName].value)&&!this.processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!this.isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!this.processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!this.processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||this.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!this.processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==this.options.displayType){var success=this.displayHTMLErrors(form,this.returnHash);if(!success)return!1}return"function"==typeof this.options.onCompleteHandler?this.options.onCompleteHandler():!0},processError:function(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(this.options.displayType){case"alert-one":alert(b),this.styleField(a,!0),c=!1;break;case"alert-all":case"display-html":this.returnHash.push([a,b])}return c},displayHTMLErrors:function(a,b){for(var c=this.options.errorTextIntro+"

",d=0;d",this.styleField(b[d][0],0==d);return b.length>0?($(this.options.errorTargetElementId).style.display="block",$(this.options.errorTargetElementId).innerHTML=c,!1):!0},styleField:function(a,b){if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0}}; \ No newline at end of file diff --git a/standalone/rsv.js b/standalone/rsv.js index d2c5e8b..fd70054 100644 --- a/standalone/rsv.js +++ b/standalone/rsv.js @@ -2,7 +2,7 @@ * rsv.js - Really Simple Validation * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * - * v2.5.2, Jan 30 2010 + * v2.5.3, Nov 1 2014 * * This powerful little script lets you add client-side validation to any webform with very little * work. It includes a number of pre-existing routines for common tasks like validating email @@ -13,9 +13,9 @@ * This script is written by Ben Keen with additional code contributed by Mihai Ionescu and Nathan * Howard. It is free to distribute, to re-write, spread on your toast - do what ever you want with it! */ - +{ if (typeof rsv == 'undefined') - rsv = {}; + rsv = {};} // SETTINGS @@ -529,8 +529,11 @@ rsv.validate = function(form, rules) // if the user has defined a custom event handler, pass the information to it - if (typeof rsv.customErrorHandler == 'function') - return rsv.customErrorHandler(form, rsv.returnHash); + if (typeof rsv.customErrorHandler == 'function') { + if (!rsv.customErrorHandler(form, rsv.returnHash)) { + return false; + } + } // if the user has chosen "alert-all" or "return-errors", perform the appropriate action else if (rsv.displayType == "alert-all") diff --git a/standalone/rsv.min.js b/standalone/rsv.min.js index 649193b..1c1bbb9 100644 --- a/standalone/rsv.min.js +++ b/standalone/rsv.min.js @@ -1 +1,4 @@ -"undefined"==typeof rsv&&(rsv={}),rsv.displayType="alert-all",rsv.errorFieldClass=null,rsv.errorTextIntro="Please fix the following error(s) and resubmit:",rsv.errorJSItemBullet="* ",rsv.errorHTMLItemBullet="• ",rsv.errorTargetElementId="rsvErrors",rsv.customErrorHandler=null,rsv.onCompleteHandler=null,rsv.validate=function(form,rules){rsv.returnHash=[];for(var i=0;i=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!rsv.processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!rsv.processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!rsv.isValidEmail(form[fieldName].value)&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!rsv.isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!rsv.processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==rsv.displayType){var success=rsv.displayHTMLErrors(form,rsv.returnHash);if(!success)return!1}return"function"==typeof rsv.onCompleteHandler?rsv.onCompleteHandler():!0},rsv.processError=function(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(rsv.displayType){case"alert-one":alert(b),rsv.styleField(a,!0),c=!1;break;case"alert-all":case"display-html":rsv.returnHash.push([a,b])}return c},rsv.displayHTMLErrors=function(a,b){for(var c=rsv.errorTextIntro+"

",d=0;d",rsv.styleField(b[d][0],0==d);return b.length>0?(document.getElementById(rsv.errorTargetElementId).style.display="block",document.getElementById(rsv.errorTargetElementId).innerHTML=c,!1):!0},rsv.styleField=function(a,b){if(rsv.errorFieldClass)if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0},rsv.addClassName=function(a,b,c){if(a.className){var d=a.className.split(" ");if(c)for(var e=b.toUpperCase(),f=0;f=/)?(comparison_rule="greater_than_or_equal",rule_string=lengthRequirements.replace("length>=","")):lengthRequirements.match(/length>/)?(comparison_rule="greater_than",rule_string=lengthRequirements.replace("length>","")):lengthRequirements.match(/length<=/)?(comparison_rule="less_than_or_equal",rule_string=lengthRequirements.replace("length<=","")):lengthRequirements.match(/length=parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value.length>parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value.length<=parseInt(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].value.lengthfieldCount[1])&&!rsv.processError(form[fieldName],errorMessage))return!1}else if(form[fieldName].value.length!=fieldCount[0]&&!rsv.processError(form[fieldName],errorMessage))return!1}break;case"valid_email":if(form[fieldName].value&&!rsv.isValidEmail(form[fieldName].value)&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"valid_date":var isLaterDate=!1;if("later_date"==date_flag?isLaterDate=!0:"any_date"==date_flag&&(isLaterDate=!1),!rsv.isValidDate(form[fieldName].value,form[fieldName2].value,form[fieldName3].value,isLaterDate)&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"same_as":if(form[fieldName].value!=form[fieldName2].value&&!rsv.processError(form[fieldName],errorMessage))return!1;break;case"range":switch(comparison_rule="",rule_string="",rangeRequirements.match(/range=/)?(comparison_rule="equal",rule_string=rangeRequirements.replace("range=","")):rangeRequirements.match(/range>=/)?(comparison_rule="greater_than_or_equal",rule_string=rangeRequirements.replace("range>=","")):rangeRequirements.match(/range>/)?(comparison_rule="greater_than",rule_string=rangeRequirements.replace("range>","")):rangeRequirements.match(/range<=/)?(comparison_rule="less_than_or_equal",rule_string=rangeRequirements.replace("range<=","")):rangeRequirements.match(/range=Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"greater_than":if(!(form[fieldName].value>Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than_or_equal":if(!(form[fieldName].value<=Number(rule_string)||rsv.processError(form[fieldName],errorMessage)))return!1;break;case"less_than":if(!(form[fieldName].valueNumber(rangeValues[1]))&&!rsv.processError(form[fieldName],errorMessage))return!1}break;case"function":if(custom_function=fieldName,eval("var result = "+custom_function+"()"),-1!=result.constructor.toString().indexOf("Array"))for(var j=0;j0)return alert(errorStr),!1}else if("display-html"==rsv.displayType){var success=rsv.displayHTMLErrors(form,rsv.returnHash);if(!success)return!1}return"function"==typeof rsv.onCompleteHandler?rsv.onCompleteHandler():!0},rsv.processError=function(a,b){b=b.replace(/%%C%%/gi,",");var c=!0;switch(rsv.displayType){case"alert-one":alert(b),rsv.styleField(a,!0),c=!1;break;case"alert-all":case"display-html":rsv.returnHash.push([a,b])}return c},rsv.displayHTMLErrors=function(a,b){for(var c=rsv.errorTextIntro+"

",d=0;d",rsv.styleField(b[d][0],0==d);return b.length>0?(document.getElementById(rsv.errorTargetElementId).style.display="block",document.getElementById(rsv.errorTargetElementId).innerHTML=c,!1):!0},rsv.styleField=function(a,b){if(rsv.errorFieldClass)if(void 0==a.type){b&&a[0].focus();for(var c=0;ca||a>12)return!1;if(0>c)return!1;if(1>b||b>e[a-1])return!1;if(d){var f=new Date,g=f.getMonth()+1,h=f.getDate(),i=f.getFullYear();1==String(g).length&&(g="0"+g),1==String(h).length&&(h="0"+h);var j=String(i)+String(g)+String(h);if(1==String(a).length&&(a="0"+a),1==String(b).length&&(b="0"+b),incomingDate=String(c)+String(a)+String(b),Number(j)>Number(incomingDate))return!1}return!0},rsv.addClassName=function(a,b,c){if(a.className){var d=a.className.split(" ");if(c)for(var e=b.toUpperCase(),f=0;f