/* * 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":"Please fill in the field!", "alertTextCheckboxMultiple":"* Пожалуйста, выберите опцию", "alertTextCheckboxe":"* Опция не выбрана"}, "length":{ "regex":"Нет", "alertText":"Must be ", "alertText2":" only 4 ", "alertText3": " symbols!"}, "minCheckbox":{ "regex":"none", "alertText":"Выбрано слишком много!"}, "confirm":{ "regex":"none", "alertText":"Поля не совпадают!"}, "telephone":{ "regex":"/^[0-9\-\(\)]+$/", "alertText":"Некорректный номер телефона!"}, "email":{ "regex":"/^[0-9a-z]+[0-9a-z\-\_\.]*@[0-9a-z]+[0-9a-z\-\_\.]*\.[a-z]{2,6}$/g", "alertText":"Wrong e-mail!"}, "onlyNumber":{ "regex":"/^([7]{1})([0-9]{3})+$/", "alertText":"Order Number is not correct!"}, "noSpecialCaracters":{ "regex":"/^[0-9a-zA-Zа-яА-Я]+$/", "alertText":"Специальные символы запрещены!"}, "onlyLetter":{ "regex":"/^[a-zA-Z]+$/", "alertText":"Onle letters!"} } } settings = jQuery.extend({ allrules:allRules, 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; } }) $(this).not("[type=checkbox]").bind("blur", function(caller){loadValidation(this)}) $(this+"[type=checkbox]").bind("click", function(caller){loadValidation(this)}) var buildPrompt = function(caller,promptText) { // 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("name")) $(formErrorContent).addClass("formErrorContent") $(arrow).addClass("formErrorArrow") $("body").append(divFormError) $(divFormError).append(arrow) $(divFormError).append(formErrorContent) $(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("name") $("."+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 FILED var promptText ="" var isError = false var prompt = $(caller).attr("name") var caller = caller for (i=0; i" } } if (callerType == "radio" || callerType == "checkbox" ){ callerName = $(caller).attr("name") if($("input[name="+callerName+"]:checked").size() == 0) { isError = true if($("input[name="+callerName+"]").size() ==1) { promptText += settings.allrules[rules[i]].alertTextCheckboxe+"
" }else{ promptText += settings.allrules[rules[i]].alertTextCheckboxMultiple+"
" } } } } function _customRegex(caller,rules,position){ // VALIDATE REGEX RULES customRule = rules[position+1] pattern = eval(settings.allrules[customRule].regex) if(!pattern.test($(caller).attr('value'))){ isError = true promptText += settings.allrules[customRule].alertText+"
" } } function _confirm(caller,rules,position){ // VALIDATE FIELD MATCH confirmField = rules[position+1] if($(caller).attr('value') != $("#"+confirmField).attr('value')){ isError = true promptText += settings.allrules["confirm"].alertText+"
" } } function _length(caller,rules,position){ // VALIDATE LENGTH startLength = eval(rules[position+1]) endLength = eval(rules[position+2]) feildLength = $(caller).attr('value').length if(feildLengthendLength){ isError = true promptText += settings.allrules["length"].alertText+settings.allrules["length"].alertText2+settings.allrules["length"].alertText3+"
" } } function _minCheckbox(caller,rules,position){ // VALIDATE CHECKBOX NUMBER nbCheck = eval(rules[position+1]) groupname = $(caller).attr("name") groupSize = $("input[name="+groupname+"]:checked").size() if(groupSize > nbCheck){ isError = true promptText += settings.allrules["minCheckbox"].alertText+"
" } } return(isError) ? isError : false; }; var closePrompt = function(caller) { // CLOSE PROMPT WHEN ERROR CORRECTED closingPrompt = $(caller).attr("name") $("."+closingPrompt).fadeTo("fast",0,function(){ $("."+closingPrompt).remove() }); }; var submitValidation = function(caller) { // FORM SUBMIT VALIDATION LOOPING INLINE VALIDATION var stopForm = false $(".formError").remove() var toValidateSize = $(caller).find("[class^=validate]").size() $(caller).find("[class^=validate]").each(function(){ var validationPass = loadValidation(this) return(validationPass) ? stopForm = true : ""; }); if(stopForm){ // GET IF THERE IS AN ERROR OR NOT FROM THIS VALIDATION FUNCTIONS destination = $(".formError:first").offset().top; $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100) return true; }else{ return false } }; };