bootstrapValidator.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.4.0-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. this._init();
  23. };
  24. // The default options
  25. BootstrapValidator.DEFAULT_OPTIONS = {
  26. // The form CSS class
  27. elementClass: 'bootstrap-validator-form',
  28. // Default invalid message
  29. message: 'This value is not valid',
  30. // Shows ok/error/loading icons based on the field validity.
  31. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  32. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  33. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  34. //
  35. // Examples:
  36. // - Use Glyphicons icons:
  37. // feedbackIcons: {
  38. // valid: 'glyphicon glyphicon-ok',
  39. // invalid: 'glyphicon glyphicon-remove',
  40. // validating: 'glyphicon glyphicon-refresh'
  41. // }
  42. // - Use FontAwesome icons:
  43. // feedbackIcons: {
  44. // valid: 'fa fa-check',
  45. // invalid: 'fa fa-times',
  46. // validating: 'fa fa-refresh'
  47. // }
  48. feedbackIcons: {
  49. valid: null,
  50. invalid: null,
  51. validating: null
  52. },
  53. // The submit buttons selector
  54. // These buttons will be disabled to prevent the valid form from multiple submissions
  55. submitButtons: 'button[type="submit"]',
  56. // The custom submit handler
  57. // It will prevent the form from the default submission
  58. //
  59. // submitHandler: function(validator, form) {
  60. // - validator is the BootstrapValidator instance
  61. // - form is the jQuery object present the current form
  62. // }
  63. submitHandler: null,
  64. // Live validating option
  65. // Can be one of 3 values:
  66. // - enabled: The plugin validates fields as soon as they are changed
  67. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  68. // - submitted: The live validating is enabled after the form is submitted
  69. live: 'enabled',
  70. // Map the field name with validator rules
  71. fields: null
  72. };
  73. BootstrapValidator.prototype = {
  74. constructor: BootstrapValidator,
  75. /**
  76. * Init form
  77. */
  78. _init: function() {
  79. var that = this,
  80. options = {
  81. trigger: this.$form.attr('data-bv-trigger'),
  82. message: this.$form.attr('data-bv-message'),
  83. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  84. live: this.$form.attr('data-bv-live'),
  85. fields: {},
  86. feedbackIcons: {
  87. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  88. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  89. validating: this.$form.attr('data-bv-feedbackicons-validating')
  90. }
  91. },
  92. validator,
  93. i = 0,
  94. v, // Validator name
  95. enabled,
  96. html5AttrName,
  97. optionName,
  98. optionValue,
  99. html5Attrs;
  100. this.$form
  101. // Disable client side validation in HTML 5
  102. .attr('novalidate', 'novalidate')
  103. .addClass(this.options.elementClass)
  104. // Disable the default submission first
  105. .on('submit.bv', function(e) {
  106. e.preventDefault();
  107. that.validate();
  108. })
  109. .on('click', this.options.submitButtons, function() {
  110. that.$submitButton = $(this);
  111. })
  112. // Find all fields which have either "name" or "data-bv-field" attribute
  113. .find('[name], [data-bv-field]').each(function() {
  114. var $field = $(this),
  115. field = $field.attr('name') || $field.attr('data-bv-field');
  116. $field.attr('data-bv-field', field);
  117. options.fields[field] = $.extend({}, {
  118. trigger: $field.attr('data-bv-trigger'),
  119. message: $field.attr('data-bv-message'),
  120. container: $field.attr('data-bv-container'),
  121. selector: $field.attr('data-bv-selector'),
  122. validators: {}
  123. }, options.fields[field]);
  124. for (v in $.fn.bootstrapValidator.validators) {
  125. validator = $.fn.bootstrapValidator.validators[v];
  126. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  127. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  128. if ((html5Attrs && enabled != 'false')
  129. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  130. {
  131. // Try to parse the options via attributes
  132. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  133. options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
  134. for (html5AttrName in validator.html5Attributes) {
  135. optionName = validator.html5Attributes[html5AttrName];
  136. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  137. if (optionValue) {
  138. if ('true' == optionValue) {
  139. optionValue = true;
  140. } else if ('false' == optionValue) {
  141. optionValue = false;
  142. }
  143. options.fields[field]['validators'][v][optionName] = optionValue;
  144. }
  145. }
  146. }
  147. }
  148. });
  149. this.options = $.extend(true, this.options, options);
  150. for (var field in this.options.fields) {
  151. this._initField(field);
  152. }
  153. this.setLiveMode(this.options.live);
  154. },
  155. /**
  156. * Init field
  157. *
  158. * @param {String} field The field name
  159. */
  160. _initField: function(field) {
  161. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  162. return;
  163. }
  164. var fields = this.getFieldElements(field);
  165. // We don't need to validate non-existing fields
  166. if (fields == null) {
  167. delete this.options.fields[field];
  168. return;
  169. }
  170. for (var validatorName in this.options.fields[field].validators) {
  171. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  172. delete this.options.fields[field].validators[validatorName];
  173. }
  174. }
  175. var that = this,
  176. type = fields.attr('type'),
  177. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup',
  178. total = fields.length,
  179. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  180. for (var i = 0; i < total; i++) {
  181. var $field = $(fields[i]),
  182. $parent = $field.parents('.form-group'),
  183. // Allow user to indicate where the error messages are shown
  184. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  185. // Set the attribute to indicate the fields which are defined by selector
  186. if (!$field.attr('data-bv-field')) {
  187. $field.attr('data-bv-field', field);
  188. }
  189. // Whenever the user change the field value, mark it as not validated yet
  190. $field.on(event + '.update.bv', function() {
  191. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  192. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  193. });
  194. // Create help block elements for showing the error messages
  195. $field.data('bv.messages', $message);
  196. for (validatorName in this.options.fields[field].validators) {
  197. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  198. if (!updateAll || i == total - 1) {
  199. $('<small/>')
  200. .css('display', 'none')
  201. .attr('data-bv-validator', validatorName)
  202. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  203. .addClass('help-block')
  204. .appendTo($message);
  205. }
  206. }
  207. // Prepare the feedback icons
  208. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  209. if (this.options.feedbackIcons
  210. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  211. && (!updateAll || i == total - 1))
  212. {
  213. $parent.addClass('has-feedback');
  214. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($field);
  215. // The feedback icon does not render correctly if there is no label
  216. // https://github.com/twbs/bootstrap/issues/12873
  217. if ($parent.find('label').length == 0) {
  218. $icon.css('top', 0);
  219. }
  220. }
  221. }
  222. if (this.options.fields[field]['enabled'] == null) {
  223. this.options.fields[field]['enabled'] = true;
  224. }
  225. },
  226. /**
  227. * Get the element to place the error messages
  228. *
  229. * @param {jQuery} $field The field element
  230. * @returns {jQuery}
  231. */
  232. _getMessageContainer: function($field) {
  233. var $parent = $field.parent();
  234. if ($parent.hasClass('form-group')) {
  235. return $parent;
  236. }
  237. var cssClasses = $parent.attr('class');
  238. if (!cssClasses) {
  239. return this._getMessageContainer($parent);
  240. }
  241. cssClasses = cssClasses.split(' ');
  242. var n = cssClasses.length;
  243. for (var i = 0; i < n; i++) {
  244. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  245. return $parent;
  246. }
  247. }
  248. return this._getMessageContainer($parent);
  249. },
  250. /**
  251. * Called when all validations are completed
  252. */
  253. _submit: function() {
  254. if (!this.isValid()) {
  255. if ('submitted' == this.options.live) {
  256. this.setLiveMode('enabled');
  257. }
  258. // Focus to the first invalid field
  259. if (this.$invalidField) {
  260. this.$invalidField.focus();
  261. }
  262. return;
  263. }
  264. // Call the custom submission if enabled
  265. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  266. // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
  267. this.$form.off('submit.bv');
  268. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  269. } else {
  270. this.disableSubmitButtons(true);
  271. // Submit form
  272. this.$form.off('submit.bv').submit();
  273. }
  274. },
  275. // --- Public methods ---
  276. /**
  277. * Retrieve the field elements by given name
  278. *
  279. * @param {String} field The field name
  280. * @returns {null|jQuery[]}
  281. */
  282. getFieldElements: function(field) {
  283. var fields = this.$form.find(this.options.fields[field].selector || '[name="' + field + '"]');
  284. return (fields.length == 0) ? null : fields;
  285. },
  286. /**
  287. * Set live validating mode
  288. *
  289. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  290. * @returns {BootstrapValidator}
  291. */
  292. setLiveMode: function(mode) {
  293. this.options.live = mode;
  294. if ('submitted' == mode) {
  295. return this;
  296. }
  297. var that = this;
  298. for (var field in this.options.fields) {
  299. (function(f) {
  300. var fields = that.getFieldElements(f);
  301. if (fields) {
  302. var type = fields.attr('type'),
  303. total = fields.length,
  304. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  305. trigger = that.options.fields[field].trigger
  306. || that.options.trigger
  307. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup'),
  308. events = trigger.split(' ').map(function(item) {
  309. return item + '.live.bv';
  310. }).join(' ');
  311. for (var i = 0; i < total; i++) {
  312. ('enabled' == mode)
  313. ? $(fields[i]).on(events, function() {
  314. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  315. })
  316. : $(fields[i]).off(events);
  317. }
  318. }
  319. })(field);
  320. }
  321. return this;
  322. },
  323. /**
  324. * Disable/enable submit buttons
  325. *
  326. * @param {Boolean} disabled Can be true or false
  327. * @returns {BootstrapValidator}
  328. */
  329. disableSubmitButtons: function(disabled) {
  330. if (!disabled) {
  331. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  332. } else if (this.options.live != 'disabled') {
  333. // Don't disable if the live validating mode is disabled
  334. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  335. }
  336. return this;
  337. },
  338. /**
  339. * Validate the form
  340. *
  341. * @return {BootstrapValidator}
  342. */
  343. validate: function() {
  344. if (!this.options.fields) {
  345. return this;
  346. }
  347. this.disableSubmitButtons(true);
  348. for (var field in this.options.fields) {
  349. this.validateField(field);
  350. }
  351. this._submit();
  352. return this;
  353. },
  354. /**
  355. * Validate given field
  356. *
  357. * @param {String} field The field element
  358. * @returns {BootstrapValidator}
  359. */
  360. validateField: function(field) {
  361. var fields = this.getFieldElements(field),
  362. type = fields.attr('type'),
  363. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  364. for (var i = 0; i < n; i++) {
  365. this.validateFieldElement($(fields[i]), (n == 1));
  366. }
  367. return this;
  368. },
  369. /**
  370. * Validate field element
  371. *
  372. * @param {jQuery} $field The field element
  373. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  374. * @returns {BootstrapValidator}
  375. */
  376. validateFieldElement: function($field, updateAll) {
  377. var that = this,
  378. field = $field.attr('data-bv-field'),
  379. validators = this.options.fields[field].validators,
  380. validatorName,
  381. validateResult;
  382. // We don't need to validate disabled field
  383. if ($field.is(':disabled')) {
  384. return this;
  385. }
  386. for (validatorName in validators) {
  387. if ($field.data('bv.dfs.' + validatorName)) {
  388. $field.data('bv.dfs.' + validatorName).reject();
  389. }
  390. // Don't validate field if it is already done
  391. var result = $field.data('bv.result.' + validatorName);
  392. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  393. continue;
  394. }
  395. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  396. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  397. if ('object' == typeof validateResult) {
  398. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  399. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  400. $field.data('bv.dfs.' + validatorName, validateResult);
  401. validateResult.done(function($f, v, isValid) {
  402. // v is validator name
  403. $f.removeData('bv.dfs.' + v);
  404. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  405. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  406. if (isValid && 'disabled' == that.options.live) {
  407. that._submit();
  408. }
  409. });
  410. } else if ('boolean' == typeof validateResult) {
  411. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  412. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  413. }
  414. }
  415. return this;
  416. },
  417. /**
  418. * Update all validating results of elements which have the same field name
  419. *
  420. * @param {String} field The field name
  421. * @param {String} status The status
  422. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  423. * @param {String|null} validatorName The validator name. If null, the method updates validity result for all validators
  424. * @return {BootstrapValidator}
  425. */
  426. updateStatus: function(field, status, validatorName) {
  427. var fields = this.getFieldElements(field),
  428. type = fields.attr('type'),
  429. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  430. for (var i = 0; i < n; i++) {
  431. this.updateElementStatus($(fields[i]), status, validatorName);
  432. }
  433. return this;
  434. },
  435. /**
  436. * Update validating result of given element
  437. *
  438. * @param {String} field The field name
  439. * @param {String} status The status
  440. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  441. * @param {String|null} validatorName The validator name. If null, the method updates validity result for all validators
  442. * @return {BootstrapValidator}
  443. */
  444. updateElementStatus: function($field, status, validatorName) {
  445. var that = this,
  446. field = $field.attr('data-bv-field'),
  447. $parent = $field.parents('.form-group'),
  448. $message = $field.data('bv.messages'),
  449. $errors = $message.find('.help-block[data-bv-validator]'),
  450. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  451. // Update status
  452. if (validatorName) {
  453. $field.data('bv.result.' + validatorName, status);
  454. } else {
  455. for (var v in this.options.fields[field].validators) {
  456. $field.data('bv.result.' + v, status);
  457. }
  458. }
  459. // Show/hide error elements and feedback icons
  460. switch (status) {
  461. case this.STATUS_VALIDATING:
  462. this.disableSubmitButtons(true);
  463. $parent.removeClass('has-success').removeClass('has-error');
  464. // TODO: Show validating message
  465. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  466. if ($icon) {
  467. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  468. }
  469. break;
  470. case this.STATUS_INVALID:
  471. this.disableSubmitButtons(true);
  472. $parent.removeClass('has-success').addClass('has-error');
  473. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  474. if ($icon) {
  475. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  476. }
  477. break;
  478. case this.STATUS_VALID:
  479. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  480. // If the field is valid (passes all validators)
  481. if ($errors.filter(function() {
  482. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  483. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  484. }).length == 0)
  485. {
  486. this.disableSubmitButtons(false);
  487. $parent.removeClass('has-error').addClass('has-success');
  488. if ($icon) {
  489. $icon.removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.valid).show();
  490. }
  491. } else {
  492. this.disableSubmitButtons(true);
  493. $parent.removeClass('has-success').addClass('has-error');
  494. if ($icon) {
  495. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  496. }
  497. }
  498. break;
  499. case this.STATUS_NOT_VALIDATED:
  500. default:
  501. this.disableSubmitButtons(false);
  502. $parent.removeClass('has-success').removeClass('has-error');
  503. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  504. if ($icon) {
  505. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  506. }
  507. break;
  508. }
  509. return this;
  510. },
  511. /**
  512. * Check the form validity
  513. *
  514. * @returns {Boolean}
  515. */
  516. isValid: function() {
  517. var fields, field, $field,
  518. type, status, validatorName,
  519. n, i;
  520. for (field in this.options.fields) {
  521. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  522. continue;
  523. }
  524. fields = this.getFieldElements(field);
  525. type = fields.attr('type');
  526. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  527. for (i = 0; i < n; i++) {
  528. $field = $(fields[i]);
  529. for (validatorName in this.options.fields[field].validators) {
  530. status = $field.data('bv.result.' + validatorName);
  531. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  532. return false;
  533. }
  534. if (status == this.STATUS_INVALID) {
  535. this.$invalidField = $field;
  536. return false;
  537. }
  538. }
  539. }
  540. }
  541. return true;
  542. },
  543. // Useful APIs which aren't used internally
  544. /**
  545. * Reset the form
  546. *
  547. * @param {Boolean} resetFormData Reset current form data
  548. * @return {BootstrapValidator}
  549. */
  550. resetForm: function(resetFormData) {
  551. var field, fields, total, type, validator;
  552. for (field in this.options.fields) {
  553. fields = this.getFieldElements(field);
  554. total = fields.length;
  555. for (var i = 0; i < total; i++) {
  556. for (validator in this.options.fields[field].validators) {
  557. $(fields[i]).removeData('bv.dfs.' + validator);
  558. }
  559. }
  560. // Mark field as not validated yet
  561. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  562. if (resetFormData) {
  563. type = fields.attr('type');
  564. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  565. }
  566. }
  567. this.$invalidField = null;
  568. this.$submitButton = null;
  569. // Enable submit buttons
  570. this.disableSubmitButtons(false);
  571. return this;
  572. },
  573. /**
  574. * Enable/Disable all validators to given field
  575. *
  576. * @param {String} field The field name
  577. * @param {Boolean} enabled Enable/Disable field validators
  578. * @return {BootstrapValidator}
  579. */
  580. enableFieldValidators: function(field, enabled) {
  581. this.options.fields[field]['enabled'] = enabled;
  582. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  583. return this;
  584. }
  585. };
  586. // Plugin definition
  587. $.fn.bootstrapValidator = function(options) {
  588. return this.each(function() {
  589. var $this = $(this), data = $this.data('bootstrapValidator');
  590. if (!data) {
  591. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  592. }
  593. if ('string' == typeof options) {
  594. data[options]();
  595. }
  596. });
  597. };
  598. // Available validators
  599. $.fn.bootstrapValidator.validators = {};
  600. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  601. }(window.jQuery));
  602. ;(function($) {
  603. $.fn.bootstrapValidator.validators.base64 = {
  604. /**
  605. * Return true if the input value is a base 64 encoded string.
  606. *
  607. * @param {BootstrapValidator} validator The validator plugin instance
  608. * @param {jQuery} $field Field element
  609. * @param {Object} options Can consist of the following keys:
  610. * - message: The invalid message
  611. * @returns {Boolean}
  612. */
  613. validate: function(validator, $field, options) {
  614. var value = $field.val();
  615. if (value == '') {
  616. return true;
  617. }
  618. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  619. }
  620. };
  621. }(window.jQuery));
  622. ;(function($) {
  623. $.fn.bootstrapValidator.validators.between = {
  624. html5Attributes: {
  625. message: 'message',
  626. min: 'min',
  627. max: 'max',
  628. inclusive: 'inclusive'
  629. },
  630. /**
  631. * Return true if the input value is between (strictly or not) two given numbers
  632. *
  633. * @param {BootstrapValidator} validator The validator plugin instance
  634. * @param {jQuery} $field Field element
  635. * @param {Object} options Can consist of the following keys:
  636. * - min
  637. * - max
  638. * - inclusive [optional]: Can be true or false. Default is true
  639. * - message: The invalid message
  640. * @returns {Boolean}
  641. */
  642. validate: function(validator, $field, options) {
  643. var value = $field.val();
  644. if (value == '') {
  645. return true;
  646. }
  647. value = parseFloat(value);
  648. return (options.inclusive === true)
  649. ? (value > options.min && value < options.max)
  650. : (value >= options.min && value <= options.max);
  651. }
  652. };
  653. }(window.jQuery));
  654. ;(function($) {
  655. $.fn.bootstrapValidator.validators.callback = {
  656. /**
  657. * Return result from the callback method
  658. *
  659. * @param {BootstrapValidator} validator The validator plugin instance
  660. * @param {jQuery} $field Field element
  661. * @param {Object} options Can consist of the following keys:
  662. * - callback: The callback method that passes 2 parameters:
  663. * callback: function(fieldValue, validator) {
  664. * // fieldValue is the value of field
  665. * // validator is instance of BootstrapValidator
  666. * }
  667. * - message: The invalid message
  668. * @returns {Boolean|Deferred}
  669. */
  670. validate: function(validator, $field, options) {
  671. var value = $field.val();
  672. if (options.callback && 'function' == typeof options.callback) {
  673. var dfd = new $.Deferred();
  674. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  675. return dfd;
  676. }
  677. return true;
  678. }
  679. };
  680. }(window.jQuery));
  681. ;(function($) {
  682. $.fn.bootstrapValidator.validators.choice = {
  683. html5Attributes: {
  684. message: 'message',
  685. min: 'min',
  686. max: 'max'
  687. },
  688. /**
  689. * Check if the number of checked boxes are less or more than a given number
  690. *
  691. * @param {BootstrapValidator} validator The validator plugin instance
  692. * @param {jQuery} $field Field element
  693. * @param {Object} options Consists of following keys:
  694. * - min
  695. * - max
  696. * At least one of two keys is required
  697. * - message: The invalid message
  698. * @returns {Boolean}
  699. */
  700. validate: function(validator, $field, options) {
  701. var numChoices = validator
  702. .getFieldElements($field.attr('data-bv-field'))
  703. .filter(':checked')
  704. .length;
  705. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  706. return false;
  707. }
  708. return true;
  709. }
  710. };
  711. }(window.jQuery));
  712. ;(function($) {
  713. $.fn.bootstrapValidator.validators.creditCard = {
  714. /**
  715. * Return true if the input value is valid credit card number
  716. * Based on https://gist.github.com/DiegoSalazar/4075533
  717. *
  718. * @param {BootstrapValidator} validator The validator plugin instance
  719. * @param {jQuery} $field Field element
  720. * @param {Object} options Can consist of the following key:
  721. * - message: The invalid message
  722. * @returns {Boolean}
  723. */
  724. validate: function(validator, $field, options) {
  725. var value = $field.val();
  726. if (value == '') {
  727. return true;
  728. }
  729. // Accept only digits, dashes or spaces
  730. if (/[^0-9-\s]+/.test(value)) {
  731. return false;
  732. }
  733. value = value.replace(/\D/g, '');
  734. // Validate the check sum
  735. // The Luhn Algorithm
  736. // http://en.wikipedia.org/wiki/Luhn
  737. var check = 0, digit = 0, even = false, length = value.length;
  738. for (var n = length - 1; n >= 0; n--) {
  739. digit = parseInt(value.charAt(n), 10);
  740. if (even) {
  741. if ((digit *= 2) > 9) {
  742. digit -= 9;
  743. }
  744. }
  745. check += digit;
  746. even = !even;
  747. }
  748. if ((check % 10) != 0) {
  749. return false;
  750. }
  751. // Validate the card number based on prefix (IIN ranges) and length
  752. var cards = {
  753. AMERICAN_EXPRESS: {
  754. length: [15],
  755. prefix: ['34', '37']
  756. },
  757. DINERS_CLUB: {
  758. length: [14],
  759. prefix: ['300', '301', '302', '303', '304', '305', '36']
  760. },
  761. DINERS_CLUB_US: {
  762. length: [16],
  763. prefix: ['54', '55']
  764. },
  765. DISCOVER: {
  766. length: [16],
  767. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  768. '62214', '62215', '62216', '62217', '62218', '62219',
  769. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  770. '62290', '62291', '622920', '622921', '622922', '622923',
  771. '622924', '622925', '644', '645', '646', '647', '648',
  772. '649', '65']
  773. },
  774. JCB: {
  775. length: [16],
  776. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  777. },
  778. LASER: {
  779. length: [16, 17, 18, 19],
  780. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  781. },
  782. MAESTRO: {
  783. length: [12, 13, 14, 15, 16, 17, 18, 19],
  784. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  785. },
  786. MASTERCARD: {
  787. length: [16],
  788. prefix: ['51', '52', '53', '54', '55']
  789. },
  790. SOLO: {
  791. length: [16, 18, 19],
  792. prefix: ['6334', '6767']
  793. },
  794. UNIONPAY: {
  795. length: [16, 17, 18, 19],
  796. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  797. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  798. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  799. '622920', '622921', '622922', '622923', '622924', '622925']
  800. },
  801. VISA: {
  802. length: [16],
  803. prefix: ['4']
  804. }
  805. };
  806. var type, i;
  807. for (type in cards) {
  808. for (i in cards[type]['prefix']) {
  809. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  810. && cards[type]['length'].indexOf(value.length) != -1) // and length
  811. {
  812. return true;
  813. }
  814. }
  815. }
  816. return false;
  817. }
  818. };
  819. }(window.jQuery));
  820. ;(function($) {
  821. $.fn.bootstrapValidator.validators.cvv = {
  822. html5Attributes: {
  823. message: 'message',
  824. ccfield: 'creditCardField'
  825. },
  826. /**
  827. * Return true if the input value is a valid CVV number.
  828. *
  829. * @param {BootstrapValidator} validator The validator plugin instance
  830. * @param {jQuery} $field Field element
  831. * @param {Object} options Can consist of the following keys:
  832. * - creditCardField: The credit card number field. It can be null
  833. * - message: The invalid message
  834. * @returns {Boolean}
  835. */
  836. validate: function(validator, $field, options) {
  837. var value = $field.val();
  838. if (value == '') {
  839. return true;
  840. }
  841. if (!/^[0-9]{3,4}$/.test(value)) {
  842. return false;
  843. }
  844. if (!options.creditCardField) {
  845. return true;
  846. }
  847. // Get the credit card number
  848. var creditCard = validator.getFieldElements(options.creditCardField).val();
  849. if (creditCard == '') {
  850. return true;
  851. }
  852. // Supported credit card types
  853. var cards = {
  854. AMERICAN_EXPRESS: {
  855. length: [15],
  856. prefix: ['34', '37']
  857. },
  858. DINERS_CLUB: {
  859. length: [14],
  860. prefix: ['300', '301', '302', '303', '304', '305', '36']
  861. },
  862. DINERS_CLUB_US: {
  863. length: [16],
  864. prefix: ['54', '55']
  865. },
  866. DISCOVER: {
  867. length: [16],
  868. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  869. '62214', '62215', '62216', '62217', '62218', '62219',
  870. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  871. '62290', '62291', '622920', '622921', '622922', '622923',
  872. '622924', '622925', '644', '645', '646', '647', '648',
  873. '649', '65']
  874. },
  875. JCB: {
  876. length: [16],
  877. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  878. },
  879. LASER: {
  880. length: [16, 17, 18, 19],
  881. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  882. },
  883. MAESTRO: {
  884. length: [12, 13, 14, 15, 16, 17, 18, 19],
  885. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  886. },
  887. MASTERCARD: {
  888. length: [16],
  889. prefix: ['51', '52', '53', '54', '55']
  890. },
  891. SOLO: {
  892. length: [16, 18, 19],
  893. prefix: ['6334', '6767']
  894. },
  895. UNIONPAY: {
  896. length: [16, 17, 18, 19],
  897. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  898. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  899. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  900. '622920', '622921', '622922', '622923', '622924', '622925']
  901. },
  902. VISA: {
  903. length: [16],
  904. prefix: ['4']
  905. }
  906. };
  907. var type, i, creditCardType = null;
  908. for (type in cards) {
  909. for (i in cards[type]['prefix']) {
  910. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  911. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  912. {
  913. creditCardType = type;
  914. break;
  915. }
  916. }
  917. }
  918. return (creditCardType == null)
  919. ? false
  920. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  921. }
  922. };
  923. }(window.jQuery));
  924. ;(function($) {
  925. $.fn.bootstrapValidator.validators.date = {
  926. html5Attributes: {
  927. message: 'message',
  928. format: 'format'
  929. },
  930. /**
  931. * Return true if the input value is valid date
  932. *
  933. * @param {BootstrapValidator} validator The validator plugin instance
  934. * @param {jQuery} $field Field element
  935. * @param {Object} options Can consist of the following keys:
  936. * - format: The date format. Default is MM/DD/YYYY
  937. * Support the following formats:
  938. * YYYY/DD/MM
  939. * YYYY/DD/MM h:m A
  940. * YYYY/MM/DD
  941. * YYYY/MM/DD h:m A
  942. *
  943. * YYYY-DD-MM
  944. * YYYY-DD-MM h:m A
  945. * YYYY-MM-DD
  946. * YYYY-MM-DD h:m A
  947. *
  948. * MM/DD/YYYY
  949. * MM/DD/YYYY h:m A
  950. * DD/MM/YYYY
  951. * DD/MM/YYYY h:m A
  952. *
  953. * MM-DD-YYYY
  954. * MM-DD-YYYY h:m A
  955. * DD-MM-YYYY
  956. * DD-MM-YYYY h:m A
  957. * - message: The invalid message
  958. * @returns {Boolean}
  959. */
  960. validate: function(validator, $field, options) {
  961. var value = $field.val();
  962. if (value == '') {
  963. return true;
  964. }
  965. // Determine the separator
  966. options.format = options.format || 'MM/DD/YYYY';
  967. var separator = (options.format.indexOf('/') != -1)
  968. ? '/'
  969. : ((options.format.indexOf('-') != -1) ? '-' : null);
  970. if (separator == null) {
  971. return false;
  972. }
  973. var month, day, year, minutes = null, hours = null, matches;
  974. switch (true) {
  975. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  976. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  977. year = matches[1]; day = matches[2]; month = matches[3];
  978. break;
  979. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  980. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  981. day = matches[1]; month = matches[2]; year = matches[3];
  982. break;
  983. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  984. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  985. year = matches[1]; month = matches[2]; day = matches[3];
  986. break;
  987. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  988. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  989. month = matches[1]; day = matches[2]; year = matches[3];
  990. break;
  991. 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'):
  992. 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'):
  993. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  994. break;
  995. 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'):
  996. 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'):
  997. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  998. break;
  999. 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'):
  1000. 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'):
  1001. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1002. break;
  1003. 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'):
  1004. 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'):
  1005. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1006. break;
  1007. default:
  1008. return false;
  1009. }
  1010. // Validate hours and minutes
  1011. if (hours && minutes) {
  1012. hours = parseInt(hours, 10);
  1013. minutes = parseInt(minutes, 10);
  1014. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1015. return false;
  1016. }
  1017. }
  1018. // Validate day, month, and year
  1019. day = parseInt(day, 10);
  1020. month = parseInt(month, 10);
  1021. year = parseInt(year, 10);
  1022. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1023. return false;
  1024. }
  1025. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1026. // Update the number of days in Feb of leap year
  1027. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1028. numDays[1] = 29;
  1029. }
  1030. // Check the day
  1031. return (day > 0 && day <= numDays[month - 1]);
  1032. }
  1033. };
  1034. }(window.jQuery));
  1035. ;(function($) {
  1036. $.fn.bootstrapValidator.validators.different = {
  1037. html5Attributes: {
  1038. message: 'message',
  1039. field: 'field'
  1040. },
  1041. /**
  1042. * Return true if the input value is different with given field's value
  1043. *
  1044. * @param {BootstrapValidator} validator The validator plugin instance
  1045. * @param {jQuery} $field Field element
  1046. * @param {Object} options Consists of the following key:
  1047. * - field: The name of field that will be used to compare with current one
  1048. * - message: The invalid message
  1049. * @returns {Boolean}
  1050. */
  1051. validate: function(validator, $field, options) {
  1052. var value = $field.val();
  1053. if (value == '') {
  1054. return true;
  1055. }
  1056. var compareWith = validator.getFieldElements(options.field);
  1057. if (compareWith == null) {
  1058. return true;
  1059. }
  1060. if (value != compareWith.val()) {
  1061. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1062. return true;
  1063. } else {
  1064. return false;
  1065. }
  1066. }
  1067. };
  1068. }(window.jQuery));
  1069. ;(function($) {
  1070. $.fn.bootstrapValidator.validators.digits = {
  1071. /**
  1072. * Return true if the input value contains digits only
  1073. *
  1074. * @param {BootstrapValidator} validator Validate plugin instance
  1075. * @param {jQuery} $field Field element
  1076. * @param {Object} options
  1077. * @returns {Boolean}
  1078. */
  1079. validate: function(validator, $field, options) {
  1080. var value = $field.val();
  1081. if (value == '') {
  1082. return true;
  1083. }
  1084. return /^\d+$/.test(value);
  1085. }
  1086. }
  1087. }(window.jQuery));
  1088. ;(function($) {
  1089. $.fn.bootstrapValidator.validators.emailAddress = {
  1090. enableByHtml5: function($field) {
  1091. return ('email' == $field.attr('type'));
  1092. },
  1093. /**
  1094. * Return true if and only if the input value is a valid email address
  1095. *
  1096. * @param {BootstrapValidator} validator Validate plugin instance
  1097. * @param {jQuery} $field Field element
  1098. * @param {Object} options
  1099. * @returns {Boolean}
  1100. */
  1101. validate: function(validator, $field, options) {
  1102. var value = $field.val();
  1103. if (value == '') {
  1104. return true;
  1105. }
  1106. // Email address regular expression
  1107. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1108. 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,}))$/;
  1109. return emailRegExp.test(value);
  1110. }
  1111. }
  1112. }(window.jQuery));
  1113. ;(function($) {
  1114. $.fn.bootstrapValidator.validators.greaterThan = {
  1115. html5Attributes: {
  1116. message: 'message',
  1117. value: 'value',
  1118. inclusive: 'inclusive'
  1119. },
  1120. enableByHtml5: function($field) {
  1121. var min = $field.attr('min');
  1122. if (min) {
  1123. return {
  1124. value: min
  1125. };
  1126. }
  1127. return false;
  1128. },
  1129. /**
  1130. * Return true if the input value is greater than or equals to given number
  1131. *
  1132. * @param {BootstrapValidator} validator Validate plugin instance
  1133. * @param {jQuery} $field Field element
  1134. * @param {Object} options Can consist of the following keys:
  1135. * - value: The number used to compare to
  1136. * - inclusive [optional]: Can be true or false. Default is true
  1137. * - message: The invalid message
  1138. * @returns {Boolean}
  1139. */
  1140. validate: function(validator, $field, options) {
  1141. var value = $field.val();
  1142. if (value == '') {
  1143. return true;
  1144. }
  1145. value = parseFloat(value);
  1146. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1147. }
  1148. }
  1149. }(window.jQuery));
  1150. ;(function($) {
  1151. $.fn.bootstrapValidator.validators.hexColor = {
  1152. enableByHtml5: function($field) {
  1153. return ('color' == $field.attr('type'));
  1154. },
  1155. /**
  1156. * Return true if the input value is a valid hex color
  1157. *
  1158. * @param {BootstrapValidator} validator The validator plugin instance
  1159. * @param {jQuery} $field Field element
  1160. * @param {Object} options Can consist of the following keys:
  1161. * - message: The invalid message
  1162. * @returns {Boolean}
  1163. */
  1164. validate: function(validator, $field, options) {
  1165. var value = $field.val();
  1166. if (value == '') {
  1167. return true;
  1168. }
  1169. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1170. }
  1171. };
  1172. }(window.jQuery));
  1173. ;(function($) {
  1174. $.fn.bootstrapValidator.validators.identical = {
  1175. html5Attributes: {
  1176. message: 'message',
  1177. field: 'field'
  1178. },
  1179. /**
  1180. * Check if input value equals to value of particular one
  1181. *
  1182. * @param {BootstrapValidator} validator The validator plugin instance
  1183. * @param {jQuery} $field Field element
  1184. * @param {Object} options Consists of the following key:
  1185. * - field: The name of field that will be used to compare with current one
  1186. * @returns {Boolean}
  1187. */
  1188. validate: function(validator, $field, options) {
  1189. var value = $field.val();
  1190. if (value == '') {
  1191. return true;
  1192. }
  1193. var compareWith = validator.getFieldElements(options.field);
  1194. if (compareWith == null) {
  1195. return true;
  1196. }
  1197. if (value == compareWith.val()) {
  1198. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1199. return true;
  1200. } else {
  1201. return false;
  1202. }
  1203. }
  1204. };
  1205. }(window.jQuery));
  1206. ;(function($) {
  1207. $.fn.bootstrapValidator.validators.integer = {
  1208. enableByHtml5: function($field) {
  1209. return ('number' == $field.attr('type'));
  1210. },
  1211. /**
  1212. * Return true if the input value is an integer
  1213. *
  1214. * @param {BootstrapValidator} validator The validator plugin instance
  1215. * @param {jQuery} $field Field element
  1216. * @param {Object} options Can consist of the following key:
  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|[1-9][0-9]*))$/.test(value);
  1226. }
  1227. };
  1228. }(window.jQuery));
  1229. ;(function($) {
  1230. $.fn.bootstrapValidator.validators.ip = {
  1231. html5Attributes: {
  1232. message: 'message',
  1233. ipv4: 'ipv4',
  1234. ipv6: 'ipv6'
  1235. },
  1236. /**
  1237. * Return true if the input value is a IP address.
  1238. *
  1239. * @param {BootstrapValidator} validator The validator plugin instance
  1240. * @param {jQuery} $field Field element
  1241. * @param {Object} options Can consist of the following keys:
  1242. * - ipv4: Enable IPv4 validator, default to true
  1243. * - ipv6: Enable IPv6 validator, default to true
  1244. * - message: The invalid message
  1245. * @returns {Boolean}
  1246. */
  1247. validate: function(validator, $field, options) {
  1248. var value = $field.val();
  1249. if (value == '') {
  1250. return true;
  1251. }
  1252. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1253. if (options.ipv4) {
  1254. 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);
  1255. } else if (options.ipv6) {
  1256. 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);
  1257. }
  1258. return false;
  1259. }
  1260. };
  1261. }(window.jQuery));
  1262. ;(function($) {
  1263. $.fn.bootstrapValidator.validators.isbn = {
  1264. /**
  1265. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1266. *
  1267. * @param {BootstrapValidator} validator The validator plugin instance
  1268. * @param {jQuery} $field Field element
  1269. * @param {Object} options Can consist of the following keys:
  1270. * - message: The invalid message
  1271. * @returns {Boolean}
  1272. */
  1273. validate: function(validator, $field, options) {
  1274. var value = $field.val();
  1275. if (value == '') {
  1276. return true;
  1277. }
  1278. // Replace all special characters except digits and X
  1279. value = value.replace(/[^\dX]/gi, '');
  1280. var chars = value.split(''),
  1281. sum = 0,
  1282. checksum;
  1283. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1284. switch (chars.length) {
  1285. // ISBN 10
  1286. case 10:
  1287. sum = 0;
  1288. for (var i = 0; i < 9; i++) {
  1289. sum += ((10 - i) * parseInt(chars[i]));
  1290. }
  1291. checksum = 11 - (sum % 11);
  1292. if (checksum == 11) {
  1293. checksum = 0;
  1294. } else if (checksum == 10) {
  1295. checksum = 'X';
  1296. }
  1297. return (checksum == chars[9]);
  1298. // ISBN 13
  1299. case 13:
  1300. sum = 0;
  1301. for (var i = 0; i < 12; i++) {
  1302. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1303. }
  1304. checksum = 10 - (sum % 10);
  1305. if (checksum == 10) {
  1306. checksum = '0';
  1307. }
  1308. return (checksum == chars[12]);
  1309. default:
  1310. return false;
  1311. }
  1312. }
  1313. };
  1314. }(window.jQuery));
  1315. ;(function($) {
  1316. $.fn.bootstrapValidator.validators.lessThan = {
  1317. html5Attributes: {
  1318. message: 'message',
  1319. value: 'value',
  1320. inclusive: 'inclusive'
  1321. },
  1322. enableByHtml5: function($field) {
  1323. var max = $field.attr('max');
  1324. if (max) {
  1325. return {
  1326. value: max
  1327. };
  1328. }
  1329. return false;
  1330. },
  1331. /**
  1332. * Return true if the input value is less than or equal to given number
  1333. *
  1334. * @param {BootstrapValidator} validator The validator plugin instance
  1335. * @param {jQuery} $field Field element
  1336. * @param {Object} options Can consist of the following keys:
  1337. * - value: The number used to compare to
  1338. * - inclusive [optional]: Can be true or false. Default is true
  1339. * - message: The invalid message
  1340. * @returns {Boolean}
  1341. */
  1342. validate: function(validator, $field, options) {
  1343. var value = $field.val();
  1344. if (value == '') {
  1345. return true;
  1346. }
  1347. value = parseFloat(value);
  1348. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1349. }
  1350. };
  1351. }(window.jQuery));
  1352. ;(function($) {
  1353. $.fn.bootstrapValidator.validators.mac = {
  1354. /**
  1355. * Return true if the input value is a MAC address.
  1356. *
  1357. * @param {BootstrapValidator} validator The validator plugin instance
  1358. * @param {jQuery} $field Field element
  1359. * @param {Object} options Can consist of the following keys:
  1360. * - message: The invalid message
  1361. * @returns {Boolean}
  1362. */
  1363. validate: function(validator, $field, options) {
  1364. var value = $field.val();
  1365. if (value == '') {
  1366. return true;
  1367. }
  1368. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1369. }
  1370. };
  1371. }(window.jQuery));
  1372. ;(function($) {
  1373. $.fn.bootstrapValidator.validators.notEmpty = {
  1374. enableByHtml5: function($field) {
  1375. var required = $field.attr('required') + '';
  1376. return ('required' == required || 'true' == required);
  1377. },
  1378. /**
  1379. * Check if input value is empty or not
  1380. *
  1381. * @param {BootstrapValidator} validator The validator plugin instance
  1382. * @param {jQuery} $field Field element
  1383. * @param {Object} options
  1384. * @returns {Boolean}
  1385. */
  1386. validate: function(validator, $field, options) {
  1387. var type = $field.attr('type');
  1388. if ('radio' == type || 'checkbox' == type) {
  1389. return validator
  1390. .getFieldElements($field.attr('data-bv-field'))
  1391. .filter(':checked')
  1392. .length > 0;
  1393. }
  1394. return $.trim($field.val()) != '';
  1395. }
  1396. };
  1397. }(window.jQuery));
  1398. ;(function($) {
  1399. $.fn.bootstrapValidator.validators.phone = {
  1400. html5Attributes: {
  1401. message: 'message',
  1402. country: 'country'
  1403. },
  1404. /**
  1405. * Return true if the input value contains a valid US phone number only
  1406. *
  1407. * @param {BootstrapValidator} validator Validate plugin instance
  1408. * @param {jQuery} $field Field element
  1409. * @param {Object} options Consist of key:
  1410. * - message: The invalid message
  1411. * - country: The ISO 3166 country code
  1412. * Currently it only supports United State (US) country
  1413. * @returns {Boolean}
  1414. */
  1415. validate: function(validator, $field, options) {
  1416. var value = $field.val();
  1417. if (value == '') {
  1418. return true;
  1419. }
  1420. options.country = options.country || 'US';
  1421. switch (options.country.toUpperCase()) {
  1422. case 'US':
  1423. default:
  1424. value = value.replace(/\(|\)|\s+/g, '');
  1425. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1426. }
  1427. }
  1428. }
  1429. }(window.jQuery));
  1430. ;(function($) {
  1431. $.fn.bootstrapValidator.validators.regexp = {
  1432. html5Attributes: {
  1433. message: 'message',
  1434. regexp: 'regexp'
  1435. },
  1436. enableByHtml5: function($field) {
  1437. var pattern = $field.attr('pattern');
  1438. if (pattern) {
  1439. return {
  1440. regexp: pattern
  1441. };
  1442. }
  1443. return false;
  1444. },
  1445. /**
  1446. * Check if the element value matches given regular expression
  1447. *
  1448. * @param {BootstrapValidator} validator The validator plugin instance
  1449. * @param {jQuery} $field Field element
  1450. * @param {Object} options Consists of the following key:
  1451. * - regexp: The regular expression you need to check
  1452. * @returns {Boolean}
  1453. */
  1454. validate: function(validator, $field, options) {
  1455. var value = $field.val();
  1456. if (value == '') {
  1457. return true;
  1458. }
  1459. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1460. return regexp.test(value);
  1461. }
  1462. };
  1463. }(window.jQuery));
  1464. ;(function($) {
  1465. $.fn.bootstrapValidator.validators.remote = {
  1466. html5Attributes: {
  1467. message: 'message',
  1468. url: 'url'
  1469. },
  1470. /**
  1471. * Request a remote server to check the input value
  1472. *
  1473. * @param {BootstrapValidator} validator Plugin instance
  1474. * @param {jQuery} $field Field element
  1475. * @param {Object} options Can consist of the following keys:
  1476. * - url
  1477. * - data [optional]: By default, it will take the value
  1478. * {
  1479. * <fieldName>: <fieldValue>
  1480. * }
  1481. * - message: The invalid message
  1482. * @returns {Boolean|Deferred}
  1483. */
  1484. validate: function(validator, $field, options) {
  1485. var value = $field.val();
  1486. if (value == '') {
  1487. return true;
  1488. }
  1489. var name = $field.attr('data-bv-field'), data = options.data;
  1490. if (data == null) {
  1491. data = {};
  1492. }
  1493. // Support dynamic data
  1494. if ('function' == typeof data) {
  1495. data = data.call(this, validator);
  1496. }
  1497. data[name] = value;
  1498. var dfd = new $.Deferred();
  1499. var xhr = $.ajax({
  1500. type: 'POST',
  1501. url: options.url,
  1502. dataType: 'json',
  1503. data: data
  1504. });
  1505. xhr.then(function(response) {
  1506. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1507. });
  1508. dfd.fail(function() {
  1509. xhr.abort();
  1510. });
  1511. return dfd;
  1512. }
  1513. };
  1514. }(window.jQuery));
  1515. ;(function($) {
  1516. $.fn.bootstrapValidator.validators.step = {
  1517. html5Attributes: {
  1518. message: 'message',
  1519. base: 'baseValue',
  1520. step: 'step'
  1521. },
  1522. /**
  1523. * Return true if the input value is valid step one
  1524. *
  1525. * @param {BootstrapValidator} validator The validator plugin instance
  1526. * @param {jQuery} $field Field element
  1527. * @param {Object} options Can consist of the following keys:
  1528. * - baseValue: The base value
  1529. * - step: The step
  1530. * - message: The invalid message
  1531. * @returns {Boolean}
  1532. */
  1533. validate: function(validator, $field, options) {
  1534. var value = $field.val();
  1535. if (value == '') {
  1536. return true;
  1537. }
  1538. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1539. value = parseFloat(value);
  1540. if (isNaN(value) || !isFinite(value)) {
  1541. return false;
  1542. }
  1543. var round = function(x, precision) {
  1544. var m = Math.pow(10, precision);
  1545. x = x * m;
  1546. var sign = (x > 0) | -(x < 0),
  1547. isHalf = (x % 1 === 0.5 * sign);
  1548. if (isHalf) {
  1549. return (Math.floor(x) + (sign > 0)) / m;
  1550. } else {
  1551. return Math.round(x) / m;
  1552. }
  1553. },
  1554. floatMod = function(x, y) {
  1555. if (y == 0.0) {
  1556. return 1.0;
  1557. }
  1558. var dotX = (x + '').split('.'),
  1559. dotY = (y + '').split('.'),
  1560. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1561. return round(x - y * Math.floor(x / y), precision);
  1562. };
  1563. var mod = floatMod(value - options.baseValue, options.step);
  1564. return (mod == 0.0 || mod == options.step);
  1565. }
  1566. };
  1567. }(window.jQuery));
  1568. ;(function($) {
  1569. $.fn.bootstrapValidator.validators.stringLength = {
  1570. html5Attributes: {
  1571. message: 'message',
  1572. min: 'min',
  1573. max: 'max'
  1574. },
  1575. enableByHtml5: function($field) {
  1576. var maxLength = $field.attr('maxlength');
  1577. if (maxLength) {
  1578. return {
  1579. max: parseInt(maxLength, 10)
  1580. };
  1581. }
  1582. return false;
  1583. },
  1584. /**
  1585. * Check if the length of element value is less or more than given number
  1586. *
  1587. * @param {BootstrapValidator} validator The validator plugin instance
  1588. * @param {jQuery} $field Field element
  1589. * @param {Object} options Consists of following keys:
  1590. * - min
  1591. * - max
  1592. * At least one of two keys is required
  1593. * - message: The invalid message
  1594. * @returns {Boolean}
  1595. */
  1596. validate: function(validator, $field, options) {
  1597. var value = $field.val();
  1598. if (value == '') {
  1599. return true;
  1600. }
  1601. var length = $.trim(value).length;
  1602. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1603. return false;
  1604. }
  1605. return true;
  1606. }
  1607. };
  1608. }(window.jQuery));
  1609. ;(function($) {
  1610. $.fn.bootstrapValidator.validators.uri = {
  1611. enableByHtml5: function($field) {
  1612. return ('url' == $field.attr('type'));
  1613. },
  1614. /**
  1615. * Return true if the input value is a valid URL
  1616. *
  1617. * @param {BootstrapValidator} validator The validator plugin instance
  1618. * @param {jQuery} $field Field element
  1619. * @param {Object} options
  1620. * @returns {Boolean}
  1621. */
  1622. validate: function(validator, $field, options) {
  1623. var value = $field.val();
  1624. if (value == '') {
  1625. return true;
  1626. }
  1627. // Credit to https://gist.github.com/dperini/729294
  1628. //
  1629. // Regular Expression for URL validation
  1630. //
  1631. // Author: Diego Perini
  1632. // Updated: 2010/12/05
  1633. //
  1634. // the regular expression composed & commented
  1635. // could be easily tweaked for RFC compliance,
  1636. // it was expressly modified to fit & satisfy
  1637. // these test for an URL shortener:
  1638. //
  1639. // http://mathiasbynens.be/demo/url-regex
  1640. //
  1641. // Notes on possible differences from a standard/generic validation:
  1642. //
  1643. // - utf-8 char class take in consideration the full Unicode range
  1644. // - TLDs have been made mandatory so single names like "localhost" fails
  1645. // - protocols have been restricted to ftp, http and https only as requested
  1646. //
  1647. // Changes:
  1648. //
  1649. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1650. // first and last IP address of each class is considered invalid
  1651. // (since they are broadcast/network addresses)
  1652. //
  1653. // - Added exclusion of private, reserved and/or local networks ranges
  1654. //
  1655. // Compressed one-line versions:
  1656. //
  1657. // Javascript version
  1658. //
  1659. // /^(?:(?: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
  1660. //
  1661. // PHP version
  1662. //
  1663. // _^(?:(?: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
  1664. var urlExp = new RegExp(
  1665. "^" +
  1666. // protocol identifier
  1667. "(?:(?:https?|ftp)://)" +
  1668. // user:pass authentication
  1669. "(?:\\S+(?::\\S*)?@)?" +
  1670. "(?:" +
  1671. // IP address exclusion
  1672. // private & local networks
  1673. "(?!10(?:\\.\\d{1,3}){3})" +
  1674. "(?!127(?:\\.\\d{1,3}){3})" +
  1675. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1676. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1677. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1678. // IP address dotted notation octets
  1679. // excludes loopback network 0.0.0.0
  1680. // excludes reserved space >= 224.0.0.0
  1681. // excludes network & broacast addresses
  1682. // (first & last IP address of each class)
  1683. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1684. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1685. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1686. "|" +
  1687. // host name
  1688. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1689. // domain name
  1690. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1691. // TLD identifier
  1692. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1693. ")" +
  1694. // port number
  1695. "(?::\\d{2,5})?" +
  1696. // resource path
  1697. "(?:/[^\\s]*)?" +
  1698. "$", "i"
  1699. );
  1700. return urlExp.test(value);
  1701. }
  1702. };
  1703. }(window.jQuery));
  1704. ;(function($) {
  1705. $.fn.bootstrapValidator.validators.zipCode = {
  1706. html5Attributes: {
  1707. message: 'message',
  1708. country: 'country'
  1709. },
  1710. /**
  1711. * Return true if and only if the input value is a valid country zip code
  1712. *
  1713. * @param {BootstrapValidator} validator The validator plugin instance
  1714. * @param {jQuery} $field Field element
  1715. * @param {Object} options Consist of key:
  1716. * - message: The invalid message
  1717. * - country: The ISO 3166 country code
  1718. *
  1719. * Currently it supports the following countries:
  1720. * - US (United State)
  1721. * - DK (Denmark)
  1722. * - SE (Sweden)
  1723. * @returns {Boolean}
  1724. */
  1725. validate: function(validator, $field, options) {
  1726. var value = $field.val();
  1727. if (value == '' || !options.country) {
  1728. return true;
  1729. }
  1730. options.country = options.country || 'US';
  1731. switch (options.country.toUpperCase()) {
  1732. case 'DK':
  1733. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  1734. case 'SE':
  1735. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  1736. case 'US':
  1737. default:
  1738. return /^\d{5}([\-]\d{4})?$/.test(value);
  1739. }
  1740. }
  1741. };
  1742. }(window.jQuery));