jquery.validationEngine.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. /*
  2. * Inline Form Validation Engine 2.6.2, jQuery plugin
  3. *
  4. * Copyright(c) 2010, Cedric Dugas
  5. * http://www.position-absolute.com
  6. *
  7. * 2.0 Rewrite by Olivier Refalo
  8. * http://www.crionics.com
  9. *
  10. * Form validation engine allowing custom regex rules to be added.
  11. * Licensed under the MIT License
  12. */
  13. (function($) {
  14. "use strict";
  15. var methods = {
  16. /**
  17. * Kind of the constructor, called before any action
  18. * @param {Map} user options
  19. */
  20. init: function(options) {
  21. var form = this;
  22. if (!form.data('jqv') || form.data('jqv') == null ) {
  23. options = methods._saveOptions(form, options);
  24. // bind all formError elements to close on click
  25. $(document).on("click", ".formError", function() {
  26. $(this).fadeOut(150, function() {
  27. // remove prompt once invisible
  28. $(this).closest('.formError').remove();
  29. });
  30. });
  31. }
  32. return this;
  33. },
  34. /**
  35. * Attachs jQuery.validationEngine to form.submit and field.blur events
  36. * Takes an optional params: a list of options
  37. * ie. jQuery("#formID1").validationEngine('attach', {promptPosition : "centerRight"});
  38. */
  39. attach: function(userOptions) {
  40. var form = this;
  41. var options;
  42. if(userOptions)
  43. options = methods._saveOptions(form, userOptions);
  44. else
  45. options = form.data('jqv');
  46. options.validateAttribute = (form.find("[data-validation-engine*=validate]").length) ? "data-validation-engine" : "class";
  47. if (options.binded) {
  48. // delegate fields
  49. form.on(options.validationEventTrigger, "["+options.validateAttribute+"*=validate]:not([type=checkbox]):not([type=radio]):not(.datepicker)", methods._onFieldEvent);
  50. form.on("click", "["+options.validateAttribute+"*=validate][type=checkbox],["+options.validateAttribute+"*=validate][type=radio]", methods._onFieldEvent);
  51. form.on(options.validationEventTrigger,"["+options.validateAttribute+"*=validate][class*=datepicker]", {"delay": 300}, methods._onFieldEvent);
  52. }
  53. if (options.autoPositionUpdate) {
  54. $(window).bind("resize", {
  55. "noAnimation": true,
  56. "formElem": form
  57. }, methods.updatePromptsPosition);
  58. }
  59. form.on("click","a[data-validation-engine-skip], a[class*='validate-skip'], button[data-validation-engine-skip], button[class*='validate-skip'], input[data-validation-engine-skip], input[class*='validate-skip']", methods._submitButtonClick);
  60. form.removeData('jqv_submitButton');
  61. // bind form.submit
  62. form.on("submit", methods._onSubmitEvent);
  63. return this;
  64. },
  65. /**
  66. * Unregisters any bindings that may point to jQuery.validaitonEngine
  67. */
  68. detach: function() {
  69. var form = this;
  70. var options = form.data('jqv');
  71. // unbind fields
  72. form.off(options.validationEventTrigger, "["+options.validateAttribute+"*=validate]:not([type=checkbox]):not([type=radio]):not(.datepicker)", methods._onFieldEvent);
  73. form.off("click", "["+options.validateAttribute+"*=validate][type=checkbox],["+options.validateAttribute+"*=validate][type=radio]", methods._onFieldEvent);
  74. form.off(options.validationEventTrigger,"["+options.validateAttribute+"*=validate][class*=datepicker]", methods._onFieldEvent);
  75. // unbind form.submit
  76. form.off("submit", methods._onSubmitEvent);
  77. form.removeData('jqv');
  78. form.off("click", "a[data-validation-engine-skip], a[class*='validate-skip'], button[data-validation-engine-skip], button[class*='validate-skip'], input[data-validation-engine-skip], input[class*='validate-skip']", methods._submitButtonClick);
  79. form.removeData('jqv_submitButton');
  80. if (options.autoPositionUpdate)
  81. $(window).off("resize", methods.updatePromptsPosition);
  82. return this;
  83. },
  84. /**
  85. * Validates either a form or a list of fields, shows prompts accordingly.
  86. * Note: There is no ajax form validation with this method, only field ajax validation are evaluated
  87. *
  88. * @return true if the form validates, false if it fails
  89. */
  90. validate: function(userOptions) {
  91. var element = $(this);
  92. var valid = null;
  93. var options;
  94. if (element.is("form") || element.hasClass("validationEngineContainer")) {
  95. if (element.hasClass('validating')) {
  96. // form is already validating.
  97. // Should abort old validation and start new one. I don't know how to implement it.
  98. return false;
  99. } else {
  100. element.addClass('validating');
  101. if(userOptions)
  102. options = methods._saveOptions(element, userOptions);
  103. else
  104. options = element.data('jqv');
  105. var valid = methods._validateFields(this);
  106. // If the form doesn't validate, clear the 'validating' class before the user has a chance to submit again
  107. setTimeout(function(){
  108. element.removeClass('validating');
  109. }, 100);
  110. if (valid && options.onSuccess) {
  111. options.onSuccess();
  112. } else if (!valid && options.onFailure) {
  113. options.onFailure();
  114. }
  115. }
  116. } else if (element.is('form') || element.hasClass('validationEngineContainer')) {
  117. element.removeClass('validating');
  118. } else {
  119. // field validation
  120. var form = element.closest('form, .validationEngineContainer');
  121. options = (form.data('jqv')) ? form.data('jqv') : $.validationEngine.defaults;
  122. valid = methods._validateField(element, options);
  123. if (valid && options.onFieldSuccess)
  124. options.onFieldSuccess();
  125. else if (options.onFieldFailure && options.InvalidFields.length > 0) {
  126. options.onFieldFailure();
  127. }
  128. return !valid;
  129. }
  130. if(options.onValidationComplete) {
  131. // !! ensures that an undefined return is interpreted as return false but allows a onValidationComplete() to possibly return true and have form continue processing
  132. return !!options.onValidationComplete(form, valid);
  133. }
  134. return valid;
  135. },
  136. /**
  137. * Redraw prompts position, useful when you change the DOM state when validating
  138. */
  139. updatePromptsPosition: function(event) {
  140. if (event && this == window) {
  141. var form = event.data.formElem;
  142. var noAnimation = event.data.noAnimation;
  143. }
  144. else
  145. var form = $(this.closest('form, .validationEngineContainer'));
  146. var options = form.data('jqv');
  147. // No option, take default one
  148. if (!options)
  149. options = methods._saveOptions(form, options);
  150. form.find('['+options.validateAttribute+'*=validate]').not(":disabled").each(function(){
  151. var field = $(this);
  152. if (options.prettySelect && field.is(":hidden"))
  153. field = form.find("#" + options.usePrefix + field.attr('id') + options.useSuffix);
  154. var prompt = methods._getPrompt(field);
  155. var promptText = $(prompt).find(".formErrorContent").html();
  156. if(prompt)
  157. methods._updatePrompt(field, $(prompt), promptText, undefined, false, options, noAnimation);
  158. });
  159. return this;
  160. },
  161. /**
  162. * Displays a prompt on a element.
  163. * Note that the element needs an id!
  164. *
  165. * @param {String} promptText html text to display type
  166. * @param {String} type the type of bubble: 'pass' (green), 'load' (black) anything else (red)
  167. * @param {String} possible values topLeft, topRight, bottomLeft, centerRight, bottomRight
  168. */
  169. showPrompt: function(promptText, type, promptPosition, showArrow) {
  170. var form = this.closest('form, .validationEngineContainer');
  171. var options = form.data('jqv');
  172. // No option, take default one
  173. if(!options)
  174. options = methods._saveOptions(this, options);
  175. if(promptPosition)
  176. options.promptPosition=promptPosition;
  177. options.showArrow = showArrow==true;
  178. methods._showPrompt(this, promptText, type, false, options);
  179. return this;
  180. },
  181. /**
  182. * Closes form error prompts, CAN be invidual
  183. */
  184. hide: function() {
  185. var form = $(this).closest('form, .validationEngineContainer');
  186. var options = form.data('jqv');
  187. // No option, take default one
  188. if (!options)
  189. options = methods._saveOptions(form, options);
  190. var fadeDuration = (options && options.fadeDuration) ? options.fadeDuration : 0.3;
  191. var closingtag;
  192. if(form.is("form") || form.hasClass("validationEngineContainer")) {
  193. closingtag = "parentForm"+methods._getClassName($(form).attr("id"));
  194. } else {
  195. closingtag = methods._getClassName($(form).attr("id")) +"formError";
  196. }
  197. $('.'+closingtag).fadeTo(fadeDuration, 0, function() {
  198. $(this).closest('.formError').remove();
  199. });
  200. return this;
  201. },
  202. /**
  203. * Closes all error prompts on the page
  204. */
  205. hideAll: function() {
  206. var form = this;
  207. var options = form.data('jqv');
  208. var duration = options ? options.fadeDuration:300;
  209. $('.formError').fadeTo(duration, 0, function() {
  210. $(this).closest('.formError').remove();
  211. });
  212. return this;
  213. },
  214. /**
  215. * Typically called when user exists a field using tab or a mouse click, triggers a field
  216. * validation
  217. */
  218. _onFieldEvent: function(event) {
  219. var field = $(this);
  220. var form = field.closest('form, .validationEngineContainer');
  221. var options = form.data('jqv');
  222. // No option, take default one
  223. if (!options)
  224. options = methods._saveOptions(form, options);
  225. options.eventTrigger = "field";
  226. if (options.notEmpty == true){
  227. if(field.val().length > 0){
  228. // validate the current field
  229. window.setTimeout(function() {
  230. methods._validateField(field, options);
  231. }, (event.data) ? event.data.delay : 0);
  232. }
  233. }else{
  234. // validate the current field
  235. window.setTimeout(function() {
  236. methods._validateField(field, options);
  237. }, (event.data) ? event.data.delay : 0);
  238. }
  239. },
  240. /**
  241. * Called when the form is submited, shows prompts accordingly
  242. *
  243. * @param {jqObject}
  244. * form
  245. * @return false if form submission needs to be cancelled
  246. */
  247. _onSubmitEvent: function() {
  248. var form = $(this);
  249. var options = form.data('jqv');
  250. //check if it is trigger from skipped button
  251. if (form.data("jqv_submitButton")){
  252. var submitButton = $("#" + form.data("jqv_submitButton"));
  253. if (submitButton){
  254. if (submitButton.length > 0){
  255. if (submitButton.hasClass("validate-skip") || submitButton.attr("data-validation-engine-skip") == "true")
  256. return true;
  257. }
  258. }
  259. }
  260. options.eventTrigger = "submit";
  261. // validate each field
  262. // (- skip field ajax validation, not necessary IF we will perform an ajax form validation)
  263. var r=methods._validateFields(form);
  264. if (r && options.ajaxFormValidation) {
  265. methods._validateFormWithAjax(form, options);
  266. // cancel form auto-submission - process with async call onAjaxFormComplete
  267. return false;
  268. }
  269. if(options.onValidationComplete) {
  270. // !! ensures that an undefined return is interpreted as return false but allows a onValidationComplete() to possibly return true and have form continue processing
  271. return !!options.onValidationComplete(form, r);
  272. }
  273. return r;
  274. },
  275. /**
  276. * Return true if the ajax field validations passed so far
  277. * @param {Object} options
  278. * @return true, is all ajax validation passed so far (remember ajax is async)
  279. */
  280. _checkAjaxStatus: function(options) {
  281. var status = true;
  282. $.each(options.ajaxValidCache, function(key, value) {
  283. if (!value) {
  284. status = false;
  285. // break the each
  286. return false;
  287. }
  288. });
  289. return status;
  290. },
  291. /**
  292. * Return true if the ajax field is validated
  293. * @param {String} fieldid
  294. * @param {Object} options
  295. * @return true, if validation passed, false if false or doesn't exist
  296. */
  297. _checkAjaxFieldStatus: function(fieldid, options) {
  298. return options.ajaxValidCache[fieldid] == true;
  299. },
  300. /**
  301. * Validates form fields, shows prompts accordingly
  302. *
  303. * @param {jqObject}
  304. * form
  305. * @param {skipAjaxFieldValidation}
  306. * boolean - when set to true, ajax field validation is skipped, typically used when the submit button is clicked
  307. *
  308. * @return true if form is valid, false if not, undefined if ajax form validation is done
  309. */
  310. _validateFields: function(form) {
  311. var options = form.data('jqv');
  312. // this variable is set to true if an error is found
  313. var errorFound = false;
  314. // Trigger hook, start validation
  315. form.trigger("jqv.form.validating");
  316. // first, evaluate status of non ajax fields
  317. var first_err=null;
  318. form.find('['+options.validateAttribute+'*=validate]').not(":disabled").each( function() {
  319. var field = $(this);
  320. var names = [];
  321. if ($.inArray(field.attr('name'), names) < 0) {
  322. errorFound |= methods._validateField(field, options);
  323. if (errorFound && first_err==null)
  324. if (field.is(":hidden") && options.prettySelect)
  325. first_err = field = form.find("#" + options.usePrefix + methods._jqSelector(field.attr('id')) + options.useSuffix);
  326. else {
  327. //Check if we need to adjust what element to show the prompt on
  328. //and and such scroll to instead
  329. if(field.data('jqv-prompt-at') instanceof jQuery ){
  330. field = field.data('jqv-prompt-at');
  331. } else if(field.data('jqv-prompt-at')) {
  332. field = $(field.data('jqv-prompt-at'));
  333. }
  334. first_err=field;
  335. }
  336. if (options.doNotShowAllErrosOnSubmit)
  337. return false;
  338. names.push(field.attr('name'));
  339. //if option set, stop checking validation rules after one error is found
  340. if(options.showOneMessage == true && errorFound){
  341. return false;
  342. }
  343. }
  344. });
  345. // second, check to see if all ajax calls completed ok
  346. // errorFound |= !methods._checkAjaxStatus(options);
  347. // third, check status and scroll the container accordingly
  348. form.trigger("jqv.form.result", [errorFound]);
  349. if (errorFound) {
  350. if (options.scroll) {
  351. var destination=first_err.offset().top;
  352. var fixleft = first_err.offset().left;
  353. //prompt positioning adjustment support. Usage: positionType:Xshift,Yshift (for ex.: bottomLeft:+20 or bottomLeft:-20,+10)
  354. var positionType=options.promptPosition;
  355. if (typeof(positionType)=='string' && positionType.indexOf(":")!=-1)
  356. positionType=positionType.substring(0,positionType.indexOf(":"));
  357. if (positionType!="bottomRight" && positionType!="bottomLeft") {
  358. var prompt_err= methods._getPrompt(first_err);
  359. if (prompt_err) {
  360. destination=prompt_err.offset().top;
  361. }
  362. }
  363. // Offset the amount the page scrolls by an amount in px to accomodate fixed elements at top of page
  364. if (options.scrollOffset) {
  365. destination -= options.scrollOffset;
  366. }
  367. // get the position of the first error, there should be at least one, no need to check this
  368. //var destination = form.find(".formError:not('.greenPopup'):first").offset().top;
  369. if (options.isOverflown) {
  370. var overflowDIV = $(options.overflownDIV);
  371. if(!overflowDIV.length) return false;
  372. var scrollContainerScroll = overflowDIV.scrollTop();
  373. var scrollContainerPos = -parseInt(overflowDIV.offset().top);
  374. destination += scrollContainerScroll + scrollContainerPos - 5;
  375. var scrollContainer = $(options.overflownDIV).filter(":not(:animated)");
  376. scrollContainer.animate({ scrollTop: destination }, 1100, function(){
  377. if(options.focusFirstField) first_err.focus();
  378. });
  379. } else {
  380. $("html, body").animate({
  381. scrollTop: destination
  382. }, 1100, function(){
  383. if(options.focusFirstField) first_err.focus();
  384. });
  385. $("html, body").animate({scrollLeft: fixleft},1100)
  386. }
  387. } else if(options.focusFirstField)
  388. first_err.focus();
  389. return false;
  390. }
  391. return true;
  392. },
  393. /**
  394. * This method is called to perform an ajax form validation.
  395. * During this process all the (field, value) pairs are sent to the server which returns a list of invalid fields or true
  396. *
  397. * @param {jqObject} form
  398. * @param {Map} options
  399. */
  400. _validateFormWithAjax: function(form, options) {
  401. var data = form.serialize();
  402. var type = (options.ajaxFormValidationMethod) ? options.ajaxFormValidationMethod : "GET";
  403. var url = (options.ajaxFormValidationURL) ? options.ajaxFormValidationURL : form.attr("action");
  404. var dataType = (options.dataType) ? options.dataType : "json";
  405. $.ajax({
  406. type: type,
  407. url: url,
  408. cache: false,
  409. dataType: dataType,
  410. data: data,
  411. form: form,
  412. methods: methods,
  413. options: options,
  414. beforeSend: function() {
  415. return options.onBeforeAjaxFormValidation(form, options);
  416. },
  417. error: function(data, transport) {
  418. if (options.onFailure) {
  419. options.onFailure(data, transport);
  420. } else {
  421. methods._ajaxError(data, transport);
  422. }
  423. },
  424. success: function(json) {
  425. if ((dataType == "json") && (json !== true)) {
  426. // getting to this case doesn't necessary means that the form is invalid
  427. // the server may return green or closing prompt actions
  428. // this flag helps figuring it out
  429. var errorInForm=false;
  430. for (var i = 0; i < json.length; i++) {
  431. var value = json[i];
  432. var errorFieldId = value[0];
  433. var errorField = $($("#" + errorFieldId)[0]);
  434. // make sure we found the element
  435. if (errorField.length == 1) {
  436. // promptText or selector
  437. var msg = value[2];
  438. // if the field is valid
  439. if (value[1] == true) {
  440. if (msg == "" || !msg){
  441. // if for some reason, status==true and error="", just close the prompt
  442. methods._closePrompt(errorField);
  443. } else {
  444. // the field is valid, but we are displaying a green prompt
  445. if (options.allrules[msg]) {
  446. var txt = options.allrules[msg].alertTextOk;
  447. if (txt)
  448. msg = txt;
  449. }
  450. if (options.showPrompts) methods._showPrompt(errorField, msg, "pass", false, options, true);
  451. }
  452. } else {
  453. // the field is invalid, show the red error prompt
  454. errorInForm|=true;
  455. if (options.allrules[msg]) {
  456. var txt = options.allrules[msg].alertText;
  457. if (txt)
  458. msg = txt;
  459. }
  460. if(options.showPrompts) methods._showPrompt(errorField, msg, "", false, options, true);
  461. }
  462. }
  463. }
  464. options.onAjaxFormComplete(!errorInForm, form, json, options);
  465. } else
  466. options.onAjaxFormComplete(true, form, json, options);
  467. }
  468. });
  469. },
  470. /**
  471. * Validates field, shows prompts accordingly
  472. *
  473. * @param {jqObject}
  474. * field
  475. * @param {Array[String]}
  476. * field's validation rules
  477. * @param {Map}
  478. * user options
  479. * @return false if field is valid (It is inversed for *fields*, it return false on validate and true on errors.)
  480. */
  481. _validateField: function(field, options, skipAjaxValidation) {
  482. if (!field.attr("id")) {
  483. field.attr("id", "form-validation-field-" + $.validationEngine.fieldIdCounter);
  484. ++$.validationEngine.fieldIdCounter;
  485. }
  486. if(field.hasClass(options.ignoreFieldsWithClass))
  487. return false;
  488. if (!options.validateNonVisibleFields && (field.is(":hidden") && !options.prettySelect || field.parent().is(":hidden")))
  489. return false;
  490. var rulesParsing = field.attr(options.validateAttribute);
  491. var getRules = /validate\[(.*)\]/.exec(rulesParsing);
  492. if (!getRules)
  493. return false;
  494. var str = getRules[1];
  495. var rules = str.split(/\[|,|\]/);
  496. // true if we ran the ajax validation, tells the logic to stop messing with prompts
  497. var isAjaxValidator = false;
  498. var fieldName = field.attr("name");
  499. var promptText = "";
  500. var promptType = "";
  501. var required = false;
  502. var limitErrors = false;
  503. options.isError = false;
  504. options.showArrow = options.showArrow ==true;
  505. // If the programmer wants to limit the amount of error messages per field,
  506. if (options.maxErrorsPerField > 0) {
  507. limitErrors = true;
  508. }
  509. var form = $(field.closest("form, .validationEngineContainer"));
  510. // Fix for adding spaces in the rules
  511. for (var i = 0; i < rules.length; i++) {
  512. rules[i] = rules[i].toString().replace(" ", "");//.toString to worked on IE8
  513. // Remove any parsing errors
  514. if (rules[i] === '') {
  515. delete rules[i];
  516. }
  517. }
  518. for (var i = 0, field_errors = 0; i < rules.length; i++) {
  519. // If we are limiting errors, and have hit the max, break
  520. if (limitErrors && field_errors >= options.maxErrorsPerField) {
  521. // If we haven't hit a required yet, check to see if there is one in the validation rules for this
  522. // field and that it's index is greater or equal to our current index
  523. if (!required) {
  524. var have_required = $.inArray('required', rules);
  525. required = (have_required != -1 && have_required >= i);
  526. }
  527. break;
  528. }
  529. var errorMsg = undefined;
  530. switch (rules[i]) {
  531. case "required":
  532. required = true;
  533. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._required);
  534. break;
  535. case "custom":
  536. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._custom);
  537. break;
  538. case "groupRequired":
  539. // Check is its the first of group, if not, reload validation with first field
  540. // AND continue normal validation on present field
  541. var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";
  542. var firstOfGroup = form.find(classGroup).eq(0);
  543. if(firstOfGroup[0] != field[0]){
  544. methods._validateField(firstOfGroup, options, skipAjaxValidation);
  545. options.showArrow = true;
  546. }
  547. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._groupRequired);
  548. if(errorMsg) required = true;
  549. options.showArrow = false;
  550. break;
  551. case "ajax":
  552. // AJAX defaults to returning it's loading message
  553. errorMsg = methods._ajax(field, rules, i, options);
  554. if (errorMsg) {
  555. promptType = "load";
  556. }
  557. break;
  558. case "minSize":
  559. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._minSize);
  560. break;
  561. case "maxSize":
  562. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._maxSize);
  563. break;
  564. case "min":
  565. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._min);
  566. break;
  567. case "max":
  568. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._max);
  569. break;
  570. case "past":
  571. errorMsg = methods._getErrorMessage(form, field,rules[i], rules, i, options, methods._past);
  572. break;
  573. case "future":
  574. errorMsg = methods._getErrorMessage(form, field,rules[i], rules, i, options, methods._future);
  575. break;
  576. case "dateRange":
  577. var classGroup = "["+options.validateAttribute+"*=" + rules[i + 1] + "]";
  578. options.firstOfGroup = form.find(classGroup).eq(0);
  579. options.secondOfGroup = form.find(classGroup).eq(1);
  580. //if one entry out of the pair has value then proceed to run through validation
  581. if (options.firstOfGroup[0].value || options.secondOfGroup[0].value) {
  582. errorMsg = methods._getErrorMessage(form, field,rules[i], rules, i, options, methods._dateRange);
  583. }
  584. if (errorMsg) required = true;
  585. options.showArrow = false;
  586. break;
  587. case "dateTimeRange":
  588. var classGroup = "["+options.validateAttribute+"*=" + rules[i + 1] + "]";
  589. options.firstOfGroup = form.find(classGroup).eq(0);
  590. options.secondOfGroup = form.find(classGroup).eq(1);
  591. //if one entry out of the pair has value then proceed to run through validation
  592. if (options.firstOfGroup[0].value || options.secondOfGroup[0].value) {
  593. errorMsg = methods._getErrorMessage(form, field,rules[i], rules, i, options, methods._dateTimeRange);
  594. }
  595. if (errorMsg) required = true;
  596. options.showArrow = false;
  597. break;
  598. case "maxCheckbox":
  599. field = $(form.find("input[name='" + fieldName + "']"));
  600. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._maxCheckbox);
  601. break;
  602. case "minCheckbox":
  603. field = $(form.find("input[name='" + fieldName + "']"));
  604. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._minCheckbox);
  605. break;
  606. case "equals":
  607. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._equals);
  608. break;
  609. case "funcCall":
  610. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._funcCall);
  611. break;
  612. case "creditCard":
  613. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._creditCard);
  614. break;
  615. case "condRequired":
  616. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._condRequired);
  617. if (errorMsg !== undefined) {
  618. required = true;
  619. }
  620. break;
  621. case "funcCallRequired":
  622. errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._funcCallRequired);
  623. if (errorMsg !== undefined) {
  624. required = true;
  625. }
  626. break;
  627. default:
  628. }
  629. var end_validation = false;
  630. // If we were passed back an message object, check what the status was to determine what to do
  631. if (typeof errorMsg == "object") {
  632. switch (errorMsg.status) {
  633. case "_break":
  634. end_validation = true;
  635. break;
  636. // If we have an error message, set errorMsg to the error message
  637. case "_error":
  638. errorMsg = errorMsg.message;
  639. break;
  640. // If we want to throw an error, but not show a prompt, return early with true
  641. case "_error_no_prompt":
  642. return true;
  643. break;
  644. // Anything else we continue on
  645. default:
  646. break;
  647. }
  648. }
  649. //funcCallRequired, first in rules, and has error, skip anything else
  650. if( i==0 && str.indexOf('funcCallRequired')==0 && errorMsg !== undefined ){
  651. if(promptText != '') {
  652. promptText += "<br/>";
  653. }
  654. promptText += errorMsg;
  655. options.isError=true;
  656. field_errors++;
  657. end_validation=true;
  658. }
  659. // If it has been specified that validation should end now, break
  660. if (end_validation) {
  661. break;
  662. }
  663. // If we have a string, that means that we have an error, so add it to the error message.
  664. if (typeof errorMsg == 'string') {
  665. if(promptText != '') {
  666. promptText += "<br/>";
  667. }
  668. promptText += errorMsg;
  669. options.isError = true;
  670. field_errors++;
  671. }
  672. }
  673. // If the rules required is not added, an empty field is not validated
  674. //the 3rd condition is added so that even empty password fields should be equal
  675. //otherwise if one is filled and another left empty, the "equal" condition would fail
  676. //which does not make any sense
  677. if(!required && !(field.val()) && field.val().length < 1 && $.inArray('equals', rules) < 0) options.isError = false;
  678. // Hack for radio/checkbox group button, the validation go into the
  679. // first radio/checkbox of the group
  680. var fieldType = field.prop("type");
  681. var positionType=field.data("promptPosition") || options.promptPosition;
  682. if ((fieldType == "radio" || fieldType == "checkbox") && form.find("input[name='" + fieldName + "']").length > 1) {
  683. if(positionType === 'inline') {
  684. field = $(form.find("input[name='" + fieldName + "'][type!=hidden]:last"));
  685. } else {
  686. field = $(form.find("input[name='" + fieldName + "'][type!=hidden]:first"));
  687. }
  688. options.showArrow = options.showArrowOnRadioAndCheckbox;
  689. }
  690. if(field.is(":hidden") && options.prettySelect) {
  691. field = form.find("#" + options.usePrefix + methods._jqSelector(field.attr('id')) + options.useSuffix);
  692. }
  693. if (options.isError && options.showPrompts){
  694. methods._showPrompt(field, promptText, promptType, false, options);
  695. }else{
  696. if (!isAjaxValidator) methods._closePrompt(field);
  697. }
  698. if (!isAjaxValidator) {
  699. field.trigger("jqv.field.result", [field, options.isError, promptText]);
  700. }
  701. /* Record error */
  702. var errindex = $.inArray(field[0], options.InvalidFields);
  703. if (errindex == -1) {
  704. if (options.isError)
  705. options.InvalidFields.push(field[0]);
  706. } else if (!options.isError) {
  707. options.InvalidFields.splice(errindex, 1);
  708. }
  709. methods._handleStatusCssClasses(field, options);
  710. /* run callback function for each field */
  711. if (options.isError && options.onFieldFailure)
  712. options.onFieldFailure(field);
  713. if (!options.isError && options.onFieldSuccess)
  714. options.onFieldSuccess(field);
  715. return options.isError;
  716. },
  717. /**
  718. * Handling css classes of fields indicating result of validation
  719. *
  720. * @param {jqObject}
  721. * field
  722. * @param {Array[String]}
  723. * field's validation rules
  724. * @private
  725. */
  726. _handleStatusCssClasses: function(field, options) {
  727. /* remove all classes */
  728. if(options.addSuccessCssClassToField)
  729. field.removeClass(options.addSuccessCssClassToField);
  730. if(options.addFailureCssClassToField)
  731. field.removeClass(options.addFailureCssClassToField);
  732. /* Add classes */
  733. if (options.addSuccessCssClassToField && !options.isError)
  734. field.addClass(options.addSuccessCssClassToField);
  735. if (options.addFailureCssClassToField && options.isError)
  736. field.addClass(options.addFailureCssClassToField);
  737. },
  738. /********************
  739. * _getErrorMessage
  740. *
  741. * @param form
  742. * @param field
  743. * @param rule
  744. * @param rules
  745. * @param i
  746. * @param options
  747. * @param originalValidationMethod
  748. * @return {*}
  749. * @private
  750. */
  751. _getErrorMessage:function (form, field, rule, rules, i, options, originalValidationMethod) {
  752. // If we are using the custon validation type, build the index for the rule.
  753. // Otherwise if we are doing a function call, make the call and return the object
  754. // that is passed back.
  755. var rule_index = jQuery.inArray(rule, rules);
  756. if (rule === "custom" || rule === "funcCall" || rule === "funcCallRequired") {
  757. var custom_validation_type = rules[rule_index + 1];
  758. rule = rule + "[" + custom_validation_type + "]";
  759. // Delete the rule from the rules array so that it doesn't try to call the
  760. // same rule over again
  761. delete(rules[rule_index]);
  762. }
  763. // Change the rule to the composite rule, if it was different from the original
  764. var alteredRule = rule;
  765. var element_classes = (field.attr("data-validation-engine")) ? field.attr("data-validation-engine") : field.attr("class");
  766. var element_classes_array = element_classes.split(" ");
  767. // Call the original validation method. If we are dealing with dates or checkboxes, also pass the form
  768. var errorMsg;
  769. if (rule == "future" || rule == "past" || rule == "maxCheckbox" || rule == "minCheckbox") {
  770. errorMsg = originalValidationMethod(form, field, rules, i, options);
  771. } else {
  772. errorMsg = originalValidationMethod(field, rules, i, options);
  773. }
  774. // If the original validation method returned an error and we have a custom error message,
  775. // return the custom message instead. Otherwise return the original error message.
  776. if (errorMsg != undefined) {
  777. var custom_message = methods._getCustomErrorMessage($(field), element_classes_array, alteredRule, options);
  778. if (custom_message) errorMsg = custom_message;
  779. }
  780. return errorMsg;
  781. },
  782. _getCustomErrorMessage:function (field, classes, rule, options) {
  783. var custom_message = false;
  784. var validityProp = /^custom\[.*\]$/.test(rule) ? methods._validityProp["custom"] : methods._validityProp[rule];
  785. // If there is a validityProp for this rule, check to see if the field has an attribute for it
  786. if (validityProp != undefined) {
  787. custom_message = field.attr("data-errormessage-"+validityProp);
  788. // If there was an error message for it, return the message
  789. if (custom_message != undefined)
  790. return custom_message;
  791. }
  792. custom_message = field.attr("data-errormessage");
  793. // If there is an inline custom error message, return it
  794. if (custom_message != undefined)
  795. return custom_message;
  796. var id = '#' + field.attr("id");
  797. // If we have custom messages for the element's id, get the message for the rule from the id.
  798. // Otherwise, if we have custom messages for the element's classes, use the first class message we find instead.
  799. if (typeof options.custom_error_messages[id] != "undefined" &&
  800. typeof options.custom_error_messages[id][rule] != "undefined" ) {
  801. custom_message = options.custom_error_messages[id][rule]['message'];
  802. } else if (classes.length > 0) {
  803. for (var i = 0; i < classes.length && classes.length > 0; i++) {
  804. var element_class = "." + classes[i];
  805. if (typeof options.custom_error_messages[element_class] != "undefined" &&
  806. typeof options.custom_error_messages[element_class][rule] != "undefined") {
  807. custom_message = options.custom_error_messages[element_class][rule]['message'];
  808. break;
  809. }
  810. }
  811. }
  812. if (!custom_message &&
  813. typeof options.custom_error_messages[rule] != "undefined" &&
  814. typeof options.custom_error_messages[rule]['message'] != "undefined"){
  815. custom_message = options.custom_error_messages[rule]['message'];
  816. }
  817. return custom_message;
  818. },
  819. _validityProp: {
  820. "required": "value-missing",
  821. "custom": "custom-error",
  822. "groupRequired": "value-missing",
  823. "ajax": "custom-error",
  824. "minSize": "range-underflow",
  825. "maxSize": "range-overflow",
  826. "min": "range-underflow",
  827. "max": "range-overflow",
  828. "past": "type-mismatch",
  829. "future": "type-mismatch",
  830. "dateRange": "type-mismatch",
  831. "dateTimeRange": "type-mismatch",
  832. "maxCheckbox": "range-overflow",
  833. "minCheckbox": "range-underflow",
  834. "equals": "pattern-mismatch",
  835. "funcCall": "custom-error",
  836. "funcCallRequired": "custom-error",
  837. "creditCard": "pattern-mismatch",
  838. "condRequired": "value-missing"
  839. },
  840. /**
  841. * Required validation
  842. *
  843. * @param {jqObject} field
  844. * @param {Array[String]} rules
  845. * @param {int} i rules index
  846. * @param {Map}
  847. * user options
  848. * @param {bool} condRequired flag when method is used for internal purpose in condRequired check
  849. * @return an error string if validation failed
  850. */
  851. _required: function(field, rules, i, options, condRequired) {
  852. switch (field.prop("type")) {
  853. case "radio":
  854. case "checkbox":
  855. // new validation style to only check dependent field
  856. if (condRequired) {
  857. if (!field.prop('checked')) {
  858. return options.allrules[rules[i]].alertTextCheckboxMultiple;
  859. }
  860. break;
  861. }
  862. // old validation style
  863. var form = field.closest("form, .validationEngineContainer");
  864. var name = field.attr("name");
  865. if (form.find("input[name='" + name + "']:checked").length == 0) {
  866. if (form.find("input[name='" + name + "']:visible").length == 1)
  867. return options.allrules[rules[i]].alertTextCheckboxe;
  868. else
  869. return options.allrules[rules[i]].alertTextCheckboxMultiple;
  870. }
  871. break;
  872. case "text":
  873. case "password":
  874. case "textarea":
  875. case "file":
  876. case "select-one":
  877. case "select-multiple":
  878. default:
  879. var field_val = $.trim( field.val() );
  880. var dv_placeholder = $.trim( field.attr("data-validation-placeholder") );
  881. var placeholder = $.trim( field.attr("placeholder") );
  882. if (
  883. ( !field_val )
  884. || ( dv_placeholder && field_val == dv_placeholder )
  885. || ( placeholder && field_val == placeholder )
  886. ) {
  887. return options.allrules[rules[i]].alertText;
  888. }
  889. break;
  890. }
  891. },
  892. /**
  893. * Validate that 1 from the group field is required
  894. *
  895. * @param {jqObject} field
  896. * @param {Array[String]} rules
  897. * @param {int} i rules index
  898. * @param {Map}
  899. * user options
  900. * @return an error string if validation failed
  901. */
  902. _groupRequired: function(field, rules, i, options) {
  903. var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";
  904. var isValid = false;
  905. // Check all fields from the group
  906. field.closest("form, .validationEngineContainer").find(classGroup).each(function(){
  907. if(!methods._required($(this), rules, i, options)){
  908. isValid = true;
  909. return false;
  910. }
  911. });
  912. if(!isValid) {
  913. return options.allrules[rules[i]].alertText;
  914. }
  915. },
  916. /**
  917. * Validate rules
  918. *
  919. * @param {jqObject} field
  920. * @param {Array[String]} rules
  921. * @param {int} i rules index
  922. * @param {Map}
  923. * user options
  924. * @return an error string if validation failed
  925. */
  926. _custom: function(field, rules, i, options) {
  927. var customRule = rules[i + 1];
  928. var rule = options.allrules[customRule];
  929. var fn;
  930. if(!rule) {
  931. alert("jqv:custom rule not found - "+customRule);
  932. return;
  933. }
  934. if(rule["regex"]) {
  935. var ex=rule.regex;
  936. if(!ex) {
  937. alert("jqv:custom regex not found - "+customRule);
  938. return;
  939. }
  940. var pattern = new RegExp(ex);
  941. if (!pattern.test(field.val())) return options.allrules[customRule].alertText;
  942. } else if(rule["func"]) {
  943. fn = rule["func"];
  944. if (typeof(fn) !== "function") {
  945. alert("jqv:custom parameter 'function' is no function - "+customRule);
  946. return;
  947. }
  948. if (!fn(field, rules, i, options))
  949. return options.allrules[customRule].alertText;
  950. } else {
  951. alert("jqv:custom type not allowed "+customRule);
  952. return;
  953. }
  954. },
  955. /**
  956. * Validate custom function outside of the engine scope
  957. *
  958. * @param {jqObject} field
  959. * @param {Array[String]} rules
  960. * @param {int} i rules index
  961. * @param {Map}
  962. * user options
  963. * @return an error string if validation failed
  964. */
  965. _funcCall: function(field, rules, i, options) {
  966. var functionName = rules[i + 1];
  967. var fn;
  968. if(functionName.indexOf('.') >-1)
  969. {
  970. var namespaces = functionName.split('.');
  971. var scope = window;
  972. while(namespaces.length)
  973. {
  974. scope = scope[namespaces.shift()];
  975. }
  976. fn = scope;
  977. }
  978. else
  979. fn = window[functionName] || options.customFunctions[functionName];
  980. if (typeof(fn) == 'function')
  981. return fn(field, rules, i, options);
  982. },
  983. _funcCallRequired: function(field, rules, i, options) {
  984. return methods._funcCall(field,rules,i,options);
  985. },
  986. /**
  987. * Field match
  988. *
  989. * @param {jqObject} field
  990. * @param {Array[String]} rules
  991. * @param {int} i rules index
  992. * @param {Map}
  993. * user options
  994. * @return an error string if validation failed
  995. */
  996. _equals: function(field, rules, i, options) {
  997. var equalsField = rules[i + 1];
  998. if (field.val() != $("#" + equalsField).val())
  999. return options.allrules.equals.alertText;
  1000. },
  1001. /**
  1002. * Check the maximum size (in characters)
  1003. *
  1004. * @param {jqObject} field
  1005. * @param {Array[String]} rules
  1006. * @param {int} i rules index
  1007. * @param {Map}
  1008. * user options
  1009. * @return an error string if validation failed
  1010. */
  1011. _maxSize: function(field, rules, i, options) {
  1012. var max = rules[i + 1];
  1013. var len = field.val().length;
  1014. if (len > max) {
  1015. var rule = options.allrules.maxSize;
  1016. return rule.alertText + max + rule.alertText2;
  1017. }
  1018. },
  1019. /**
  1020. * Check the minimum size (in characters)
  1021. *
  1022. * @param {jqObject} field
  1023. * @param {Array[String]} rules
  1024. * @param {int} i rules index
  1025. * @param {Map}
  1026. * user options
  1027. * @return an error string if validation failed
  1028. */
  1029. _minSize: function(field, rules, i, options) {
  1030. var min = rules[i + 1];
  1031. var len = field.val().length;
  1032. if (len < min) {
  1033. var rule = options.allrules.minSize;
  1034. return rule.alertText + min + rule.alertText2;
  1035. }
  1036. },
  1037. /**
  1038. * Check number minimum value
  1039. *
  1040. * @param {jqObject} field
  1041. * @param {Array[String]} rules
  1042. * @param {int} i rules index
  1043. * @param {Map}
  1044. * user options
  1045. * @return an error string if validation failed
  1046. */
  1047. _min: function(field, rules, i, options) {
  1048. var min = parseFloat(rules[i + 1]);
  1049. var len = parseFloat(field.val());
  1050. if (len < min) {
  1051. var rule = options.allrules.min;
  1052. if (rule.alertText2) return rule.alertText + min + rule.alertText2;
  1053. return rule.alertText + min;
  1054. }
  1055. },
  1056. /**
  1057. * Check number maximum value
  1058. *
  1059. * @param {jqObject} field
  1060. * @param {Array[String]} rules
  1061. * @param {int} i rules index
  1062. * @param {Map}
  1063. * user options
  1064. * @return an error string if validation failed
  1065. */
  1066. _max: function(field, rules, i, options) {
  1067. var max = parseFloat(rules[i + 1]);
  1068. var len = parseFloat(field.val());
  1069. if (len >max ) {
  1070. var rule = options.allrules.max;
  1071. if (rule.alertText2) return rule.alertText + max + rule.alertText2;
  1072. //orefalo: to review, also do the translations
  1073. return rule.alertText + max;
  1074. }
  1075. },
  1076. /**
  1077. * Checks date is in the past
  1078. *
  1079. * @param {jqObject} field
  1080. * @param {Array[String]} rules
  1081. * @param {int} i rules index
  1082. * @param {Map}
  1083. * user options
  1084. * @return an error string if validation failed
  1085. */
  1086. _past: function(form, field, rules, i, options) {
  1087. var p=rules[i + 1];
  1088. var fieldAlt = $(form.find("*[name='" + p.replace(/^#+/, '') + "']"));
  1089. var pdate;
  1090. if (p.toLowerCase() == "now") {
  1091. pdate = new Date();
  1092. } else if (undefined != fieldAlt.val()) {
  1093. if (fieldAlt.is(":disabled"))
  1094. return;
  1095. pdate = methods._parseDate(fieldAlt.val());
  1096. } else {
  1097. pdate = methods._parseDate(p);
  1098. }
  1099. var vdate = methods._parseDate(field.val());
  1100. if (vdate > pdate ) {
  1101. var rule = options.allrules.past;
  1102. if (rule.alertText2) return rule.alertText + methods._dateToString(pdate) + rule.alertText2;
  1103. return rule.alertText + methods._dateToString(pdate);
  1104. }
  1105. },
  1106. /**
  1107. * Checks date is in the future
  1108. *
  1109. * @param {jqObject} field
  1110. * @param {Array[String]} rules
  1111. * @param {int} i rules index
  1112. * @param {Map}
  1113. * user options
  1114. * @return an error string if validation failed
  1115. */
  1116. _future: function(form, field, rules, i, options) {
  1117. var p=rules[i + 1];
  1118. var fieldAlt = $(form.find("*[name='" + p.replace(/^#+/, '') + "']"));
  1119. var pdate;
  1120. if (p.toLowerCase() == "now") {
  1121. pdate = new Date();
  1122. } else if (undefined != fieldAlt.val()) {
  1123. if (fieldAlt.is(":disabled"))
  1124. return;
  1125. pdate = methods._parseDate(fieldAlt.val());
  1126. } else {
  1127. pdate = methods._parseDate(p);
  1128. }
  1129. var vdate = methods._parseDate(field.val());
  1130. if (vdate < pdate ) {
  1131. var rule = options.allrules.future;
  1132. if (rule.alertText2)
  1133. return rule.alertText + methods._dateToString(pdate) + rule.alertText2;
  1134. return rule.alertText + methods._dateToString(pdate);
  1135. }
  1136. },
  1137. /**
  1138. * Checks if valid date
  1139. *
  1140. * @param {string} date string
  1141. * @return a bool based on determination of valid date
  1142. */
  1143. _isDate: function (value) {
  1144. var dateRegEx = new RegExp(/^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$|^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$/);
  1145. return dateRegEx.test(value);
  1146. },
  1147. /**
  1148. * Checks if valid date time
  1149. *
  1150. * @param {string} date string
  1151. * @return a bool based on determination of valid date time
  1152. */
  1153. _isDateTime: function (value){
  1154. var dateTimeRegEx = new RegExp(/^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])\s+(1[012]|0?[1-9]){1}:(0?[1-5]|[0-6][0-9]){1}:(0?[0-6]|[0-6][0-9]){1}\s+(am|pm|AM|PM){1}$|^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^((1[012]|0?[1-9]){1}\/(0?[1-9]|[12][0-9]|3[01]){1}\/\d{2,4}\s+(1[012]|0?[1-9]){1}:(0?[1-5]|[0-6][0-9]){1}:(0?[0-6]|[0-6][0-9]){1}\s+(am|pm|AM|PM){1})$/);
  1155. return dateTimeRegEx.test(value);
  1156. },
  1157. //Checks if the start date is before the end date
  1158. //returns true if end is later than start
  1159. _dateCompare: function (start, end) {
  1160. return (new Date(start.toString()) < new Date(end.toString()));
  1161. },
  1162. /**
  1163. * Checks date range
  1164. *
  1165. * @param {jqObject} first field name
  1166. * @param {jqObject} second field name
  1167. * @return an error string if validation failed
  1168. */
  1169. _dateRange: function (field, rules, i, options) {
  1170. //are not both populated
  1171. if ((!options.firstOfGroup[0].value && options.secondOfGroup[0].value) || (options.firstOfGroup[0].value && !options.secondOfGroup[0].value)) {
  1172. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1173. }
  1174. //are not both dates
  1175. if (!methods._isDate(options.firstOfGroup[0].value) || !methods._isDate(options.secondOfGroup[0].value)) {
  1176. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1177. }
  1178. //are both dates but range is off
  1179. if (!methods._dateCompare(options.firstOfGroup[0].value, options.secondOfGroup[0].value)) {
  1180. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1181. }
  1182. },
  1183. /**
  1184. * Checks date time range
  1185. *
  1186. * @param {jqObject} first field name
  1187. * @param {jqObject} second field name
  1188. * @return an error string if validation failed
  1189. */
  1190. _dateTimeRange: function (field, rules, i, options) {
  1191. //are not both populated
  1192. if ((!options.firstOfGroup[0].value && options.secondOfGroup[0].value) || (options.firstOfGroup[0].value && !options.secondOfGroup[0].value)) {
  1193. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1194. }
  1195. //are not both dates
  1196. if (!methods._isDateTime(options.firstOfGroup[0].value) || !methods._isDateTime(options.secondOfGroup[0].value)) {
  1197. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1198. }
  1199. //are both dates but range is off
  1200. if (!methods._dateCompare(options.firstOfGroup[0].value, options.secondOfGroup[0].value)) {
  1201. return options.allrules[rules[i]].alertText + options.allrules[rules[i]].alertText2;
  1202. }
  1203. },
  1204. /**
  1205. * Max number of checkbox selected
  1206. *
  1207. * @param {jqObject} field
  1208. * @param {Array[String]} rules
  1209. * @param {int} i rules index
  1210. * @param {Map}
  1211. * user options
  1212. * @return an error string if validation failed
  1213. */
  1214. _maxCheckbox: function(form, field, rules, i, options) {
  1215. var nbCheck = rules[i + 1];
  1216. var groupname = field.attr("name");
  1217. var groupSize = form.find("input[name='" + groupname + "']:checked").length;
  1218. if (groupSize > nbCheck) {
  1219. options.showArrow = false;
  1220. if (options.allrules.maxCheckbox.alertText2)
  1221. return options.allrules.maxCheckbox.alertText + " " + nbCheck + " " + options.allrules.maxCheckbox.alertText2;
  1222. return options.allrules.maxCheckbox.alertText;
  1223. }
  1224. },
  1225. /**
  1226. * Min number of checkbox selected
  1227. *
  1228. * @param {jqObject} field
  1229. * @param {Array[String]} rules
  1230. * @param {int} i rules index
  1231. * @param {Map}
  1232. * user options
  1233. * @return an error string if validation failed
  1234. */
  1235. _minCheckbox: function(form, field, rules, i, options) {
  1236. var nbCheck = rules[i + 1];
  1237. var groupname = field.attr("name");
  1238. var groupSize = form.find("input[name='" + groupname + "']:checked").length;
  1239. if (groupSize < nbCheck) {
  1240. options.showArrow = false;
  1241. return options.allrules.minCheckbox.alertText + " " + nbCheck + " " + options.allrules.minCheckbox.alertText2;
  1242. }
  1243. },
  1244. /**
  1245. * Checks that it is a valid credit card number according to the
  1246. * Luhn checksum algorithm.
  1247. *
  1248. * @param {jqObject} field
  1249. * @param {Array[String]} rules
  1250. * @param {int} i rules index
  1251. * @param {Map}
  1252. * user options
  1253. * @return an error string if validation failed
  1254. */
  1255. _creditCard: function(field, rules, i, options) {
  1256. //spaces and dashes may be valid characters, but must be stripped to calculate the checksum.
  1257. var valid = false, cardNumber = field.val().replace(/ +/g, '').replace(/-+/g, '');
  1258. var numDigits = cardNumber.length;
  1259. if (numDigits >= 14 && numDigits <= 16 && parseInt(cardNumber) > 0) {
  1260. var sum = 0, i = numDigits - 1, pos = 1, digit, luhn = new String();
  1261. do {
  1262. digit = parseInt(cardNumber.charAt(i));
  1263. luhn += (pos++ % 2 == 0) ? digit * 2 : digit;
  1264. } while (--i >= 0)
  1265. for (i = 0; i < luhn.length; i++) {
  1266. sum += parseInt(luhn.charAt(i));
  1267. }
  1268. valid = sum % 10 == 0;
  1269. }
  1270. if (!valid) return options.allrules.creditCard.alertText;
  1271. },
  1272. /**
  1273. * Ajax field validation
  1274. *
  1275. * @param {jqObject} field
  1276. * @param {Array[String]} rules
  1277. * @param {int} i rules index
  1278. * @param {Map}
  1279. * user options
  1280. * @return nothing! the ajax validator handles the prompts itself
  1281. */
  1282. _ajax: function(field, rules, i, options) {
  1283. var errorSelector = rules[i + 1];
  1284. var rule = options.allrules[errorSelector];
  1285. var extraData = rule.extraData;
  1286. var extraDataDynamic = rule.extraDataDynamic;
  1287. var data = {
  1288. "fieldId" : field.attr("id"),
  1289. "fieldValue" : field.val()
  1290. };
  1291. if (typeof extraData === "object") {
  1292. $.extend(data, extraData);
  1293. } else if (typeof extraData === "string") {
  1294. var tempData = extraData.split("&");
  1295. for(var i = 0; i < tempData.length; i++) {
  1296. var values = tempData[i].split("=");
  1297. if (values[0] && values[0]) {
  1298. data[values[0]] = values[1];
  1299. }
  1300. }
  1301. }
  1302. if (extraDataDynamic) {
  1303. var tmpData = [];
  1304. var domIds = String(extraDataDynamic).split(",");
  1305. for (var i = 0; i < domIds.length; i++) {
  1306. var id = domIds[i];
  1307. if ($(id).length) {
  1308. var inputValue = field.closest("form, .validationEngineContainer").find(id).val();
  1309. var keyValue = id.replace('#', '') + '=' + escape(inputValue);
  1310. data[id.replace('#', '')] = inputValue;
  1311. }
  1312. }
  1313. }
  1314. // If a field change event triggered this we want to clear the cache for this ID
  1315. if (options.eventTrigger == "field") {
  1316. delete(options.ajaxValidCache[field.attr("id")]);
  1317. }
  1318. // If there is an error or if the the field is already validated, do not re-execute AJAX
  1319. if (!options.isError && !methods._checkAjaxFieldStatus(field.attr("id"), options) && options.eventTrigger!="submit") {
  1320. $.ajax({
  1321. type: options.ajaxFormValidationMethod,
  1322. url: rule.url,
  1323. cache: false,
  1324. dataType: "json",
  1325. data: data,
  1326. field: field,
  1327. rule: rule,
  1328. methods: methods,
  1329. options: options,
  1330. beforeSend: function() {},
  1331. error: function(data, transport) {
  1332. if (options.onFailure) {
  1333. options.onFailure(data, transport);
  1334. } else {
  1335. methods._ajaxError(data, transport);
  1336. }
  1337. },
  1338. success: function(json) {
  1339. // asynchronously called on success, data is the json answer from the server
  1340. var errorFieldId = json[0];
  1341. //var errorField = $($("#" + errorFieldId)[0]);
  1342. var errorField = $("#"+ errorFieldId).eq(0);
  1343. // make sure we found the element
  1344. if (errorField.length == 1) {
  1345. var status = json[1];
  1346. // read the optional msg from the server
  1347. var msg = json[2];
  1348. if (!status) {
  1349. // Houston we got a problem - display an red prompt
  1350. options.ajaxValidCache[errorFieldId] = false;
  1351. options.isError = true;
  1352. // resolve the msg prompt
  1353. if(msg) {
  1354. if (options.allrules[msg]) {
  1355. var txt = options.allrules[msg].alertText;
  1356. if (txt) {
  1357. msg = txt;
  1358. }
  1359. }
  1360. }
  1361. else
  1362. msg = rule.alertText;
  1363. if (options.showPrompts) methods._showPrompt(errorField, msg, "", true, options);
  1364. } else {
  1365. options.ajaxValidCache[errorFieldId] = true;
  1366. // resolves the msg prompt
  1367. if(msg) {
  1368. if (options.allrules[msg]) {
  1369. var txt = options.allrules[msg].alertTextOk;
  1370. if (txt) {
  1371. msg = txt;
  1372. }
  1373. }
  1374. }
  1375. else
  1376. msg = rule.alertTextOk;
  1377. if (options.showPrompts) {
  1378. // see if we should display a green prompt
  1379. if (msg)
  1380. methods._showPrompt(errorField, msg, "pass", true, options);
  1381. else
  1382. methods._closePrompt(errorField);
  1383. }
  1384. // If a submit form triggered this, we want to re-submit the form
  1385. if (options.eventTrigger == "submit")
  1386. field.closest("form").submit();
  1387. }
  1388. }
  1389. errorField.trigger("jqv.field.result", [errorField, options.isError, msg]);
  1390. }
  1391. });
  1392. return rule.alertTextLoad;
  1393. }
  1394. },
  1395. /**
  1396. * Common method to handle ajax errors
  1397. *
  1398. * @param {Object} data
  1399. * @param {Object} transport
  1400. */
  1401. _ajaxError: function(data, transport) {
  1402. if(data.status == 0 && transport == null)
  1403. alert("The page is not served from a server! ajax call failed");
  1404. else if(typeof console != "undefined")
  1405. console.log("Ajax error: " + data.status + " " + transport);
  1406. },
  1407. /**
  1408. * date -> string
  1409. *
  1410. * @param {Object} date
  1411. */
  1412. _dateToString: function(date) {
  1413. return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
  1414. },
  1415. /**
  1416. * Parses an ISO date
  1417. * @param {String} d
  1418. */
  1419. _parseDate: function(d) {
  1420. var dateParts = d.split("-");
  1421. if(dateParts==d)
  1422. dateParts = d.split("/");
  1423. if(dateParts==d) {
  1424. dateParts = d.split(".");
  1425. return new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
  1426. }
  1427. return new Date(dateParts[0], (dateParts[1] - 1) ,dateParts[2]);
  1428. },
  1429. /**
  1430. * Builds or updates a prompt with the given information
  1431. *
  1432. * @param {jqObject} field
  1433. * @param {String} promptText html text to display type
  1434. * @param {String} type the type of bubble: 'pass' (green), 'load' (black) anything else (red)
  1435. * @param {boolean} ajaxed - use to mark fields than being validated with ajax
  1436. * @param {Map} options user options
  1437. */
  1438. _showPrompt: function(field, promptText, type, ajaxed, options, ajaxform) {
  1439. //Check if we need to adjust what element to show the prompt on
  1440. if(field.data('jqv-prompt-at') instanceof jQuery ){
  1441. field = field.data('jqv-prompt-at');
  1442. } else if(field.data('jqv-prompt-at')) {
  1443. field = $(field.data('jqv-prompt-at'));
  1444. }
  1445. var prompt = methods._getPrompt(field);
  1446. // The ajax submit errors are not see has an error in the form,
  1447. // When the form errors are returned, the engine see 2 bubbles, but those are ebing closed by the engine at the same time
  1448. // Because no error was found befor submitting
  1449. if(ajaxform) prompt = false;
  1450. // Check that there is indded text
  1451. if($.trim(promptText)){
  1452. if (prompt)
  1453. methods._updatePrompt(field, prompt, promptText, type, ajaxed, options);
  1454. else
  1455. methods._buildPrompt(field, promptText, type, ajaxed, options);
  1456. }
  1457. },
  1458. /**
  1459. * Builds and shades a prompt for the given field.
  1460. *
  1461. * @param {jqObject} field
  1462. * @param {String} promptText html text to display type
  1463. * @param {String} type the type of bubble: 'pass' (green), 'load' (black) anything else (red)
  1464. * @param {boolean} ajaxed - use to mark fields than being validated with ajax
  1465. * @param {Map} options user options
  1466. */
  1467. _buildPrompt: function(field, promptText, type, ajaxed, options) {
  1468. // create the prompt
  1469. var prompt = $('<div>');
  1470. prompt.addClass(methods._getClassName(field.attr("id")) + "formError");
  1471. // add a class name to identify the parent form of the prompt
  1472. prompt.addClass("parentForm"+methods._getClassName(field.closest('form, .validationEngineContainer').attr("id")));
  1473. prompt.addClass("formError");
  1474. switch (type) {
  1475. case "pass":
  1476. prompt.addClass("greenPopup");
  1477. break;
  1478. case "load":
  1479. prompt.addClass("blackPopup");
  1480. break;
  1481. default:
  1482. /* it has error */
  1483. //alert("unknown popup type:"+type);
  1484. }
  1485. if (ajaxed)
  1486. prompt.addClass("ajaxed");
  1487. // create the prompt content
  1488. var promptContent = $('<div>').addClass("formErrorContent").html(promptText).appendTo(prompt);
  1489. // determine position type
  1490. var positionType=field.data("promptPosition") || options.promptPosition;
  1491. // create the css arrow pointing at the field
  1492. // note that there is no triangle on max-checkbox and radio
  1493. if (options.showArrow) {
  1494. var arrow = $('<div>').addClass("formErrorArrow");
  1495. //prompt positioning adjustment support. Usage: positionType:Xshift,Yshift (for ex.: bottomLeft:+20 or bottomLeft:-20,+10)
  1496. if (typeof(positionType)=='string')
  1497. {
  1498. var pos=positionType.indexOf(":");
  1499. if(pos!=-1)
  1500. positionType=positionType.substring(0,pos);
  1501. }
  1502. switch (positionType) {
  1503. case "bottomLeft":
  1504. case "bottomRight":
  1505. prompt.find(".formErrorContent").before(arrow);
  1506. arrow.addClass("formErrorArrowBottom").html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
  1507. break;
  1508. case "topLeft":
  1509. case "topRight":
  1510. arrow.html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
  1511. prompt.append(arrow);
  1512. break;
  1513. }
  1514. }
  1515. // Add custom prompt class
  1516. if (options.addPromptClass)
  1517. prompt.addClass(options.addPromptClass);
  1518. // Add custom prompt class defined in element
  1519. var requiredOverride = field.attr('data-required-class');
  1520. if(requiredOverride !== undefined) {
  1521. prompt.addClass(requiredOverride);
  1522. } else {
  1523. if(options.prettySelect) {
  1524. if($('#' + field.attr('id')).next().is('select')) {
  1525. var prettyOverrideClass = $('#' + field.attr('id').substr(options.usePrefix.length).substring(options.useSuffix.length)).attr('data-required-class');
  1526. if(prettyOverrideClass !== undefined) {
  1527. prompt.addClass(prettyOverrideClass);
  1528. }
  1529. }
  1530. }
  1531. }
  1532. prompt.css({
  1533. "opacity": 0
  1534. });
  1535. if(positionType === 'inline') {
  1536. prompt.addClass("inline");
  1537. if(typeof field.attr('data-prompt-target') !== 'undefined' && $('#'+field.attr('data-prompt-target')).length > 0) {
  1538. prompt.appendTo($('#'+field.attr('data-prompt-target')));
  1539. } else {
  1540. field.after(prompt);
  1541. }
  1542. } else {
  1543. field.before(prompt);
  1544. }
  1545. var pos = methods._calculatePosition(field, prompt, options);
  1546. // Support RTL layouts by @yasser_lotfy ( Yasser Lotfy )
  1547. if ($('body').hasClass('rtl')) {
  1548. prompt.css({
  1549. 'position': positionType === 'inline' ? 'relative' : 'absolute',
  1550. "top": pos.callerTopPosition,
  1551. "left": "initial",
  1552. "right": pos.callerleftPosition,
  1553. "marginTop": pos.marginTopSize,
  1554. "opacity": 0
  1555. }).data("callerField", field);
  1556. } else {
  1557. prompt.css({
  1558. 'position': positionType === 'inline' ? 'relative' : 'absolute',
  1559. "top": pos.callerTopPosition,
  1560. "left": pos.callerleftPosition,
  1561. "right": "initial",
  1562. "marginTop": pos.marginTopSize,
  1563. "opacity": 0
  1564. }).data("callerField", field);
  1565. }
  1566. if (options.autoHidePrompt) {
  1567. setTimeout(function(){
  1568. prompt.animate({
  1569. "opacity": 0
  1570. },function(){
  1571. prompt.closest('.formError').remove();
  1572. });
  1573. }, options.autoHideDelay);
  1574. }
  1575. return prompt.animate({
  1576. "opacity": 0.87
  1577. });
  1578. },
  1579. /**
  1580. * Updates the prompt text field - the field for which the prompt
  1581. * @param {jqObject} field
  1582. * @param {String} promptText html text to display type
  1583. * @param {String} type the type of bubble: 'pass' (green), 'load' (black) anything else (red)
  1584. * @param {boolean} ajaxed - use to mark fields than being validated with ajax
  1585. * @param {Map} options user options
  1586. */
  1587. _updatePrompt: function(field, prompt, promptText, type, ajaxed, options, noAnimation) {
  1588. if (prompt) {
  1589. if (typeof type !== "undefined") {
  1590. if (type == "pass")
  1591. prompt.addClass("greenPopup");
  1592. else
  1593. prompt.removeClass("greenPopup");
  1594. if (type == "load")
  1595. prompt.addClass("blackPopup");
  1596. else
  1597. prompt.removeClass("blackPopup");
  1598. }
  1599. if (ajaxed)
  1600. prompt.addClass("ajaxed");
  1601. else
  1602. prompt.removeClass("ajaxed");
  1603. prompt.find(".formErrorContent").html(promptText);
  1604. var pos = methods._calculatePosition(field, prompt, options);
  1605. // Support RTL layouts by @yasser_lotfy ( Yasser Lotfy )
  1606. if ($('body').hasClass('rtl')) {
  1607. var css = {"top": pos.callerTopPosition,
  1608. "left": "initial",
  1609. "right": pos.callerleftPosition,
  1610. "marginTop": pos.marginTopSize,
  1611. "opacity": 0.87};
  1612. } else {
  1613. var css = {"top": pos.callerTopPosition,
  1614. "left": pos.callerleftPosition,
  1615. "right": "initial",
  1616. "marginTop": pos.marginTopSize,
  1617. "opacity": 0.87};
  1618. }
  1619. prompt.css({
  1620. "opacity": 0,
  1621. "display": "block"
  1622. });
  1623. if (noAnimation)
  1624. prompt.css(css);
  1625. else
  1626. prompt.animate(css);
  1627. }
  1628. },
  1629. /**
  1630. * Closes the prompt associated with the given field
  1631. *
  1632. * @param {jqObject}
  1633. * field
  1634. */
  1635. _closePrompt: function(field) {
  1636. var prompt = methods._getPrompt(field);
  1637. if (prompt)
  1638. prompt.fadeTo("fast", 0, function() {
  1639. prompt.closest('.formError').remove();
  1640. });
  1641. },
  1642. closePrompt: function(field) {
  1643. return methods._closePrompt(field);
  1644. },
  1645. /**
  1646. * Returns the error prompt matching the field if any
  1647. *
  1648. * @param {jqObject}
  1649. * field
  1650. * @return undefined or the error prompt (jqObject)
  1651. */
  1652. _getPrompt: function(field) {
  1653. var formId = $(field).closest('form, .validationEngineContainer').attr('id');
  1654. var className = methods._getClassName(field.attr("id")) + "formError";
  1655. var match = $("." + methods._escapeExpression(className) + '.parentForm' + methods._getClassName(formId))[0];
  1656. if (match)
  1657. return $(match);
  1658. },
  1659. /**
  1660. * Returns the escapade classname
  1661. *
  1662. * @param {selector}
  1663. * className
  1664. */
  1665. _escapeExpression: function (selector) {
  1666. return selector.replace(/([#;&,\.\+\*\~':"\!\^$\[\]\(\)=>\|])/g, "\\$1");
  1667. },
  1668. /**
  1669. * returns true if we are in a RTLed document
  1670. *
  1671. * @param {jqObject} field
  1672. */
  1673. isRTL: function(field)
  1674. {
  1675. var $document = $(document);
  1676. var $body = $('body');
  1677. var rtl =
  1678. (field && field.hasClass('rtl')) ||
  1679. (field && (field.attr('dir') || '').toLowerCase()==='rtl') ||
  1680. $document.hasClass('rtl') ||
  1681. ($document.attr('dir') || '').toLowerCase()==='rtl' ||
  1682. $body.hasClass('rtl') ||
  1683. ($body.attr('dir') || '').toLowerCase()==='rtl';
  1684. return Boolean(rtl);
  1685. },
  1686. /**
  1687. * Calculates prompt position
  1688. *
  1689. * @param {jqObject}
  1690. * field
  1691. * @param {jqObject}
  1692. * the prompt
  1693. * @param {Map}
  1694. * options
  1695. * @return positions
  1696. */
  1697. _calculatePosition: function (field, promptElmt, options) {
  1698. var promptTopPosition, promptleftPosition, marginTopSize;
  1699. var fieldWidth = field.width();
  1700. var fieldLeft = field.position().left;
  1701. var fieldTop = field.position().top;
  1702. var fieldHeight = field.height();
  1703. var promptHeight = promptElmt.height();
  1704. // is the form contained in an overflown container?
  1705. promptTopPosition = promptleftPosition = 0;
  1706. // compensation for the arrow
  1707. marginTopSize = -promptHeight;
  1708. //prompt positioning adjustment support
  1709. //now you can adjust prompt position
  1710. //usage: positionType:Xshift,Yshift
  1711. //for example:
  1712. // bottomLeft:+20 means bottomLeft position shifted by 20 pixels right horizontally
  1713. // topRight:20, -15 means topRight position shifted by 20 pixels to right and 15 pixels to top
  1714. //You can use +pixels, - pixels. If no sign is provided than + is default.
  1715. var positionType=field.data("promptPosition") || options.promptPosition;
  1716. var shift1="";
  1717. var shift2="";
  1718. var shiftX=0;
  1719. var shiftY=0;
  1720. if (typeof(positionType)=='string') {
  1721. //do we have any position adjustments ?
  1722. if (positionType.indexOf(":")!=-1) {
  1723. shift1=positionType.substring(positionType.indexOf(":")+1);
  1724. positionType=positionType.substring(0,positionType.indexOf(":"));
  1725. //if any advanced positioning will be needed (percents or something else) - parser should be added here
  1726. //for now we use simple parseInt()
  1727. //do we have second parameter?
  1728. if (shift1.indexOf(",") !=-1) {
  1729. shift2=shift1.substring(shift1.indexOf(",") +1);
  1730. shift1=shift1.substring(0,shift1.indexOf(","));
  1731. shiftY=parseInt(shift2);
  1732. if (isNaN(shiftY)) shiftY=0;
  1733. };
  1734. shiftX=parseInt(shift1);
  1735. if (isNaN(shift1)) shift1=0;
  1736. };
  1737. };
  1738. switch (positionType) {
  1739. default:
  1740. case "topRight":
  1741. promptleftPosition += fieldLeft + fieldWidth - 27;
  1742. promptTopPosition += fieldTop;
  1743. break;
  1744. case "topLeft":
  1745. promptTopPosition += fieldTop;
  1746. promptleftPosition += fieldLeft;
  1747. break;
  1748. case "centerRight":
  1749. promptTopPosition = fieldTop+4;
  1750. marginTopSize = 0;
  1751. promptleftPosition= fieldLeft + field.outerWidth(true)+5;
  1752. break;
  1753. case "centerLeft":
  1754. promptleftPosition = fieldLeft - (promptElmt.width() + 2);
  1755. promptTopPosition = fieldTop+4;
  1756. marginTopSize = 0;
  1757. break;
  1758. case "bottomLeft":
  1759. promptTopPosition = fieldTop + field.height() + 5;
  1760. marginTopSize = 0;
  1761. promptleftPosition = fieldLeft;
  1762. break;
  1763. case "bottomRight":
  1764. promptleftPosition = fieldLeft + fieldWidth - 27;
  1765. promptTopPosition = fieldTop + field.height() + 5;
  1766. marginTopSize = 0;
  1767. break;
  1768. case "inline":
  1769. promptleftPosition = 0;
  1770. promptTopPosition = 0;
  1771. marginTopSize = 0;
  1772. };
  1773. //apply adjusments if any
  1774. promptleftPosition += shiftX;
  1775. promptTopPosition += shiftY;
  1776. return {
  1777. "callerTopPosition": promptTopPosition + "px",
  1778. "callerleftPosition": promptleftPosition + "px",
  1779. "marginTopSize": marginTopSize + "px"
  1780. };
  1781. },
  1782. /**
  1783. * Saves the user options and variables in the form.data
  1784. *
  1785. * @param {jqObject}
  1786. * form - the form where the user option should be saved
  1787. * @param {Map}
  1788. * options - the user options
  1789. * @return the user options (extended from the defaults)
  1790. */
  1791. _saveOptions: function(form, options) {
  1792. // is there a language localisation ?
  1793. if ($.validationEngineLanguage)
  1794. var allRules = $.validationEngineLanguage.allRules;
  1795. else
  1796. $.error("jQuery.validationEngine rules are not loaded, plz add localization files to the page");
  1797. // --- Internals DO NOT TOUCH or OVERLOAD ---
  1798. // validation rules and i18
  1799. $.validationEngine.defaults.allrules = allRules;
  1800. var userOptions = $.extend(true,{},$.validationEngine.defaults,options);
  1801. form.data('jqv', userOptions);
  1802. return userOptions;
  1803. },
  1804. /**
  1805. * Removes forbidden characters from class name
  1806. * @param {String} className
  1807. */
  1808. _getClassName: function(className) {
  1809. if(className)
  1810. return className.replace(/:/g, "_").replace(/\./g, "_");
  1811. },
  1812. /**
  1813. * Escape special character for jQuery selector
  1814. * http://totaldev.com/content/escaping-characters-get-valid-jquery-id
  1815. * @param {String} selector
  1816. */
  1817. _jqSelector: function(str){
  1818. return str.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
  1819. },
  1820. /**
  1821. * Conditionally required field
  1822. *
  1823. * @param {jqObject} field
  1824. * @param {Array[String]} rules
  1825. * @param {int} i rules index
  1826. * @param {Map}
  1827. * user options
  1828. * @return an error string if validation failed
  1829. */
  1830. _condRequired: function(field, rules, i, options) {
  1831. var idx, dependingField;
  1832. for(idx = (i + 1); idx < rules.length; idx++) {
  1833. dependingField = jQuery("#" + rules[idx]).first();
  1834. /* Use _required for determining wether dependingField has a value.
  1835. * There is logic there for handling all field types, and default value; so we won't replicate that here
  1836. * Indicate this special use by setting the last parameter to true so we only validate the dependingField on chackboxes and radio buttons (#462)
  1837. */
  1838. if (dependingField.length && methods._required(dependingField, ["required"], 0, options, true) == undefined) {
  1839. /* We now know any of the depending fields has a value,
  1840. * so we can validate this field as per normal required code
  1841. */
  1842. return methods._required(field, ["required"], 0, options);
  1843. }
  1844. }
  1845. },
  1846. _submitButtonClick: function(event) {
  1847. var button = $(this);
  1848. var form = button.closest('form, .validationEngineContainer');
  1849. form.data("jqv_submitButton", button.attr("id"));
  1850. }
  1851. };
  1852. /**
  1853. * Plugin entry point.
  1854. * You may pass an action as a parameter or a list of options.
  1855. * if none, the init and attach methods are being called.
  1856. * Remember: if you pass options, the attached method is NOT called automatically
  1857. *
  1858. * @param {String}
  1859. * method (optional) action
  1860. */
  1861. $.fn.validationEngine = function(method) {
  1862. var form = $(this);
  1863. if(!form[0]) return form; // stop here if the form does not exist
  1864. if (typeof(method) == 'string' && method.charAt(0) != '_' && methods[method]) {
  1865. // make sure init is called once
  1866. if(method != "showPrompt" && method != "hide" && method != "hideAll")
  1867. methods.init.apply(form);
  1868. return methods[method].apply(form, Array.prototype.slice.call(arguments, 1));
  1869. } else if (typeof method == 'object' || !method) {
  1870. // default constructor with or without arguments
  1871. methods.init.apply(form, arguments);
  1872. return methods.attach.apply(form);
  1873. } else {
  1874. $.error('Method ' + method + ' does not exist in jQuery.validationEngine');
  1875. }
  1876. };
  1877. // LEAK GLOBAL OPTIONS
  1878. $.validationEngine= {fieldIdCounter: 0,defaults:{
  1879. // Name of the event triggering field validation
  1880. validationEventTrigger: "blur",
  1881. // Automatically scroll viewport to the first error
  1882. scroll: true,
  1883. // Focus on the first input
  1884. focusFirstField:true,
  1885. // Show prompts, set to false to disable prompts
  1886. showPrompts: true,
  1887. // Should we attempt to validate non-visible input fields contained in the form? (Useful in cases of tabbed containers, e.g. jQuery-UI tabs)
  1888. validateNonVisibleFields: false,
  1889. // ignore the validation for fields with this specific class (Useful in cases of tabbed containers AND hidden fields we don't want to validate)
  1890. ignoreFieldsWithClass: 'ignoreMe',
  1891. // Opening box position, possible locations are: topLeft,
  1892. // topRight, bottomLeft, centerRight, bottomRight, inline
  1893. // inline gets inserted after the validated field or into an element specified in data-prompt-target
  1894. promptPosition: "topRight",
  1895. bindMethod:"bind",
  1896. // internal, automatically set to true when it parse a _ajax rule
  1897. inlineAjax: false,
  1898. // if set to true, the form data is sent asynchronously via ajax to the form.action url (get)
  1899. ajaxFormValidation: false,
  1900. // The url to send the submit ajax validation (default to action)
  1901. ajaxFormValidationURL: false,
  1902. // HTTP method used for ajax validation
  1903. ajaxFormValidationMethod: 'get',
  1904. // Ajax form validation callback method: boolean onComplete(form, status, errors, options)
  1905. // retuns false if the form.submit event needs to be canceled.
  1906. onAjaxFormComplete: $.noop,
  1907. // called right before the ajax call, may return false to cancel
  1908. onBeforeAjaxFormValidation: $.noop,
  1909. // Stops form from submitting and execute function assiciated with it
  1910. onValidationComplete: false,
  1911. // Used when you have a form fields too close and the errors messages are on top of other disturbing viewing messages
  1912. doNotShowAllErrosOnSubmit: false,
  1913. // Object where you store custom messages to override the default error messages
  1914. custom_error_messages:{},
  1915. // true if you want to validate the input fields on blur event
  1916. binded: true,
  1917. // set to true if you want to validate the input fields on blur only if the field it's not empty
  1918. notEmpty: false,
  1919. // set to true, when the prompt arrow needs to be displayed
  1920. showArrow: true,
  1921. // set to false, determines if the prompt arrow should be displayed when validating
  1922. // checkboxes and radio buttons
  1923. showArrowOnRadioAndCheckbox: false,
  1924. // did one of the validation fail ? kept global to stop further ajax validations
  1925. isError: false,
  1926. // Limit how many displayed errors a field can have
  1927. maxErrorsPerField: false,
  1928. // Caches field validation status, typically only bad status are created.
  1929. // the array is used during ajax form validation to detect issues early and prevent an expensive submit
  1930. ajaxValidCache: {},
  1931. // Auto update prompt position after window resize
  1932. autoPositionUpdate: false,
  1933. InvalidFields: [],
  1934. onFieldSuccess: false,
  1935. onFieldFailure: false,
  1936. onSuccess: false,
  1937. onFailure: false,
  1938. validateAttribute: "class",
  1939. addSuccessCssClassToField: "",
  1940. addFailureCssClassToField: "",
  1941. // Auto-hide prompt
  1942. autoHidePrompt: false,
  1943. // Delay before auto-hide
  1944. autoHideDelay: 10000,
  1945. // Fade out duration while hiding the validations
  1946. fadeDuration: 300,
  1947. // Use Prettify select library
  1948. prettySelect: false,
  1949. // Add css class on prompt
  1950. addPromptClass : "",
  1951. // Custom ID uses prefix
  1952. usePrefix: "",
  1953. // Custom ID uses suffix
  1954. useSuffix: "",
  1955. // Only show one message per error prompt
  1956. showOneMessage: false
  1957. }};
  1958. $(function(){$.validationEngine.defaults.promptPosition = methods.isRTL()?'topLeft':"topRight"});
  1959. })(jQuery);