validationEngine.min.js 69 KB

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