/* * Inline Form Validation Engine, jQuery plugin * * Copyright(c) 2009, Cedric Dugas * http://www.position-relative.net * * Form validation engine witch allow custom regex rules to be added. * Licenced under the MIT Licence */ $(document).ready(function() { // SUCCESS AJAX CALL, replace "success: false," by: success : function() { callSuccessFunction() }, $("[class^=validate]").validationEngine({ success : false, failure : function() {} }) }); jQuery.fn.validationEngine = function(settings) { if($.validationEngineLanguage){ // IS THERE A LANGUAGE LOCALISATION ? allRules = $.validationEngineLanguage.allRules }else{ allRules = {"required":{ // Add your regex rules here, you can take telephone as an example "regex":"none", "alertText":"* This field is required", "alertTextCheckboxMultiple":"* Please select an option", "alertTextCheckboxe":"* This checkbox is required"}, "length":{ "regex":"none", "alertText":"*Between ", "alertText2":" and ", "alertText3": " characters allowed"}, "minCheckbox":{ "regex":"none", "alertText":"* Checks allowed Exceeded"}, "confirm":{ "regex":"none", "alertText":"* Your field is not matching"}, "telephone":{ "regex":"/^[0-9\-\(\)\ ]+$/", "alertText":"* Invalid phone number"}, "email":{ "regex":"/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/", "alertText":"* Invalid email address"}, "date":{ "regex":"/^[0-9]{4}\-\[0-9]{1,2}\-\[0-9]{1,2}$/", "alertText":"* Invalid date, must be in YYYY-MM-DD format"}, "datefr":{ "regex":"/^[0-9]{1,2}\\/\[0-9]{1,2}\\/\[0-9]{4}$/", "alertText":"* Date invalide, format DD/MM/YYYY requis"}, "onlyNumber":{ "regex":"/^[0-9\ ]+$/", "alertText":"* Numbers only"}, "noSpecialCaracters":{ "regex":"/^[0-9a-zA-Z]+$/", "alertText":"* No special caracters allowed"}, "onlyLetter":{ "regex":"/^[a-zA-Z\ \']+$/", "alertText":"* Letters only"} } } settings = jQuery.extend({ allrules:allRules, inlineValidation: true, success : false, failure : function() {} }, settings); $("form").bind("submit", function(caller){ // ON FORM SUBMIT, CONTROL AJAX FUNCTION IF SPECIFIED ON DOCUMENT READY if(submitValidation(this) == false){ if (settings.success){ settings.success && settings.success(); return false; } }else{ settings.failure && settings.failure(); return false; } }) if(settings.inlineValidation == true){ // Validating Inline ? $(this).not("[type=checkbox]").bind("blur", function(caller){loadValidation(this)}) $(this+"[type=checkbox]").bind("click", function(caller){loadValidation(this)}) } var buildPrompt = function(caller,promptText,showTriangle) { // ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR var divFormError = document.createElement('div') var formErrorContent = document.createElement('div') var arrow = document.createElement('div') $(divFormError).addClass("formError") $(divFormError).addClass($(caller).attr("id")) $(formErrorContent).addClass("formErrorContent") $(arrow).addClass("formErrorArrow") $("body").append(divFormError) $(divFormError).append(arrow) $(divFormError).append(formErrorContent) if(showTriangle == true){ // NO TRIANGLE ON MAX CHECKBOX AND RADIO $(arrow).html('
'); } $(formErrorContent).html(promptText) callerTopPosition = $(caller).offset().top; callerleftPosition = $(caller).offset().left; callerWidth = $(caller).width() callerHeight = $(caller).height() inputHeight = $(divFormError).height() callerleftPosition = callerleftPosition + callerWidth -30 callerTopPosition = callerTopPosition -inputHeight -10 $(divFormError).css({ top:callerTopPosition, left:callerleftPosition, opacity:0 }) $(divFormError).fadeTo("fast",0.8); }; var updatePromptText = function(caller,promptText) { // UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED updateThisPrompt = $(caller).attr("id") $("."+updateThisPrompt).find(".formErrorContent").html(promptText) callerTopPosition = $(caller).offset().top; inputHeight = $("."+updateThisPrompt).height() callerTopPosition = callerTopPosition -inputHeight -10 $("."+updateThisPrompt).animate({ top:callerTopPosition }); } var loadValidation = function(caller) { // GET VALIDATIONS TO BE EXECUTED rulesParsing = $(caller).attr('class'); rulesRegExp = /\[(.*)\]/; getRules = rulesRegExp.exec(rulesParsing); str = getRules[1] pattern = /\W+/; result= str.split(pattern); var validateCalll = validateCall(caller,result) return validateCalll }; var validateCall = function(caller,rules) { // EXECUTE VALIDATION REQUIRED BY THE USER FOR THIS FIELD var promptText ="" var prompt = $(caller).attr("id"); var caller = caller; var callerName = $(caller).attr("name"); isError = false; callerType = $(caller).attr("type"); for (i=0; i