bootstrapValidator.js 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.4.2-dev
  7. * @author https://twitter.com/nghuuphuoc
  8. * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
  9. * @license MIT
  10. */
  11. (function($) {
  12. var BootstrapValidator = function(form, options) {
  13. this.$form = $(form);
  14. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  15. this.$invalidField = null; // First invalid field
  16. this.$submitButton = null; // The submit button which is clicked to submit form
  17. // Validating status
  18. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  19. this.STATUS_VALIDATING = 'VALIDATING';
  20. this.STATUS_INVALID = 'INVALID';
  21. this.STATUS_VALID = 'VALID';
  22. // Determine the event that is fired when user change the field value
  23. // Most modern browsers supports input event except IE 7, 8.
  24. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  25. // In that case I will use the keydown event
  26. var el = document.createElement('div');
  27. this._changeEvent = ('oninput' in el) ? 'input' : 'keydown';
  28. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  29. this._submitIfValid = null;
  30. this._init();
  31. };
  32. // The default options
  33. BootstrapValidator.DEFAULT_OPTIONS = {
  34. // The form CSS class
  35. elementClass: 'bootstrap-validator-form',
  36. // Default invalid message
  37. message: 'This value is not valid',
  38. // Shows ok/error/loading icons based on the field validity.
  39. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  40. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  41. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  42. //
  43. // Examples:
  44. // - Use Glyphicons icons:
  45. // feedbackIcons: {
  46. // valid: 'glyphicon glyphicon-ok',
  47. // invalid: 'glyphicon glyphicon-remove',
  48. // validating: 'glyphicon glyphicon-refresh'
  49. // }
  50. // - Use FontAwesome icons:
  51. // feedbackIcons: {
  52. // valid: 'fa fa-check',
  53. // invalid: 'fa fa-times',
  54. // validating: 'fa fa-refresh'
  55. // }
  56. feedbackIcons: {
  57. valid: null,
  58. invalid: null,
  59. validating: null
  60. },
  61. // The submit buttons selector
  62. // These buttons will be disabled to prevent the valid form from multiple submissions
  63. submitButtons: 'button[type="submit"]',
  64. // The custom submit handler
  65. // It will prevent the form from the default submission
  66. //
  67. // submitHandler: function(validator, form) {
  68. // - validator is the BootstrapValidator instance
  69. // - form is the jQuery object present the current form
  70. // }
  71. submitHandler: null,
  72. // Live validating option
  73. // Can be one of 3 values:
  74. // - enabled: The plugin validates fields as soon as they are changed
  75. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  76. // - submitted: The live validating is enabled after the form is submitted
  77. live: 'enabled',
  78. // Map the field name with validator rules
  79. fields: null
  80. };
  81. BootstrapValidator.prototype = {
  82. constructor: BootstrapValidator,
  83. /**
  84. * Init form
  85. */
  86. _init: function() {
  87. var that = this,
  88. options = {
  89. trigger: this.$form.attr('data-bv-trigger'),
  90. message: this.$form.attr('data-bv-message'),
  91. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  92. live: this.$form.attr('data-bv-live'),
  93. fields: {},
  94. feedbackIcons: {
  95. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  96. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  97. validating: this.$form.attr('data-bv-feedbackicons-validating')
  98. }
  99. },
  100. validator,
  101. v, // Validator name
  102. enabled,
  103. optionName,
  104. optionValue,
  105. html5AttrName,
  106. html5Attrs;
  107. this.$form
  108. // Disable client side validation in HTML 5
  109. .attr('novalidate', 'novalidate')
  110. .addClass(this.options.elementClass)
  111. // Disable the default submission first
  112. .on('submit.bv', function(e) {
  113. e.preventDefault();
  114. that.validate();
  115. })
  116. .on('click', this.options.submitButtons, function() {
  117. that.$submitButton = $(this);
  118. // The user just click the submit button
  119. that._submitIfValid = true;
  120. })
  121. // Find all fields which have either "name" or "data-bv-field" attribute
  122. .find('[name], [data-bv-field]').each(function() {
  123. var $field = $(this);
  124. // Don't initialize hidden input
  125. if ('hidden' == $field.attr('type')) {
  126. return;
  127. }
  128. var field = $field.attr('name') || $field.attr('data-bv-field');
  129. $field.attr('data-bv-field', field);
  130. options.fields[field] = $.extend({}, {
  131. trigger: $field.attr('data-bv-trigger'),
  132. message: $field.attr('data-bv-message'),
  133. container: $field.attr('data-bv-container'),
  134. selector: $field.attr('data-bv-selector'),
  135. validators: {}
  136. }, options.fields[field]);
  137. for (v in $.fn.bootstrapValidator.validators) {
  138. validator = $.fn.bootstrapValidator.validators[v];
  139. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  140. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  141. if ((html5Attrs && enabled != 'false')
  142. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  143. {
  144. // Try to parse the options via attributes
  145. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  146. options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
  147. for (html5AttrName in validator.html5Attributes) {
  148. optionName = validator.html5Attributes[html5AttrName];
  149. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  150. if (optionValue) {
  151. if ('true' == optionValue) {
  152. optionValue = true;
  153. } else if ('false' == optionValue) {
  154. optionValue = false;
  155. }
  156. options.fields[field]['validators'][v][optionName] = optionValue;
  157. }
  158. }
  159. }
  160. }
  161. });
  162. this.options = $.extend(true, this.options, options);
  163. for (var field in this.options.fields) {
  164. this._initField(field);
  165. }
  166. this.setLiveMode(this.options.live);
  167. },
  168. /**
  169. * Init field
  170. *
  171. * @param {String} field The field name
  172. */
  173. _initField: function(field) {
  174. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  175. return;
  176. }
  177. var fields = this.getFieldElements(field);
  178. // We don't need to validate non-existing fields
  179. if (fields == null) {
  180. delete this.options.fields[field];
  181. return;
  182. }
  183. for (var validatorName in this.options.fields[field].validators) {
  184. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  185. delete this.options.fields[field].validators[validatorName];
  186. }
  187. }
  188. var that = this,
  189. type = fields.attr('type'),
  190. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent,
  191. total = fields.length,
  192. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  193. for (var i = 0; i < total; i++) {
  194. var $field = $(fields[i]),
  195. $parent = $field.parents('.form-group'),
  196. // Allow user to indicate where the error messages are shown
  197. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  198. // Set the attribute to indicate the fields which are defined by selector
  199. if (!$field.attr('data-bv-field')) {
  200. $field.attr('data-bv-field', field);
  201. }
  202. // Whenever the user change the field value, mark it as not validated yet
  203. $field.on(event + '.update.bv', function() {
  204. // Reset the flag
  205. that._submitIfValid = false;
  206. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  207. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  208. });
  209. // Create help block elements for showing the error messages
  210. $field.data('bv.messages', $message);
  211. for (validatorName in this.options.fields[field].validators) {
  212. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  213. if (!updateAll || i == total - 1) {
  214. $('<small/>')
  215. .css('display', 'none')
  216. .attr('data-bv-validator', validatorName)
  217. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  218. .addClass('help-block')
  219. .appendTo($message);
  220. }
  221. }
  222. // Prepare the feedback icons
  223. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  224. if (this.options.feedbackIcons
  225. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  226. && (!updateAll || i == total - 1))
  227. {
  228. $parent.addClass('has-feedback');
  229. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($field);
  230. // The feedback icon does not render correctly if there is no label
  231. // https://github.com/twbs/bootstrap/issues/12873
  232. if ($parent.find('label').length == 0) {
  233. $icon.css('top', 0);
  234. }
  235. }
  236. }
  237. if (this.options.fields[field]['enabled'] == null) {
  238. this.options.fields[field]['enabled'] = true;
  239. }
  240. },
  241. /**
  242. * Get the element to place the error messages
  243. *
  244. * @param {jQuery} $field The field element
  245. * @returns {jQuery}
  246. */
  247. _getMessageContainer: function($field) {
  248. var $parent = $field.parent();
  249. if ($parent.hasClass('form-group')) {
  250. return $parent;
  251. }
  252. var cssClasses = $parent.attr('class');
  253. if (!cssClasses) {
  254. return this._getMessageContainer($parent);
  255. }
  256. cssClasses = cssClasses.split(' ');
  257. var n = cssClasses.length;
  258. for (var i = 0; i < n; i++) {
  259. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  260. return $parent;
  261. }
  262. }
  263. return this._getMessageContainer($parent);
  264. },
  265. /**
  266. * Called when all validations are completed
  267. */
  268. _submit: function() {
  269. if (!this.isValid()) {
  270. if ('submitted' == this.options.live) {
  271. this.setLiveMode('enabled');
  272. }
  273. // Focus to the first invalid field
  274. if (this.$invalidField) {
  275. this.$invalidField.focus();
  276. }
  277. return;
  278. }
  279. // Call the custom submission if enabled
  280. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  281. // If you want to submit the form inside your submit handler, please call defaultSubmit() method
  282. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  283. } else {
  284. this.disableSubmitButtons(true).defaultSubmit();
  285. }
  286. },
  287. // --- Public methods ---
  288. /**
  289. * Retrieve the field elements by given name
  290. *
  291. * @param {String} field The field name
  292. * @returns {null|jQuery[]}
  293. */
  294. getFieldElements: function(field) {
  295. var fields = this.options.fields[field].selector ? $(this.options.fields[field].selector) : this.$form.find('[name="' + field + '"]');
  296. return (fields.length == 0) ? null : fields;
  297. },
  298. /**
  299. * Set live validating mode
  300. *
  301. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  302. * @returns {BootstrapValidator}
  303. */
  304. setLiveMode: function(mode) {
  305. this.options.live = mode;
  306. if ('submitted' == mode) {
  307. return this;
  308. }
  309. var that = this;
  310. for (var field in this.options.fields) {
  311. (function(f) {
  312. var fields = that.getFieldElements(f);
  313. if (fields) {
  314. var type = fields.attr('type'),
  315. total = fields.length,
  316. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  317. trigger = that.options.fields[field].trigger
  318. || that.options.trigger
  319. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
  320. events = $.map(trigger.split(' '), function(item) {
  321. return item + '.live.bv';
  322. }).join(' ');
  323. for (var i = 0; i < total; i++) {
  324. ('enabled' == mode)
  325. ? $(fields[i]).on(events, function() {
  326. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  327. })
  328. : $(fields[i]).off(events);
  329. }
  330. }
  331. })(field);
  332. }
  333. return this;
  334. },
  335. /**
  336. * Disable/enable submit buttons
  337. *
  338. * @param {Boolean} disabled Can be true or false
  339. * @returns {BootstrapValidator}
  340. */
  341. disableSubmitButtons: function(disabled) {
  342. if (!disabled) {
  343. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  344. } else if (this.options.live != 'disabled') {
  345. // Don't disable if the live validating mode is disabled
  346. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  347. }
  348. return this;
  349. },
  350. /**
  351. * Validate the form
  352. *
  353. * @return {BootstrapValidator}
  354. */
  355. validate: function() {
  356. if (!this.options.fields) {
  357. return this;
  358. }
  359. this.disableSubmitButtons(true);
  360. for (var field in this.options.fields) {
  361. this.validateField(field);
  362. }
  363. // Check if whether the submit button is clicked
  364. if (this.$submitButton) {
  365. this._submit();
  366. }
  367. return this;
  368. },
  369. /**
  370. * Validate given field
  371. *
  372. * @param {String} field The field name
  373. * @returns {BootstrapValidator}
  374. */
  375. validateField: function(field) {
  376. var fields = this.getFieldElements(field),
  377. type = fields.attr('type'),
  378. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  379. for (var i = 0; i < n; i++) {
  380. this.validateFieldElement($(fields[i]), (n == 1));
  381. }
  382. return this;
  383. },
  384. /**
  385. * Validate field element
  386. *
  387. * @param {jQuery} $field The field element
  388. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  389. * @returns {BootstrapValidator}
  390. */
  391. validateFieldElement: function($field, updateAll) {
  392. var that = this,
  393. field = $field.attr('data-bv-field'),
  394. validators = this.options.fields[field].validators,
  395. validatorName,
  396. validateResult;
  397. // We don't need to validate disabled, hidden field
  398. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  399. return this;
  400. }
  401. for (validatorName in validators) {
  402. if ($field.data('bv.dfs.' + validatorName)) {
  403. $field.data('bv.dfs.' + validatorName).reject();
  404. }
  405. // Don't validate field if it is already done
  406. var result = $field.data('bv.result.' + validatorName);
  407. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  408. continue;
  409. }
  410. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  411. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  412. if ('object' == typeof validateResult) {
  413. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  414. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  415. $field.data('bv.dfs.' + validatorName, validateResult);
  416. validateResult.done(function($f, v, isValid) {
  417. // v is validator name
  418. $f.removeData('bv.dfs.' + v);
  419. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  420. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  421. if (isValid && that._submitIfValid == true) {
  422. // If a remote validator returns true and the form is ready to submit, then do it
  423. that._submit();
  424. }
  425. });
  426. } else if ('boolean' == typeof validateResult) {
  427. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  428. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  429. }
  430. }
  431. return this;
  432. },
  433. /**
  434. * Update all validating results of elements which have the same field name
  435. *
  436. * @param {String} field The field name
  437. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  438. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  439. * @return {BootstrapValidator}
  440. */
  441. updateStatus: function(field, status, validatorName) {
  442. var fields = this.getFieldElements(field),
  443. type = fields.attr('type'),
  444. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  445. for (var i = 0; i < n; i++) {
  446. this.updateElementStatus($(fields[i]), status, validatorName);
  447. }
  448. return this;
  449. },
  450. /**
  451. * Update validating result of given element
  452. *
  453. * @param {jQuery} $field The field element
  454. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  455. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  456. * @return {BootstrapValidator}
  457. */
  458. updateElementStatus: function($field, status, validatorName) {
  459. var that = this,
  460. field = $field.attr('data-bv-field'),
  461. $parent = $field.parents('.form-group'),
  462. $message = $field.data('bv.messages'),
  463. $rowErrors = $parent.find('.help-block[data-bv-validator]'),
  464. $errors = $message.find('.help-block[data-bv-validator]'),
  465. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  466. // Update status
  467. if (validatorName) {
  468. $field.data('bv.result.' + validatorName, status);
  469. } else {
  470. for (var v in this.options.fields[field].validators) {
  471. $field.data('bv.result.' + v, status);
  472. }
  473. }
  474. // Show/hide error elements and feedback icons
  475. switch (status) {
  476. case this.STATUS_VALIDATING:
  477. this.disableSubmitButtons(true);
  478. $parent.removeClass('has-success').removeClass('has-error');
  479. // TODO: Show validating message
  480. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  481. if ($icon) {
  482. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  483. }
  484. break;
  485. case this.STATUS_INVALID:
  486. this.disableSubmitButtons(true);
  487. $parent.removeClass('has-success').addClass('has-error');
  488. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  489. if ($icon) {
  490. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  491. }
  492. break;
  493. case this.STATUS_VALID:
  494. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  495. // If the field is valid (passes all validators)
  496. var validField = ($errors.filter(function() {
  497. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  498. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  499. }).length == 0);
  500. this.disableSubmitButtons(validField ? false : true);
  501. if ($icon) {
  502. $icon
  503. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  504. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  505. .show();
  506. }
  507. // Check if all fields in the same row are valid
  508. var validRow = ($rowErrors.filter(function() {
  509. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  510. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  511. }).length == 0);
  512. $parent.removeClass('has-error has-success').addClass(validRow ? 'has-success' : 'has-error');
  513. break;
  514. case this.STATUS_NOT_VALIDATED:
  515. default:
  516. this.disableSubmitButtons(false);
  517. $parent.removeClass('has-success').removeClass('has-error');
  518. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  519. if ($icon) {
  520. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  521. }
  522. break;
  523. }
  524. return this;
  525. },
  526. /**
  527. * Check the form validity
  528. *
  529. * @returns {Boolean}
  530. */
  531. isValid: function() {
  532. var fields, field, $field,
  533. type, status, validatorName,
  534. n, i;
  535. for (field in this.options.fields) {
  536. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  537. continue;
  538. }
  539. fields = this.getFieldElements(field);
  540. type = fields.attr('type');
  541. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  542. for (i = 0; i < n; i++) {
  543. $field = $(fields[i]);
  544. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  545. continue;
  546. }
  547. for (validatorName in this.options.fields[field].validators) {
  548. status = $field.data('bv.result.' + validatorName);
  549. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  550. return false;
  551. }
  552. if (status == this.STATUS_INVALID) {
  553. this.$invalidField = $field;
  554. return false;
  555. }
  556. }
  557. }
  558. }
  559. return true;
  560. },
  561. /**
  562. * Submit the form using default submission.
  563. * It also does not perform any validations when submitting the form
  564. *
  565. * It might be used when you want to submit the form right inside the submitHandler()
  566. */
  567. defaultSubmit: function() {
  568. this.$form.off('submit.bv').submit();
  569. },
  570. // Useful APIs which aren't used internally
  571. /**
  572. * Reset the form
  573. *
  574. * @param {Boolean} resetFormData Reset current form data
  575. * @return {BootstrapValidator}
  576. */
  577. resetForm: function(resetFormData) {
  578. var field, fields, total, type, validator;
  579. for (field in this.options.fields) {
  580. fields = this.getFieldElements(field);
  581. total = fields.length;
  582. for (var i = 0; i < total; i++) {
  583. for (validator in this.options.fields[field].validators) {
  584. $(fields[i]).removeData('bv.dfs.' + validator);
  585. }
  586. }
  587. // Mark field as not validated yet
  588. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  589. if (resetFormData) {
  590. type = fields.attr('type');
  591. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  592. }
  593. }
  594. this.$invalidField = null;
  595. this.$submitButton = null;
  596. // Enable submit buttons
  597. this.disableSubmitButtons(false);
  598. return this;
  599. },
  600. /**
  601. * Enable/Disable all validators to given field
  602. *
  603. * @param {String} field The field name
  604. * @param {Boolean} enabled Enable/Disable field validators
  605. * @return {BootstrapValidator}
  606. */
  607. enableFieldValidators: function(field, enabled) {
  608. this.options.fields[field]['enabled'] = enabled;
  609. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  610. return this;
  611. }
  612. };
  613. // Plugin definition
  614. $.fn.bootstrapValidator = function(options) {
  615. return this.each(function() {
  616. var $this = $(this), data = $this.data('bootstrapValidator');
  617. if (!data) {
  618. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  619. }
  620. if ('string' == typeof options) {
  621. data[options]();
  622. }
  623. });
  624. };
  625. // Available validators
  626. $.fn.bootstrapValidator.validators = {};
  627. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  628. }(window.jQuery));
  629. ;(function($) {
  630. $.fn.bootstrapValidator.validators.base64 = {
  631. /**
  632. * Return true if the input value is a base 64 encoded string.
  633. *
  634. * @param {BootstrapValidator} validator The validator plugin instance
  635. * @param {jQuery} $field Field element
  636. * @param {Object} options Can consist of the following keys:
  637. * - message: The invalid message
  638. * @returns {Boolean}
  639. */
  640. validate: function(validator, $field, options) {
  641. var value = $field.val();
  642. if (value == '') {
  643. return true;
  644. }
  645. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  646. }
  647. };
  648. }(window.jQuery));
  649. ;(function($) {
  650. $.fn.bootstrapValidator.validators.between = {
  651. html5Attributes: {
  652. message: 'message',
  653. min: 'min',
  654. max: 'max',
  655. inclusive: 'inclusive'
  656. },
  657. enableByHtml5: function($field) {
  658. if ('range' == $field.attr('type')) {
  659. return {
  660. min: $field.attr('min'),
  661. max: $field.attr('max')
  662. };
  663. }
  664. return false;
  665. },
  666. /**
  667. * Return true if the input value is between (strictly or not) two given numbers
  668. *
  669. * @param {BootstrapValidator} validator The validator plugin instance
  670. * @param {jQuery} $field Field element
  671. * @param {Object} options Can consist of the following keys:
  672. * - min
  673. * - max
  674. * - inclusive [optional]: Can be true or false. Default is true
  675. * - message: The invalid message
  676. * @returns {Boolean}
  677. */
  678. validate: function(validator, $field, options) {
  679. var value = $field.val();
  680. if (value == '') {
  681. return true;
  682. }
  683. value = parseFloat(value);
  684. return (options.inclusive === true)
  685. ? (value > options.min && value < options.max)
  686. : (value >= options.min && value <= options.max);
  687. }
  688. };
  689. }(window.jQuery));
  690. ;(function($) {
  691. $.fn.bootstrapValidator.validators.callback = {
  692. /**
  693. * Return result from the callback method
  694. *
  695. * @param {BootstrapValidator} validator The validator plugin instance
  696. * @param {jQuery} $field Field element
  697. * @param {Object} options Can consist of the following keys:
  698. * - callback: The callback method that passes 2 parameters:
  699. * callback: function(fieldValue, validator) {
  700. * // fieldValue is the value of field
  701. * // validator is instance of BootstrapValidator
  702. * }
  703. * - message: The invalid message
  704. * @returns {Boolean|Deferred}
  705. */
  706. validate: function(validator, $field, options) {
  707. var value = $field.val();
  708. if (options.callback && 'function' == typeof options.callback) {
  709. var dfd = new $.Deferred();
  710. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  711. return dfd;
  712. }
  713. return true;
  714. }
  715. };
  716. }(window.jQuery));
  717. ;(function($) {
  718. $.fn.bootstrapValidator.validators.choice = {
  719. html5Attributes: {
  720. message: 'message',
  721. min: 'min',
  722. max: 'max'
  723. },
  724. /**
  725. * Check if the number of checked boxes are less or more than a given number
  726. *
  727. * @param {BootstrapValidator} validator The validator plugin instance
  728. * @param {jQuery} $field Field element
  729. * @param {Object} options Consists of following keys:
  730. * - min
  731. * - max
  732. * At least one of two keys is required
  733. * - message: The invalid message
  734. * @returns {Boolean}
  735. */
  736. validate: function(validator, $field, options) {
  737. var numChoices = validator
  738. .getFieldElements($field.attr('data-bv-field'))
  739. .filter(':checked')
  740. .length;
  741. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  742. return false;
  743. }
  744. return true;
  745. }
  746. };
  747. }(window.jQuery));
  748. ;(function($) {
  749. $.fn.bootstrapValidator.validators.creditCard = {
  750. /**
  751. * Return true if the input value is valid credit card number
  752. * Based on https://gist.github.com/DiegoSalazar/4075533
  753. *
  754. * @param {BootstrapValidator} validator The validator plugin instance
  755. * @param {jQuery} $field Field element
  756. * @param {Object} options Can consist of the following key:
  757. * - message: The invalid message
  758. * @returns {Boolean}
  759. */
  760. validate: function(validator, $field, options) {
  761. var value = $field.val();
  762. if (value == '') {
  763. return true;
  764. }
  765. // Accept only digits, dashes or spaces
  766. if (/[^0-9-\s]+/.test(value)) {
  767. return false;
  768. }
  769. value = value.replace(/\D/g, '');
  770. // Validate the check sum
  771. // The Luhn Algorithm
  772. // http://en.wikipedia.org/wiki/Luhn
  773. var check = 0, digit = 0, even = false, length = value.length;
  774. for (var n = length - 1; n >= 0; n--) {
  775. digit = parseInt(value.charAt(n), 10);
  776. if (even) {
  777. if ((digit *= 2) > 9) {
  778. digit -= 9;
  779. }
  780. }
  781. check += digit;
  782. even = !even;
  783. }
  784. if ((check % 10) != 0) {
  785. return false;
  786. }
  787. // Validate the card number based on prefix (IIN ranges) and length
  788. var cards = {
  789. AMERICAN_EXPRESS: {
  790. length: [15],
  791. prefix: ['34', '37']
  792. },
  793. DINERS_CLUB: {
  794. length: [14],
  795. prefix: ['300', '301', '302', '303', '304', '305', '36']
  796. },
  797. DINERS_CLUB_US: {
  798. length: [16],
  799. prefix: ['54', '55']
  800. },
  801. DISCOVER: {
  802. length: [16],
  803. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  804. '62214', '62215', '62216', '62217', '62218', '62219',
  805. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  806. '62290', '62291', '622920', '622921', '622922', '622923',
  807. '622924', '622925', '644', '645', '646', '647', '648',
  808. '649', '65']
  809. },
  810. JCB: {
  811. length: [16],
  812. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  813. },
  814. LASER: {
  815. length: [16, 17, 18, 19],
  816. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  817. },
  818. MAESTRO: {
  819. length: [12, 13, 14, 15, 16, 17, 18, 19],
  820. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  821. },
  822. MASTERCARD: {
  823. length: [16],
  824. prefix: ['51', '52', '53', '54', '55']
  825. },
  826. SOLO: {
  827. length: [16, 18, 19],
  828. prefix: ['6334', '6767']
  829. },
  830. UNIONPAY: {
  831. length: [16, 17, 18, 19],
  832. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  833. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  834. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  835. '622920', '622921', '622922', '622923', '622924', '622925']
  836. },
  837. VISA: {
  838. length: [16],
  839. prefix: ['4']
  840. }
  841. };
  842. var type, i;
  843. for (type in cards) {
  844. for (i in cards[type]['prefix']) {
  845. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  846. && cards[type]['length'].indexOf(value.length) != -1) // and length
  847. {
  848. return true;
  849. }
  850. }
  851. }
  852. return false;
  853. }
  854. };
  855. }(window.jQuery));
  856. ;(function($) {
  857. $.fn.bootstrapValidator.validators.cvv = {
  858. html5Attributes: {
  859. message: 'message',
  860. ccfield: 'creditCardField'
  861. },
  862. /**
  863. * Return true if the input value is a valid CVV number.
  864. *
  865. * @param {BootstrapValidator} validator The validator plugin instance
  866. * @param {jQuery} $field Field element
  867. * @param {Object} options Can consist of the following keys:
  868. * - creditCardField: The credit card number field. It can be null
  869. * - message: The invalid message
  870. * @returns {Boolean}
  871. */
  872. validate: function(validator, $field, options) {
  873. var value = $field.val();
  874. if (value == '') {
  875. return true;
  876. }
  877. if (!/^[0-9]{3,4}$/.test(value)) {
  878. return false;
  879. }
  880. if (!options.creditCardField) {
  881. return true;
  882. }
  883. // Get the credit card number
  884. var creditCard = validator.getFieldElements(options.creditCardField).val();
  885. if (creditCard == '') {
  886. return true;
  887. }
  888. // Supported credit card types
  889. var cards = {
  890. AMERICAN_EXPRESS: {
  891. length: [15],
  892. prefix: ['34', '37']
  893. },
  894. DINERS_CLUB: {
  895. length: [14],
  896. prefix: ['300', '301', '302', '303', '304', '305', '36']
  897. },
  898. DINERS_CLUB_US: {
  899. length: [16],
  900. prefix: ['54', '55']
  901. },
  902. DISCOVER: {
  903. length: [16],
  904. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  905. '62214', '62215', '62216', '62217', '62218', '62219',
  906. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  907. '62290', '62291', '622920', '622921', '622922', '622923',
  908. '622924', '622925', '644', '645', '646', '647', '648',
  909. '649', '65']
  910. },
  911. JCB: {
  912. length: [16],
  913. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  914. },
  915. LASER: {
  916. length: [16, 17, 18, 19],
  917. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  918. },
  919. MAESTRO: {
  920. length: [12, 13, 14, 15, 16, 17, 18, 19],
  921. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  922. },
  923. MASTERCARD: {
  924. length: [16],
  925. prefix: ['51', '52', '53', '54', '55']
  926. },
  927. SOLO: {
  928. length: [16, 18, 19],
  929. prefix: ['6334', '6767']
  930. },
  931. UNIONPAY: {
  932. length: [16, 17, 18, 19],
  933. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  934. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  935. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  936. '622920', '622921', '622922', '622923', '622924', '622925']
  937. },
  938. VISA: {
  939. length: [16],
  940. prefix: ['4']
  941. }
  942. };
  943. var type, i, creditCardType = null;
  944. for (type in cards) {
  945. for (i in cards[type]['prefix']) {
  946. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  947. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  948. {
  949. creditCardType = type;
  950. break;
  951. }
  952. }
  953. }
  954. return (creditCardType == null)
  955. ? false
  956. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  957. }
  958. };
  959. }(window.jQuery));
  960. ;(function($) {
  961. $.fn.bootstrapValidator.validators.date = {
  962. html5Attributes: {
  963. message: 'message',
  964. format: 'format'
  965. },
  966. /**
  967. * Return true if the input value is valid date
  968. *
  969. * @param {BootstrapValidator} validator The validator plugin instance
  970. * @param {jQuery} $field Field element
  971. * @param {Object} options Can consist of the following keys:
  972. * - format: The date format. Default is MM/DD/YYYY
  973. * Support the following formats:
  974. * YYYY/DD/MM
  975. * YYYY/DD/MM h:m A
  976. * YYYY/MM/DD
  977. * YYYY/MM/DD h:m A
  978. *
  979. * YYYY-DD-MM
  980. * YYYY-DD-MM h:m A
  981. * YYYY-MM-DD
  982. * YYYY-MM-DD h:m A
  983. *
  984. * MM/DD/YYYY
  985. * MM/DD/YYYY h:m A
  986. * DD/MM/YYYY
  987. * DD/MM/YYYY h:m A
  988. *
  989. * MM-DD-YYYY
  990. * MM-DD-YYYY h:m A
  991. * DD-MM-YYYY
  992. * DD-MM-YYYY h:m A
  993. * - message: The invalid message
  994. * @returns {Boolean}
  995. */
  996. validate: function(validator, $field, options) {
  997. var value = $field.val();
  998. if (value == '') {
  999. return true;
  1000. }
  1001. // Determine the separator
  1002. options.format = options.format || 'MM/DD/YYYY';
  1003. var separator = (options.format.indexOf('/') != -1)
  1004. ? '/'
  1005. : ((options.format.indexOf('-') != -1) ? '-' : null);
  1006. if (separator == null) {
  1007. return false;
  1008. }
  1009. var month, day, year, minutes = null, hours = null, matches;
  1010. switch (true) {
  1011. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  1012. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  1013. year = matches[1]; day = matches[2]; month = matches[3];
  1014. break;
  1015. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  1016. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  1017. day = matches[1]; month = matches[2]; year = matches[3];
  1018. break;
  1019. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1020. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1021. year = matches[1]; month = matches[2]; day = matches[3];
  1022. break;
  1023. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1024. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1025. month = matches[1]; day = matches[2]; year = matches[3];
  1026. break;
  1027. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY/DD/MM h:m A'):
  1028. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY-DD-MM h:m A'):
  1029. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1030. break;
  1031. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'DD/MM/YYYY h:m A'):
  1032. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'DD-MM-YYYY h:m A'):
  1033. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1034. break;
  1035. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY/MM/DD h:m A'):
  1036. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY-MM-DD h:m A'):
  1037. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1038. break;
  1039. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'MM/DD/YYYY h:m A'):
  1040. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'MM-DD-YYYY h:m A'):
  1041. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1042. break;
  1043. default:
  1044. return false;
  1045. }
  1046. // Validate hours and minutes
  1047. if (hours && minutes) {
  1048. hours = parseInt(hours, 10);
  1049. minutes = parseInt(minutes, 10);
  1050. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1051. return false;
  1052. }
  1053. }
  1054. // Validate day, month, and year
  1055. day = parseInt(day, 10);
  1056. month = parseInt(month, 10);
  1057. year = parseInt(year, 10);
  1058. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1059. return false;
  1060. }
  1061. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1062. // Update the number of days in Feb of leap year
  1063. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1064. numDays[1] = 29;
  1065. }
  1066. // Check the day
  1067. return (day > 0 && day <= numDays[month - 1]);
  1068. }
  1069. };
  1070. }(window.jQuery));
  1071. ;(function($) {
  1072. $.fn.bootstrapValidator.validators.different = {
  1073. html5Attributes: {
  1074. message: 'message',
  1075. field: 'field'
  1076. },
  1077. /**
  1078. * Return true if the input value is different with given field's value
  1079. *
  1080. * @param {BootstrapValidator} validator The validator plugin instance
  1081. * @param {jQuery} $field Field element
  1082. * @param {Object} options Consists of the following key:
  1083. * - field: The name of field that will be used to compare with current one
  1084. * - message: The invalid message
  1085. * @returns {Boolean}
  1086. */
  1087. validate: function(validator, $field, options) {
  1088. var value = $field.val();
  1089. if (value == '') {
  1090. return true;
  1091. }
  1092. var compareWith = validator.getFieldElements(options.field);
  1093. if (compareWith == null) {
  1094. return true;
  1095. }
  1096. if (value != compareWith.val()) {
  1097. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1098. return true;
  1099. } else {
  1100. return false;
  1101. }
  1102. }
  1103. };
  1104. }(window.jQuery));
  1105. ;(function($) {
  1106. $.fn.bootstrapValidator.validators.digits = {
  1107. /**
  1108. * Return true if the input value contains digits only
  1109. *
  1110. * @param {BootstrapValidator} validator Validate plugin instance
  1111. * @param {jQuery} $field Field element
  1112. * @param {Object} options
  1113. * @returns {Boolean}
  1114. */
  1115. validate: function(validator, $field, options) {
  1116. var value = $field.val();
  1117. if (value == '') {
  1118. return true;
  1119. }
  1120. return /^\d+$/.test(value);
  1121. }
  1122. }
  1123. }(window.jQuery));
  1124. ;(function($) {
  1125. $.fn.bootstrapValidator.validators.emailAddress = {
  1126. enableByHtml5: function($field) {
  1127. return ('email' == $field.attr('type'));
  1128. },
  1129. /**
  1130. * Return true if and only if the input value is a valid email address
  1131. *
  1132. * @param {BootstrapValidator} validator Validate plugin instance
  1133. * @param {jQuery} $field Field element
  1134. * @param {Object} options
  1135. * @returns {Boolean}
  1136. */
  1137. validate: function(validator, $field, options) {
  1138. var value = $field.val();
  1139. if (value == '') {
  1140. return true;
  1141. }
  1142. // Email address regular expression
  1143. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1144. var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  1145. return emailRegExp.test(value);
  1146. }
  1147. }
  1148. }(window.jQuery));
  1149. ;(function($) {
  1150. $.fn.bootstrapValidator.validators.greaterThan = {
  1151. html5Attributes: {
  1152. message: 'message',
  1153. value: 'value',
  1154. inclusive: 'inclusive'
  1155. },
  1156. enableByHtml5: function($field) {
  1157. var min = $field.attr('min');
  1158. if (min) {
  1159. return {
  1160. value: min
  1161. };
  1162. }
  1163. return false;
  1164. },
  1165. /**
  1166. * Return true if the input value is greater than or equals to given number
  1167. *
  1168. * @param {BootstrapValidator} validator Validate plugin instance
  1169. * @param {jQuery} $field Field element
  1170. * @param {Object} options Can consist of the following keys:
  1171. * - value: The number used to compare to
  1172. * - inclusive [optional]: Can be true or false. Default is true
  1173. * - message: The invalid message
  1174. * @returns {Boolean}
  1175. */
  1176. validate: function(validator, $field, options) {
  1177. var value = $field.val();
  1178. if (value == '') {
  1179. return true;
  1180. }
  1181. value = parseFloat(value);
  1182. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1183. }
  1184. }
  1185. }(window.jQuery));
  1186. ;(function($) {
  1187. $.fn.bootstrapValidator.validators.hex = {
  1188. /**
  1189. * Return true if and only if the input value is a valid hexadecimal number
  1190. *
  1191. * @param {BootstrapValidator} validator The validator plugin instance
  1192. * @param {jQuery} $field Field element
  1193. * @param {Object} options Consist of key:
  1194. * - message: The invalid message
  1195. * @returns {Boolean}
  1196. */
  1197. validate: function(validator, $field, options) {
  1198. var value = $field.val();
  1199. if (value == '') {
  1200. return true;
  1201. }
  1202. return /^[0-9a-fA-F]+$/.test(value);
  1203. }
  1204. };
  1205. }(window.jQuery));
  1206. ;(function($) {
  1207. $.fn.bootstrapValidator.validators.hexColor = {
  1208. enableByHtml5: function($field) {
  1209. return ('color' == $field.attr('type'));
  1210. },
  1211. /**
  1212. * Return true if the input value is a valid hex color
  1213. *
  1214. * @param {BootstrapValidator} validator The validator plugin instance
  1215. * @param {jQuery} $field Field element
  1216. * @param {Object} options Can consist of the following keys:
  1217. * - message: The invalid message
  1218. * @returns {Boolean}
  1219. */
  1220. validate: function(validator, $field, options) {
  1221. var value = $field.val();
  1222. if (value == '') {
  1223. return true;
  1224. }
  1225. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1226. }
  1227. };
  1228. }(window.jQuery));
  1229. ;(function($) {
  1230. $.fn.bootstrapValidator.validators.iban = {
  1231. html5Attributes: {
  1232. message: 'message',
  1233. country: 'country'
  1234. },
  1235. /**
  1236. * Validate an International Bank Account Number (IBAN)
  1237. * To test it, take the sample IBAN from
  1238. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1239. *
  1240. * @param {BootstrapValidator} validator The validator plugin instance
  1241. * @param {jQuery} $field Field element
  1242. * @param {Object} options Can consist of the following keys:
  1243. * - message: The invalid message
  1244. * - country: The ISO 3166-1 country code
  1245. * @returns {Boolean}
  1246. */
  1247. validate: function(validator, $field, options) {
  1248. var value = $field.val();
  1249. if (value == '') {
  1250. return true;
  1251. }
  1252. // See
  1253. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1254. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1255. var ibanRegex = {
  1256. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1257. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1258. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1259. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1260. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1261. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1262. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1263. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1264. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1265. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1266. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1267. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1268. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1269. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1270. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1271. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1272. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1273. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1274. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1275. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1276. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1277. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1278. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1279. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1280. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1281. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1282. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1283. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1284. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1285. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1286. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1287. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1288. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1289. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1290. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1291. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1292. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1293. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1294. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1295. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1296. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1297. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1298. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1299. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1300. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1301. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1302. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1303. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1304. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1305. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1306. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1307. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1308. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1309. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1310. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1311. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1312. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1313. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1314. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1315. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1316. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1317. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1318. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1319. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1320. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1321. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1322. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1323. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1324. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1325. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1326. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1327. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1328. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1329. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1330. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1331. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1332. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1333. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1334. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1335. };
  1336. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1337. var country = options.country || value.substr(0, 2);
  1338. if (!ibanRegex[country]) {
  1339. return false;
  1340. }
  1341. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1342. return false;
  1343. }
  1344. value = value.substr(4) + value.substr(0, 4);
  1345. value = value.split('').map(function(n) {
  1346. var code = n.charCodeAt(0);
  1347. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1348. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1349. ? (code - 'A'.charCodeAt(0) + 10)
  1350. : n;
  1351. }).join('');
  1352. var temp = parseInt(value.substr(0, 1), 10),
  1353. length = value.length;
  1354. for (var i = 1; i < length; ++i) {
  1355. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1356. }
  1357. return (temp == 1);
  1358. }
  1359. };
  1360. }(window.jQuery));
  1361. ;(function($) {
  1362. $.fn.bootstrapValidator.validators.identical = {
  1363. html5Attributes: {
  1364. message: 'message',
  1365. field: 'field'
  1366. },
  1367. /**
  1368. * Check if input value equals to value of particular one
  1369. *
  1370. * @param {BootstrapValidator} validator The validator plugin instance
  1371. * @param {jQuery} $field Field element
  1372. * @param {Object} options Consists of the following key:
  1373. * - field: The name of field that will be used to compare with current one
  1374. * @returns {Boolean}
  1375. */
  1376. validate: function(validator, $field, options) {
  1377. var value = $field.val();
  1378. if (value == '') {
  1379. return true;
  1380. }
  1381. var compareWith = validator.getFieldElements(options.field);
  1382. if (compareWith == null) {
  1383. return true;
  1384. }
  1385. if (value == compareWith.val()) {
  1386. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1387. return true;
  1388. } else {
  1389. return false;
  1390. }
  1391. }
  1392. };
  1393. }(window.jQuery));
  1394. ;(function($) {
  1395. $.fn.bootstrapValidator.validators.integer = {
  1396. enableByHtml5: function($field) {
  1397. return ('number' == $field.attr('type'));
  1398. },
  1399. /**
  1400. * Return true if the input value is an integer
  1401. *
  1402. * @param {BootstrapValidator} validator The validator plugin instance
  1403. * @param {jQuery} $field Field element
  1404. * @param {Object} options Can consist of the following key:
  1405. * - message: The invalid message
  1406. * @returns {Boolean}
  1407. */
  1408. validate: function(validator, $field, options) {
  1409. var value = $field.val();
  1410. if (value == '') {
  1411. return true;
  1412. }
  1413. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1414. }
  1415. };
  1416. }(window.jQuery));
  1417. ;(function($) {
  1418. $.fn.bootstrapValidator.validators.ip = {
  1419. html5Attributes: {
  1420. message: 'message',
  1421. ipv4: 'ipv4',
  1422. ipv6: 'ipv6'
  1423. },
  1424. /**
  1425. * Return true if the input value is a IP address.
  1426. *
  1427. * @param {BootstrapValidator} validator The validator plugin instance
  1428. * @param {jQuery} $field Field element
  1429. * @param {Object} options Can consist of the following keys:
  1430. * - ipv4: Enable IPv4 validator, default to true
  1431. * - ipv6: Enable IPv6 validator, default to true
  1432. * - message: The invalid message
  1433. * @returns {Boolean}
  1434. */
  1435. validate: function(validator, $field, options) {
  1436. var value = $field.val();
  1437. if (value == '') {
  1438. return true;
  1439. }
  1440. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1441. if (options.ipv4) {
  1442. return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value);
  1443. } else if (options.ipv6) {
  1444. return /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/.test(str);
  1445. }
  1446. return false;
  1447. }
  1448. };
  1449. }(window.jQuery));
  1450. ;(function($) {
  1451. $.fn.bootstrapValidator.validators.isbn = {
  1452. /**
  1453. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1454. *
  1455. * @param {BootstrapValidator} validator The validator plugin instance
  1456. * @param {jQuery} $field Field element
  1457. * @param {Object} options Can consist of the following keys:
  1458. * - message: The invalid message
  1459. * @returns {Boolean}
  1460. */
  1461. validate: function(validator, $field, options) {
  1462. var value = $field.val();
  1463. if (value == '') {
  1464. return true;
  1465. }
  1466. // Replace all special characters except digits and X
  1467. value = value.replace(/[^\dX]/gi, '');
  1468. var chars = value.split(''),
  1469. sum = 0,
  1470. checksum;
  1471. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1472. switch (chars.length) {
  1473. // ISBN 10
  1474. case 10:
  1475. sum = 0;
  1476. for (var i = 0; i < 9; i++) {
  1477. sum += ((10 - i) * parseInt(chars[i]));
  1478. }
  1479. checksum = 11 - (sum % 11);
  1480. if (checksum == 11) {
  1481. checksum = 0;
  1482. } else if (checksum == 10) {
  1483. checksum = 'X';
  1484. }
  1485. return (checksum == chars[9]);
  1486. // ISBN 13
  1487. case 13:
  1488. sum = 0;
  1489. for (var i = 0; i < 12; i++) {
  1490. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1491. }
  1492. checksum = 10 - (sum % 10);
  1493. if (checksum == 10) {
  1494. checksum = '0';
  1495. }
  1496. return (checksum == chars[12]);
  1497. default:
  1498. return false;
  1499. }
  1500. }
  1501. };
  1502. }(window.jQuery));
  1503. ;(function($) {
  1504. $.fn.bootstrapValidator.validators.lessThan = {
  1505. html5Attributes: {
  1506. message: 'message',
  1507. value: 'value',
  1508. inclusive: 'inclusive'
  1509. },
  1510. enableByHtml5: function($field) {
  1511. var max = $field.attr('max');
  1512. if (max) {
  1513. return {
  1514. value: max
  1515. };
  1516. }
  1517. return false;
  1518. },
  1519. /**
  1520. * Return true if the input value is less than or equal to given number
  1521. *
  1522. * @param {BootstrapValidator} validator The validator plugin instance
  1523. * @param {jQuery} $field Field element
  1524. * @param {Object} options Can consist of the following keys:
  1525. * - value: The number used to compare to
  1526. * - inclusive [optional]: Can be true or false. Default is true
  1527. * - message: The invalid message
  1528. * @returns {Boolean}
  1529. */
  1530. validate: function(validator, $field, options) {
  1531. var value = $field.val();
  1532. if (value == '') {
  1533. return true;
  1534. }
  1535. value = parseFloat(value);
  1536. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1537. }
  1538. };
  1539. }(window.jQuery));
  1540. ;(function($) {
  1541. $.fn.bootstrapValidator.validators.mac = {
  1542. /**
  1543. * Return true if the input value is a MAC address.
  1544. *
  1545. * @param {BootstrapValidator} validator The validator plugin instance
  1546. * @param {jQuery} $field Field element
  1547. * @param {Object} options Can consist of the following keys:
  1548. * - message: The invalid message
  1549. * @returns {Boolean}
  1550. */
  1551. validate: function(validator, $field, options) {
  1552. var value = $field.val();
  1553. if (value == '') {
  1554. return true;
  1555. }
  1556. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1557. }
  1558. };
  1559. }(window.jQuery));
  1560. ;(function($) {
  1561. $.fn.bootstrapValidator.validators.notEmpty = {
  1562. enableByHtml5: function($field) {
  1563. var required = $field.attr('required') + '';
  1564. return ('required' == required || 'true' == required);
  1565. },
  1566. /**
  1567. * Check if input value is empty or not
  1568. *
  1569. * @param {BootstrapValidator} validator The validator plugin instance
  1570. * @param {jQuery} $field Field element
  1571. * @param {Object} options
  1572. * @returns {Boolean}
  1573. */
  1574. validate: function(validator, $field, options) {
  1575. var type = $field.attr('type');
  1576. if ('radio' == type || 'checkbox' == type) {
  1577. return validator
  1578. .getFieldElements($field.attr('data-bv-field'))
  1579. .filter(':checked')
  1580. .length > 0;
  1581. }
  1582. return $.trim($field.val()) != '';
  1583. }
  1584. };
  1585. }(window.jQuery));
  1586. ;(function($) {
  1587. $.fn.bootstrapValidator.validators.numeric = {
  1588. /**
  1589. * Validate decimal number
  1590. *
  1591. * @param {BootstrapValidator} validator The validator plugin instance
  1592. * @param {jQuery} $field Field element
  1593. * @param {Object} options Consist of key:
  1594. * - message: The invalid message
  1595. * @returns {Boolean}
  1596. */
  1597. validate: function(validator, $field, options) {
  1598. var value = $field.val();
  1599. if (value == '') {
  1600. return true;
  1601. }
  1602. return !isNaN(parseFloat(value)) && isFinite(value);
  1603. }
  1604. };
  1605. }(window.jQuery));
  1606. ;(function($) {
  1607. $.fn.bootstrapValidator.validators.phone = {
  1608. html5Attributes: {
  1609. message: 'message',
  1610. country: 'country'
  1611. },
  1612. /**
  1613. * Return true if the input value contains a valid US phone number only
  1614. *
  1615. * @param {BootstrapValidator} validator Validate plugin instance
  1616. * @param {jQuery} $field Field element
  1617. * @param {Object} options Consist of key:
  1618. * - message: The invalid message
  1619. * - country: The ISO 3166 country code
  1620. * Currently it only supports United State (US) country
  1621. * @returns {Boolean}
  1622. */
  1623. validate: function(validator, $field, options) {
  1624. var value = $field.val();
  1625. if (value == '') {
  1626. return true;
  1627. }
  1628. var country = (options.country || 'US').toUpperCase();
  1629. switch (country) {
  1630. case 'US':
  1631. default:
  1632. // Make sure US phone numbers have 10 digits
  1633. // May start with 1, +1, or 1-; should discard
  1634. // Area code may be delimited with (), & sections may be delimited with . or -
  1635. // Test: http://regexr.com/38mqi
  1636. value = value.replace(/\(|\)|\s+/g, '');
  1637. return (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length == 10);
  1638. }
  1639. }
  1640. }
  1641. }(window.jQuery));
  1642. ;(function($) {
  1643. $.fn.bootstrapValidator.validators.regexp = {
  1644. html5Attributes: {
  1645. message: 'message',
  1646. regexp: 'regexp'
  1647. },
  1648. enableByHtml5: function($field) {
  1649. var pattern = $field.attr('pattern');
  1650. if (pattern) {
  1651. return {
  1652. regexp: pattern
  1653. };
  1654. }
  1655. return false;
  1656. },
  1657. /**
  1658. * Check if the element value matches given regular expression
  1659. *
  1660. * @param {BootstrapValidator} validator The validator plugin instance
  1661. * @param {jQuery} $field Field element
  1662. * @param {Object} options Consists of the following key:
  1663. * - regexp: The regular expression you need to check
  1664. * @returns {Boolean}
  1665. */
  1666. validate: function(validator, $field, options) {
  1667. var value = $field.val();
  1668. if (value == '') {
  1669. return true;
  1670. }
  1671. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1672. return regexp.test(value);
  1673. }
  1674. };
  1675. }(window.jQuery));
  1676. ;(function($) {
  1677. $.fn.bootstrapValidator.validators.remote = {
  1678. html5Attributes: {
  1679. message: 'message',
  1680. url: 'url'
  1681. },
  1682. /**
  1683. * Request a remote server to check the input value
  1684. *
  1685. * @param {BootstrapValidator} validator Plugin instance
  1686. * @param {jQuery} $field Field element
  1687. * @param {Object} options Can consist of the following keys:
  1688. * - url
  1689. * - data [optional]: By default, it will take the value
  1690. * {
  1691. * <fieldName>: <fieldValue>
  1692. * }
  1693. * - message: The invalid message
  1694. * @returns {Boolean|Deferred}
  1695. */
  1696. validate: function(validator, $field, options) {
  1697. var value = $field.val();
  1698. if (value == '') {
  1699. return true;
  1700. }
  1701. var name = $field.attr('data-bv-field'), data = options.data;
  1702. if (data == null) {
  1703. data = {};
  1704. }
  1705. // Support dynamic data
  1706. if ('function' == typeof data) {
  1707. data = data.call(this, validator);
  1708. }
  1709. data[name] = value;
  1710. var dfd = new $.Deferred();
  1711. var xhr = $.ajax({
  1712. type: 'POST',
  1713. url: options.url,
  1714. dataType: 'json',
  1715. data: data
  1716. });
  1717. xhr.then(function(response) {
  1718. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1719. });
  1720. dfd.fail(function() {
  1721. xhr.abort();
  1722. });
  1723. return dfd;
  1724. }
  1725. };
  1726. }(window.jQuery));
  1727. ;(function($) {
  1728. $.fn.bootstrapValidator.validators.siret = {
  1729. /**
  1730. * Check if a string is a siren number
  1731. *
  1732. * @param {BootstrapValidator} validator The validator plugin instance
  1733. * @param {jQuery} $field Field element
  1734. * @param {Object} options Consist of key:
  1735. * - message: The invalid message
  1736. * @returns {Boolean}
  1737. */
  1738. validate : function(validator, $field, options) {
  1739. var value = $field.val();
  1740. if (value == '') {
  1741. return true;
  1742. }
  1743. var sum = 0,
  1744. length = value.length,
  1745. tmp;
  1746. for (var i = 0; i < length; i++) {
  1747. if ((i % 2) == 1) {
  1748. tmp = value.charAt(i) * 2;
  1749. if (tmp > 9) {
  1750. tmp -= 9;
  1751. }
  1752. } else {
  1753. tmp = value.charAt(i);
  1754. }
  1755. sum += parseInt(tmp);
  1756. }
  1757. return ((sum % 10) == 0);
  1758. }
  1759. };
  1760. }(window.jQuery));;(function($) {
  1761. $.fn.bootstrapValidator.validators.siret = {
  1762. /**
  1763. * Check if a string is a siret number
  1764. *
  1765. * @param {BootstrapValidator} validator The validator plugin instance
  1766. * @param {jQuery} $field Field element
  1767. * @param {Object} options Consist of key:
  1768. * - message: The invalid message
  1769. * @returns {Boolean}
  1770. */
  1771. validate : function(validator, $field, options) {
  1772. var value = $field.val();
  1773. if (value == '') {
  1774. return true;
  1775. }
  1776. var sum = 0,
  1777. length = value.length,
  1778. tmp;
  1779. for (var i = 0; i < length; i++) {
  1780. if ((i % 2) == 0) {
  1781. tmp = value.charAt(i) * 2;
  1782. if (tmp > 9) {
  1783. tmp -= 9;
  1784. }
  1785. } else {
  1786. tmp = value.charAt(i);
  1787. }
  1788. sum += parseInt(tmp);
  1789. }
  1790. return ((sum % 10) == 0);
  1791. }
  1792. };
  1793. }(window.jQuery));;(function($) {
  1794. $.fn.bootstrapValidator.validators.step = {
  1795. html5Attributes: {
  1796. message: 'message',
  1797. base: 'baseValue',
  1798. step: 'step'
  1799. },
  1800. /**
  1801. * Return true if the input value is valid step one
  1802. *
  1803. * @param {BootstrapValidator} validator The validator plugin instance
  1804. * @param {jQuery} $field Field element
  1805. * @param {Object} options Can consist of the following keys:
  1806. * - baseValue: The base value
  1807. * - step: The step
  1808. * - message: The invalid message
  1809. * @returns {Boolean}
  1810. */
  1811. validate: function(validator, $field, options) {
  1812. var value = $field.val();
  1813. if (value == '') {
  1814. return true;
  1815. }
  1816. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1817. value = parseFloat(value);
  1818. if (isNaN(value) || !isFinite(value)) {
  1819. return false;
  1820. }
  1821. var round = function(x, precision) {
  1822. var m = Math.pow(10, precision);
  1823. x = x * m;
  1824. var sign = (x > 0) | -(x < 0),
  1825. isHalf = (x % 1 === 0.5 * sign);
  1826. if (isHalf) {
  1827. return (Math.floor(x) + (sign > 0)) / m;
  1828. } else {
  1829. return Math.round(x) / m;
  1830. }
  1831. },
  1832. floatMod = function(x, y) {
  1833. if (y == 0.0) {
  1834. return 1.0;
  1835. }
  1836. var dotX = (x + '').split('.'),
  1837. dotY = (y + '').split('.'),
  1838. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1839. return round(x - y * Math.floor(x / y), precision);
  1840. };
  1841. var mod = floatMod(value - options.baseValue, options.step);
  1842. return (mod == 0.0 || mod == options.step);
  1843. }
  1844. };
  1845. }(window.jQuery));
  1846. ;(function($) {
  1847. $.fn.bootstrapValidator.validators.stringCase = {
  1848. html5Attributes: {
  1849. message: 'message',
  1850. 'case': 'case'
  1851. },
  1852. /**
  1853. * Check if a string is a lower or upper case one
  1854. *
  1855. * @param {BootstrapValidator} validator The validator plugin instance
  1856. * @param {jQuery} $field Field element
  1857. * @param {Object} options Consist of key:
  1858. * - message: The invalid message
  1859. * - case: Can be 'lower' (default) or 'upper'
  1860. * @returns {Boolean}
  1861. */
  1862. validate: function(validator, $field, options) {
  1863. var value = $field.val();
  1864. if (value == '') {
  1865. return true;
  1866. }
  1867. var stringCase = (options['case'] || 'lower').toLowerCase();
  1868. switch (stringCase) {
  1869. case 'upper':
  1870. return value === value.toUpperCase();
  1871. case 'lower':
  1872. default:
  1873. return value === value.toLowerCase();
  1874. }
  1875. }
  1876. };
  1877. }(window.jQuery));
  1878. ;(function($) {
  1879. $.fn.bootstrapValidator.validators.stringLength = {
  1880. html5Attributes: {
  1881. message: 'message',
  1882. min: 'min',
  1883. max: 'max'
  1884. },
  1885. enableByHtml5: function($field) {
  1886. var maxLength = $field.attr('maxlength');
  1887. if (maxLength) {
  1888. return {
  1889. max: parseInt(maxLength, 10)
  1890. };
  1891. }
  1892. return false;
  1893. },
  1894. /**
  1895. * Check if the length of element value is less or more than given number
  1896. *
  1897. * @param {BootstrapValidator} validator The validator plugin instance
  1898. * @param {jQuery} $field Field element
  1899. * @param {Object} options Consists of following keys:
  1900. * - min
  1901. * - max
  1902. * At least one of two keys is required
  1903. * - message: The invalid message
  1904. * @returns {Boolean}
  1905. */
  1906. validate: function(validator, $field, options) {
  1907. var value = $field.val();
  1908. if (value == '') {
  1909. return true;
  1910. }
  1911. var length = $.trim(value).length;
  1912. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1913. return false;
  1914. }
  1915. return true;
  1916. }
  1917. };
  1918. }(window.jQuery));
  1919. ;(function($) {
  1920. $.fn.bootstrapValidator.validators.uri = {
  1921. enableByHtml5: function($field) {
  1922. return ('url' == $field.attr('type'));
  1923. },
  1924. /**
  1925. * Return true if the input value is a valid URL
  1926. *
  1927. * @param {BootstrapValidator} validator The validator plugin instance
  1928. * @param {jQuery} $field Field element
  1929. * @param {Object} options
  1930. * @returns {Boolean}
  1931. */
  1932. validate: function(validator, $field, options) {
  1933. var value = $field.val();
  1934. if (value == '') {
  1935. return true;
  1936. }
  1937. // Credit to https://gist.github.com/dperini/729294
  1938. //
  1939. // Regular Expression for URL validation
  1940. //
  1941. // Author: Diego Perini
  1942. // Updated: 2010/12/05
  1943. //
  1944. // the regular expression composed & commented
  1945. // could be easily tweaked for RFC compliance,
  1946. // it was expressly modified to fit & satisfy
  1947. // these test for an URL shortener:
  1948. //
  1949. // http://mathiasbynens.be/demo/url-regex
  1950. //
  1951. // Notes on possible differences from a standard/generic validation:
  1952. //
  1953. // - utf-8 char class take in consideration the full Unicode range
  1954. // - TLDs have been made mandatory so single names like "localhost" fails
  1955. // - protocols have been restricted to ftp, http and https only as requested
  1956. //
  1957. // Changes:
  1958. //
  1959. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1960. // first and last IP address of each class is considered invalid
  1961. // (since they are broadcast/network addresses)
  1962. //
  1963. // - Added exclusion of private, reserved and/or local networks ranges
  1964. //
  1965. // Compressed one-line versions:
  1966. //
  1967. // Javascript version
  1968. //
  1969. // /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i
  1970. //
  1971. // PHP version
  1972. //
  1973. // _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS
  1974. var urlExp = new RegExp(
  1975. "^" +
  1976. // protocol identifier
  1977. "(?:(?:https?|ftp)://)" +
  1978. // user:pass authentication
  1979. "(?:\\S+(?::\\S*)?@)?" +
  1980. "(?:" +
  1981. // IP address exclusion
  1982. // private & local networks
  1983. "(?!10(?:\\.\\d{1,3}){3})" +
  1984. "(?!127(?:\\.\\d{1,3}){3})" +
  1985. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1986. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1987. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1988. // IP address dotted notation octets
  1989. // excludes loopback network 0.0.0.0
  1990. // excludes reserved space >= 224.0.0.0
  1991. // excludes network & broacast addresses
  1992. // (first & last IP address of each class)
  1993. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1994. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1995. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1996. "|" +
  1997. // host name
  1998. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1999. // domain name
  2000. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  2001. // TLD identifier
  2002. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  2003. ")" +
  2004. // port number
  2005. "(?::\\d{2,5})?" +
  2006. // resource path
  2007. "(?:/[^\\s]*)?" +
  2008. "$", "i"
  2009. );
  2010. return urlExp.test(value);
  2011. }
  2012. };
  2013. }(window.jQuery));
  2014. ;(function($) {
  2015. $.fn.bootstrapValidator.validators.uuid = {
  2016. html5Attributes: {
  2017. message: 'message',
  2018. version: 'version'
  2019. },
  2020. /**
  2021. * Return true if and only if the input value is a valid UUID string
  2022. *
  2023. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  2024. * @param {BootstrapValidator} validator The validator plugin instance
  2025. * @param {jQuery} $field Field element
  2026. * @param {Object} options Consist of key:
  2027. * - message: The invalid message
  2028. * - version: Can be 3, 4, 5, null
  2029. * @returns {Boolean}
  2030. */
  2031. validate: function(validator, $field, options) {
  2032. var value = $field.val();
  2033. if (value == '') {
  2034. return true;
  2035. }
  2036. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  2037. var patterns = {
  2038. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  2039. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2040. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2041. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  2042. },
  2043. version = options.version ? (options.version + '') : 'all';
  2044. return (null == patterns[version]) ? true : patterns[version].test(value);
  2045. }
  2046. };
  2047. }(window.jQuery));
  2048. ;(function($) {
  2049. $.fn.bootstrapValidator.validators.zipCode = {
  2050. html5Attributes: {
  2051. message: 'message',
  2052. country: 'country'
  2053. },
  2054. /**
  2055. * Return true if and only if the input value is a valid country zip code
  2056. *
  2057. * @param {BootstrapValidator} validator The validator plugin instance
  2058. * @param {jQuery} $field Field element
  2059. * @param {Object} options Consist of key:
  2060. * - message: The invalid message
  2061. * - country: The ISO 3166 country code
  2062. *
  2063. * Currently it supports the following countries:
  2064. * - US (United State)
  2065. * - DK (Denmark)
  2066. * - SE (Sweden)
  2067. * @returns {Boolean}
  2068. */
  2069. validate: function(validator, $field, options) {
  2070. var value = $field.val();
  2071. if (value == '' || !options.country) {
  2072. return true;
  2073. }
  2074. options.country = options.country || 'US';
  2075. switch (options.country.toUpperCase()) {
  2076. case 'DK':
  2077. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  2078. case 'SE':
  2079. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  2080. case 'US':
  2081. default:
  2082. return /^\d{5}([\-]\d{4})?$/.test(value);
  2083. }
  2084. }
  2085. };
  2086. }(window.jQuery));