bootstrapValidator.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /**
  2. * BootstrapValidator (https://github.com/nghuuphuoc/bootstrapvalidator)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.3.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.dfds = {}; // Array of deferred
  16. this.results = {}; // Validating results
  17. this.invalidField = null; // First invalid field
  18. this.$submitButton = null; // The submit button which is clicked to submit form
  19. this._init();
  20. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  21. this.STATUS_VALIDATING = 'VALIDATING';
  22. this.STATUS_INVALID = 'INVALID';
  23. this.STATUS_VALID = 'VALID';
  24. };
  25. // The default options
  26. BootstrapValidator.DEFAULT_OPTIONS = {
  27. // The form CSS class
  28. elementClass: 'bootstrap-validator-form',
  29. // Default invalid message
  30. message: 'This value is not valid',
  31. // Shows ok/error/loading icons based on the field validity.
  32. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  33. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  34. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  35. //
  36. // Examples:
  37. // - Use Glyphicons icons:
  38. // feedbackIcons: {
  39. // valid: 'glyphicon glyphicon-ok',
  40. // invalid: 'glyphicon glyphicon-remove',
  41. // validating: 'glyphicon glyphicon-refresh'
  42. // }
  43. // - Use FontAwesome icons:
  44. // feedbackIcons: {
  45. // valid: 'fa fa-check',
  46. // invalid: 'fa fa-times',
  47. // validating: 'fa fa-refresh'
  48. // }
  49. feedbackIcons: {
  50. valid: null,
  51. invalid: null,
  52. validating: null
  53. },
  54. // The submit buttons selector
  55. // These buttons will be disabled to prevent the valid form from multiple submissions
  56. submitButtons: 'button[type="submit"]',
  57. // The custom submit handler
  58. // It will prevent the form from the default submission
  59. //
  60. // submitHandler: function(validator, form) {
  61. // - validator is the BootstrapValidator instance
  62. // - form is the jQuery object present the current form
  63. // }
  64. submitHandler: null,
  65. // Live validating option
  66. // Can be one of 3 values:
  67. // - enabled: The plugin validates fields as soon as they are changed
  68. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  69. // - submitted: The live validating is enabled after the form is submitted
  70. live: 'enabled',
  71. // Map the field name with validator rules
  72. fields: null
  73. };
  74. BootstrapValidator.prototype = {
  75. constructor: BootstrapValidator,
  76. /**
  77. * Init form
  78. */
  79. _init: function() {
  80. if (this.options.fields == null) {
  81. return;
  82. }
  83. var that = this;
  84. this.$form
  85. // Disable client side validation in HTML 5
  86. .attr('novalidate', 'novalidate')
  87. .addClass(this.options.elementClass)
  88. // Disable the default submission first
  89. .on('submit.bootstrapValidator', function(e) {
  90. e.preventDefault();
  91. that.validate();
  92. })
  93. .find(this.options.submitButtons)
  94. .on('click', function() {
  95. that.$submitButton = $(this);
  96. });
  97. for (var field in this.options.fields) {
  98. this._initField(field);
  99. }
  100. this._setLiveValidating();
  101. },
  102. /**
  103. * Init field
  104. *
  105. * @param {String} field The field name
  106. */
  107. _initField: function(field) {
  108. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  109. return;
  110. }
  111. this.dfds[field] = {};
  112. this.results[field] = {};
  113. var fields = this.getFieldElements(field);
  114. // We don't need to validate non-existing fields
  115. if (fields == null) {
  116. delete this.options.fields[field];
  117. delete this.dfds[field];
  118. return;
  119. }
  120. fields.attr('data-bv-field', field);
  121. // Create help block elements for showing the error messages
  122. var $field = $(fields[0]),
  123. $parent = $field.parents('.form-group'),
  124. // Allow user to indicate where the error messages are shown
  125. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  126. $field.data('bootstrapValidator.messageContainer', $message);
  127. for (var validatorName in this.options.fields[field].validators) {
  128. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  129. delete this.options.fields[field].validators[validatorName];
  130. continue;
  131. }
  132. this.results[field][validatorName] = this.STATUS_NOT_VALIDATED;
  133. $('<small/>')
  134. .css('display', 'none')
  135. .attr('data-bv-validator', validatorName)
  136. .html(this.options.fields[field].validators[validatorName].message || this.options.message)
  137. .addClass('help-block')
  138. .appendTo($message);
  139. }
  140. // Prepare the feedback icons
  141. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  142. if (this.options.feedbackIcons
  143. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid)
  144. {
  145. $parent.addClass('has-feedback');
  146. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($(fields[fields.length - 1]));
  147. // The feedback icon does not render correctly if there is no label
  148. // https://github.com/twbs/bootstrap/issues/12873
  149. if ($parent.find('label').length == 0) {
  150. $icon.css('top', 0);
  151. }
  152. }
  153. if (this.options.fields[field]['enabled'] == null) {
  154. this.options.fields[field]['enabled'] = true;
  155. }
  156. // Whenever the user change the field value, mark it as not validated yet
  157. var that = this,
  158. type = fields.attr('type'),
  159. event = ('radio' == type || 'checkbox' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
  160. fields.on(event + '.bootstrapValidator', function() {
  161. that.updateStatus($field, that.STATUS_NOT_VALIDATED, null);
  162. });
  163. },
  164. /**
  165. * Get the element to place the error messages
  166. *
  167. * @param {jQuery} $field The field element
  168. * @returns {jQuery}
  169. */
  170. _getMessageContainer: function($field) {
  171. var $parent = $field.parent();
  172. if ($parent.hasClass('form-group')) {
  173. return $parent;
  174. }
  175. var cssClasses = $parent.attr('class');
  176. if (!cssClasses) {
  177. return this._getMessageContainer($parent);
  178. }
  179. cssClasses = cssClasses.split(' ');
  180. var n = cssClasses.length;
  181. for (var i = 0; i < n; i++) {
  182. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  183. return $parent;
  184. }
  185. }
  186. return this._getMessageContainer($parent);
  187. },
  188. /**
  189. * Enable live validating
  190. */
  191. _setLiveValidating: function() {
  192. if ('enabled' == this.options.live) {
  193. var that = this;
  194. for (var field in this.options.fields) {
  195. (function(f) {
  196. var fields = that.getFieldElements(f);
  197. if (fields) {
  198. var type = fields.attr('type'),
  199. event = ('radio' == type || 'checkbox' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
  200. fields.on(event + '.bootstrapValidator', function() {
  201. that.validateField(f);
  202. });
  203. }
  204. })(field);
  205. }
  206. }
  207. },
  208. /**
  209. * Disable/Enable submit buttons
  210. *
  211. * @param {Boolean} disabled
  212. */
  213. _disableSubmitButtons: function(disabled) {
  214. if (!disabled) {
  215. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  216. } else if (this.options.live != 'disabled') {
  217. // Don't disable if the live validating mode is disabled
  218. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  219. }
  220. },
  221. /**
  222. * Called when all validations are completed
  223. */
  224. _submit: function() {
  225. if (!this.isValid()) {
  226. if ('submitted' == this.options.live) {
  227. this.options.live = 'enabled';
  228. this._setLiveValidating();
  229. }
  230. // Focus to the first invalid field
  231. if (this.invalidField) {
  232. this.getFieldElements(this.invalidField).focus();
  233. }
  234. return;
  235. }
  236. this._disableSubmitButtons(true);
  237. // Call the custom submission if enabled
  238. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  239. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  240. } else {
  241. // Submit form
  242. this.$form.off('submit.bootstrapValidator').submit();
  243. }
  244. },
  245. // --- Public methods ---
  246. /**
  247. * Retrieve the field elements by given name
  248. *
  249. * @param {String} field The field name
  250. * @returns {null|jQuery[]}
  251. */
  252. getFieldElements: function(field) {
  253. var fields = this.$form.find(this.options.fields[field].selector || '[name="' + field + '"]');
  254. return (fields.length == 0) ? null : fields;
  255. },
  256. /**
  257. * Validate the form
  258. *
  259. * @return {BootstrapValidator}
  260. */
  261. validate: function() {
  262. if (!this.options.fields) {
  263. return this;
  264. }
  265. this._disableSubmitButtons(true);
  266. for (var field in this.options.fields) {
  267. this.validateField(field);
  268. }
  269. this._submit();
  270. return this;
  271. },
  272. /**
  273. * Validate given field
  274. *
  275. * @param {String} field The field name
  276. */
  277. validateField: function(field) {
  278. if (!this.options.fields[field]['enabled']) {
  279. return;
  280. }
  281. var that = this,
  282. fields = this.getFieldElements(field),
  283. $field = $(fields[0]),
  284. validators = this.options.fields[field].validators,
  285. validatorName,
  286. validateResult;
  287. // We don't need to validate disabled field
  288. if (fields.length == 1 && fields.is(':disabled')) {
  289. delete this.options.fields[field];
  290. delete this.dfds[field];
  291. return;
  292. }
  293. for (validatorName in validators) {
  294. if (this.dfds[field][validatorName]) {
  295. this.dfds[field][validatorName].reject();
  296. }
  297. // Don't validate field if it is already done
  298. if (this.results[field][validatorName] == this.STATUS_VALID || this.results[field][validatorName] == this.STATUS_INVALID) {
  299. continue;
  300. }
  301. this.results[field][validatorName] = this.STATUS_VALIDATING;
  302. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  303. if ('object' == typeof validateResult) {
  304. this.updateStatus($field, this.STATUS_VALIDATING, validatorName);
  305. this.dfds[field][validatorName] = validateResult;
  306. validateResult.done(function(isValid, v) {
  307. // v is validator name
  308. delete that.dfds[field][v];
  309. that.updateStatus($field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  310. if (isValid && 'disabled' == that.options.live) {
  311. that._submit();
  312. }
  313. });
  314. } else if ('boolean' == typeof validateResult) {
  315. this.updateStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  316. }
  317. }
  318. },
  319. /**
  320. * Check the form validity
  321. *
  322. * @returns {Boolean}
  323. */
  324. isValid: function() {
  325. var field, validatorName;
  326. for (field in this.results) {
  327. if (!this.options.fields[field]['enabled']) {
  328. continue;
  329. }
  330. for (validatorName in this.results[field]) {
  331. if (this.results[field][validatorName] == this.STATUS_NOT_VALIDATED || this.results[field][validatorName] == this.STATUS_VALIDATING) {
  332. return false;
  333. }
  334. if (this.results[field][validatorName] == this.STATUS_INVALID) {
  335. this.invalidField = field;
  336. return false;
  337. }
  338. }
  339. }
  340. return true;
  341. },
  342. /**
  343. * Update field status
  344. *
  345. * @param {String|jQuery} field The field name or field element
  346. * @param {String} status The status
  347. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  348. * @param {String|null} validatorName The validator name. If null, the method updates validity result for all validators
  349. * @return {BootstrapValidator}
  350. */
  351. updateStatus: function(field, status, validatorName) {
  352. var $field = ('string' == typeof field) ? this.getFieldElements(field) : field,
  353. that = this,
  354. field = $field.attr('data-bv-field'),
  355. $parent = $field.parents('.form-group'),
  356. $message = $field.data('bootstrapValidator.messageContainer'),
  357. $errors = $message.find('.help-block[data-bv-validator]'),
  358. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  359. // Update status
  360. if (validatorName) {
  361. this.results[field][validatorName] = status;
  362. } else {
  363. for (var v in this.options.fields[field].validators) {
  364. this.results[field][v] = status;
  365. }
  366. }
  367. // Show/hide error elements and feedback icons
  368. switch (status) {
  369. case this.STATUS_VALIDATING:
  370. this._disableSubmitButtons(true);
  371. $parent.removeClass('has-success').removeClass('has-error');
  372. // TODO: Show validating message
  373. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  374. if ($icon) {
  375. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  376. }
  377. break;
  378. case this.STATUS_INVALID:
  379. this._disableSubmitButtons(true);
  380. $parent.removeClass('has-success').addClass('has-error');
  381. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  382. if ($icon) {
  383. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  384. }
  385. break;
  386. case this.STATUS_VALID:
  387. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  388. // If the field is valid
  389. if ($errors.filter(function() {
  390. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  391. return ('block' == display) || (that.results[field][v] != that.STATUS_VALID);
  392. }).length == 0
  393. ) {
  394. this._disableSubmitButtons(false);
  395. $parent.removeClass('has-error').addClass('has-success');
  396. if ($icon) {
  397. $icon.removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.valid).show();
  398. }
  399. }
  400. break;
  401. case this.STATUS_NOT_VALIDATED:
  402. default:
  403. this._disableSubmitButtons(false);
  404. $parent.removeClass('has-success').removeClass('has-error');
  405. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  406. if ($icon) {
  407. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  408. }
  409. break;
  410. }
  411. return this;
  412. },
  413. // Useful APIs which aren't used internally
  414. /**
  415. * Reset the form
  416. *
  417. * @param {Boolean} resetFormData Reset current form data
  418. * @return {BootstrapValidator}
  419. */
  420. resetForm: function(resetFormData) {
  421. var field, $field, type;
  422. for (field in this.options.fields) {
  423. this.dfds[field] = {};
  424. this.results[field] = {};
  425. $field = this.getFieldElements(field);
  426. // Mark field as not validated yet
  427. this.updateStatus($field, this.STATUS_NOT_VALIDATED, null);
  428. if (resetFormData) {
  429. type = $field.attr('type');
  430. ('radio' == type || 'checkbox' == type) ? $field.removeAttr('checked').removeAttr('selected') : $field.val('');
  431. }
  432. }
  433. this.invalidField = null;
  434. this.$submitButton = null;
  435. // Enable submit buttons
  436. this._disableSubmitButtons(false);
  437. return this;
  438. },
  439. /**
  440. * Enable/Disable all validators to given field
  441. *
  442. * @param {String} field The field name
  443. * @param {Boolean} enabled Enable/Disable field validators
  444. * @return {BootstrapValidator}
  445. */
  446. enableFieldValidators: function(field, enabled) {
  447. this.options.fields[field]['enabled'] = enabled;
  448. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  449. return this;
  450. }
  451. };
  452. // Plugin definition
  453. $.fn.bootstrapValidator = function(options) {
  454. return this.each(function() {
  455. var $this = $(this), data = $this.data('bootstrapValidator');
  456. if (!data) {
  457. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  458. }
  459. if ('string' == typeof options) {
  460. data[options]();
  461. }
  462. });
  463. };
  464. // Available validators
  465. $.fn.bootstrapValidator.validators = {};
  466. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  467. }(window.jQuery));
  468. ;(function($) {
  469. $.fn.bootstrapValidator.validators.base64 = {
  470. /**
  471. * Return true if the input value is a base 64 encoded string.
  472. *
  473. * @param {BootstrapValidator} validator The validator plugin instance
  474. * @param {jQuery} $field Field element
  475. * @param {Object} options Can consist of the following keys:
  476. * - message: The invalid message
  477. * @returns {Boolean}
  478. */
  479. validate: function(validator, $field, options) {
  480. var value = $field.val();
  481. if (value == '') {
  482. return true;
  483. }
  484. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  485. }
  486. };
  487. }(window.jQuery));
  488. ;(function($) {
  489. $.fn.bootstrapValidator.validators.between = {
  490. /**
  491. * Return true if the input value is between (strictly or not) two given numbers
  492. *
  493. * @param {BootstrapValidator} validator The validator plugin instance
  494. * @param {jQuery} $field Field element
  495. * @param {Object} options Can consist of the following keys:
  496. * - min
  497. * - max
  498. * - inclusive [optional]: Can be true or false. Default is true
  499. * - message: The invalid message
  500. * @returns {Boolean}
  501. */
  502. validate: function(validator, $field, options) {
  503. var value = $field.val();
  504. if (value == '') {
  505. return true;
  506. }
  507. value = parseFloat(value);
  508. return (options.inclusive === true)
  509. ? (value > options.min && value < options.max)
  510. : (value >= options.min && value <= options.max);
  511. }
  512. };
  513. }(window.jQuery));
  514. ;(function($) {
  515. $.fn.bootstrapValidator.validators.callback = {
  516. /**
  517. * Return result from the callback method
  518. *
  519. * @param {BootstrapValidator} validator The validator plugin instance
  520. * @param {jQuery} $field Field element
  521. * @param {Object} options Can consist of the following keys:
  522. * - callback: The callback method that passes 2 parameters:
  523. * callback: function(fieldValue, validator) {
  524. * // fieldValue is the value of field
  525. * // validator is instance of BootstrapValidator
  526. * }
  527. * - message: The invalid message
  528. * @returns {Boolean|Deferred}
  529. */
  530. validate: function(validator, $field, options) {
  531. var value = $field.val();
  532. if (options.callback && 'function' == typeof options.callback) {
  533. var dfd = new $.Deferred();
  534. dfd.resolve(options.callback.call(this, value, validator), 'callback');
  535. return dfd;
  536. }
  537. return true;
  538. }
  539. };
  540. }(window.jQuery));
  541. ;(function($) {
  542. $.fn.bootstrapValidator.validators.choice = {
  543. /**
  544. * Check if the number of checked boxes are less or more than a given number
  545. *
  546. * @param {BootstrapValidator} validator The validator plugin instance
  547. * @param {jQuery} $field Field element
  548. * @param {Object} options Consists of following keys:
  549. * - min
  550. * - max
  551. * At least one of two keys is required
  552. * @returns {Boolean}
  553. */
  554. validate: function(validator, $field, options) {
  555. var numChoices = validator
  556. .getFieldElements($field.attr('name'))
  557. .filter(':checked')
  558. .length;
  559. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  560. return false;
  561. }
  562. return true;
  563. }
  564. };
  565. }(window.jQuery));
  566. ;(function($) {
  567. $.fn.bootstrapValidator.validators.creditCard = {
  568. /**
  569. * Return true if the input value is valid credit card number
  570. * Based on https://gist.github.com/DiegoSalazar/4075533
  571. *
  572. * @param {BootstrapValidator} validator The validator plugin instance
  573. * @param {jQuery} $field Field element
  574. * @param {Object} options Can consist of the following key:
  575. * - message: The invalid message
  576. * @returns {Boolean}
  577. */
  578. validate: function(validator, $field, options) {
  579. var value = $field.val();
  580. if (value == '') {
  581. return true;
  582. }
  583. // Accept only digits, dashes or spaces
  584. if (/[^0-9-\s]+/.test(value)) {
  585. return false;
  586. }
  587. value = value.replace(/\D/g, '');
  588. // Validate the check sum
  589. // The Luhn Algorithm
  590. // http://en.wikipedia.org/wiki/Luhn
  591. var check = 0, digit = 0, even = false, length = value.length;
  592. for (var n = length - 1; n >= 0; n--) {
  593. digit = parseInt(value.charAt(n), 10);
  594. if (even) {
  595. if ((digit *= 2) > 9) {
  596. digit -= 9;
  597. }
  598. }
  599. check += digit;
  600. even = !even;
  601. }
  602. if ((check % 10) != 0) {
  603. return false;
  604. }
  605. // Validate the card number based on pattern
  606. var cards = {
  607. AMERICAN_EXPRESS: {
  608. length: [15],
  609. ccv: ['34', '37']
  610. },
  611. DINERS_CLUB: {
  612. length: [14],
  613. ccv: ['300', '301', '302', '303', '304', '305', '36']
  614. },
  615. DINERS_CLUB_US: {
  616. length: [16],
  617. ccv: ['54', '55']
  618. },
  619. DISCOVER: {
  620. length: [16],
  621. ccv: ['6011', '622126', '622127', '622128', '622129', '62213',
  622. '62214', '62215', '62216', '62217', '62218', '62219',
  623. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  624. '62290', '62291', '622920', '622921', '622922', '622923',
  625. '622924', '622925', '644', '645', '646', '647', '648',
  626. '649', '65']
  627. },
  628. JCB: {
  629. length: [16],
  630. ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  631. },
  632. LASER: {
  633. length: [16, 17, 18, 19],
  634. ccv: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  635. },
  636. MAESTRO: {
  637. length: [12, 13, 14, 15, 16, 17, 18, 19],
  638. ccv: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  639. },
  640. MASTERCARD: {
  641. length: [16],
  642. ccv: ['51', '52', '53', '54', '55']
  643. },
  644. SOLO: {
  645. length: [16, 18, 19],
  646. ccv: ['6334', '6767']
  647. },
  648. UNIONPAY: {
  649. length: [16, 17, 18, 19],
  650. ccv: ['622126', '622127', '622128', '622129', '62213', '62214',
  651. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  652. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  653. '622920', '622921', '622922', '622923', '622924', '622925']
  654. },
  655. VISA: {
  656. length: ['16'],
  657. ccv: ['4']
  658. }
  659. };
  660. var type, prefix;
  661. for (type in cards) {
  662. for (prefix in cards[type]['ccv']) {
  663. if (value.substr(0, prefix.length) == prefix // Check the prefix
  664. && cards[type]['length'].indexOf(value.length) != -1) // and length
  665. {
  666. return true;
  667. }
  668. }
  669. }
  670. return false;
  671. }
  672. };
  673. }(window.jQuery));
  674. ;(function($) {
  675. $.fn.bootstrapValidator.validators.cvv = {
  676. /**
  677. * Return true if the input value is a valid CVV number.
  678. *
  679. * @param {BootstrapValidator} validator The validator plugin instance
  680. * @param {jQuery} $field Field element
  681. * @param {Object} options Can consist of the following keys:
  682. * - message: The invalid message
  683. * @returns {Boolean}
  684. */
  685. validate: function(validator, $field, options) {
  686. var value = $field.val();
  687. if (value == '') {
  688. return true;
  689. }
  690. return /^[0-9]{3,4}$/.test(value);
  691. }
  692. };
  693. }(window.jQuery));
  694. ;(function($) {
  695. $.fn.bootstrapValidator.validators.date = {
  696. /**
  697. * Return true if the input value is valid date
  698. *
  699. * @param {BootstrapValidator} validator The validator plugin instance
  700. * @param {jQuery} $field Field element
  701. * @param {Object} options Can consist of the following keys:
  702. * - format: The date format. Default is MM/DD/YYYY
  703. * Support the following formats:
  704. * YYYY/DD/MM
  705. * YYYY/DD/MM h:m A
  706. * YYYY/MM/DD
  707. * YYYY/MM/DD h:m A
  708. *
  709. * YYYY-DD-MM
  710. * YYYY-DD-MM h:m A
  711. * YYYY-MM-DD
  712. * YYYY-MM-DD h:m A
  713. *
  714. * MM/DD/YYYY
  715. * MM/DD/YYYY h:m A
  716. * DD/MM/YYYY
  717. * DD/MM/YYYY h:m A
  718. *
  719. * MM-DD-YYYY
  720. * MM-DD-YYYY h:m A
  721. * DD-MM-YYYY
  722. * DD-MM-YYYY h:m A
  723. * - message: The invalid message
  724. * @returns {Boolean}
  725. */
  726. validate: function(validator, $field, options) {
  727. var value = $field.val();
  728. if (value == '') {
  729. return true;
  730. }
  731. // Determine the separator
  732. options.format = options.format || 'MM/DD/YYYY';
  733. var separator = (options.format.indexOf('/') != -1)
  734. ? '/'
  735. : ((options.format.indexOf('-') != -1) ? '-' : null);
  736. if (separator == null) {
  737. return false;
  738. }
  739. var month, day, year, minutes = null, hours = null, matches;
  740. switch (true) {
  741. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  742. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  743. year = matches[1]; day = matches[2]; month = matches[3];
  744. break;
  745. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  746. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  747. day = matches[1]; month = matches[2]; year = matches[3];
  748. break;
  749. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  750. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  751. year = matches[1]; month = matches[2]; day = matches[3];
  752. break;
  753. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  754. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  755. month = matches[1]; day = matches[2]; year = matches[3];
  756. break;
  757. 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'):
  758. 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'):
  759. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  760. break;
  761. 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'):
  762. 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'):
  763. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  764. break;
  765. 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'):
  766. 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'):
  767. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  768. break;
  769. 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'):
  770. 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'):
  771. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  772. break;
  773. default:
  774. return false;
  775. }
  776. // Validate hours and minutes
  777. if (hours && minutes) {
  778. hours = parseInt(hours, 10);
  779. minutes = parseInt(minutes, 10);
  780. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  781. return false;
  782. }
  783. }
  784. // Validate day, month, and year
  785. day = parseInt(day, 10);
  786. month = parseInt(month, 10);
  787. year = parseInt(year, 10);
  788. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  789. return false;
  790. }
  791. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  792. // Update the number of days in Feb of leap year
  793. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  794. numDays[1] = 29;
  795. }
  796. // Check the day
  797. return (day > 0 && day <= numDays[month - 1]);
  798. }
  799. };
  800. }(window.jQuery));
  801. ;(function($) {
  802. $.fn.bootstrapValidator.validators.different = {
  803. /**
  804. * Return true if the input value is different with given field's value
  805. *
  806. * @param {BootstrapValidator} validator The validator plugin instance
  807. * @param {jQuery} $field Field element
  808. * @param {Object} options Consists of the following key:
  809. * - field: The name of field that will be used to compare with current one
  810. * @returns {Boolean}
  811. */
  812. validate: function(validator, $field, options) {
  813. var value = $field.val();
  814. if (value == '') {
  815. return true;
  816. }
  817. var compareWith = validator.getFieldElements(options.field);
  818. if (compareWith == null) {
  819. return true;
  820. }
  821. if (value != compareWith.val()) {
  822. validator.updateStatus(compareWith, validator.STATUS_VALID, 'different');
  823. return true;
  824. } else {
  825. return false;
  826. }
  827. }
  828. };
  829. }(window.jQuery));
  830. ;(function($) {
  831. $.fn.bootstrapValidator.validators.digits = {
  832. /**
  833. * Return true if the input value contains digits only
  834. *
  835. * @param {BootstrapValidator} validator Validate plugin instance
  836. * @param {jQuery} $field Field element
  837. * @param {Object} options
  838. * @returns {Boolean}
  839. */
  840. validate: function(validator, $field, options) {
  841. var value = $field.val();
  842. if (value == '') {
  843. return true;
  844. }
  845. return /^\d+$/.test(value);
  846. }
  847. }
  848. }(window.jQuery));
  849. ;(function($) {
  850. $.fn.bootstrapValidator.validators.emailAddress = {
  851. /**
  852. * Return true if and only if the input value is a valid email address
  853. *
  854. * @param {BootstrapValidator} validator Validate plugin instance
  855. * @param {jQuery} $field Field element
  856. * @param {Object} options
  857. * @returns {Boolean}
  858. */
  859. validate: function(validator, $field, options) {
  860. var value = $field.val();
  861. if (value == '') {
  862. return true;
  863. }
  864. // Email address regular expression
  865. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  866. 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,}))$/;
  867. return emailRegExp.test(value);
  868. }
  869. }
  870. }(window.jQuery));
  871. ;(function($) {
  872. $.fn.bootstrapValidator.validators.greaterThan = {
  873. /**
  874. * Return true if the input value is greater than or equals to given number
  875. *
  876. * @param {BootstrapValidator} validator Validate plugin instance
  877. * @param {jQuery} $field Field element
  878. * @param {Object} options Can consist of the following keys:
  879. * - value: The number used to compare to
  880. * - inclusive [optional]: Can be true or false. Default is true
  881. * - message: The invalid message
  882. * @returns {Boolean}
  883. */
  884. validate: function(validator, $field, options) {
  885. var value = $field.val();
  886. if (value == '') {
  887. return true;
  888. }
  889. value = parseFloat(value);
  890. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  891. }
  892. }
  893. }(window.jQuery));
  894. ;(function($) {
  895. $.fn.bootstrapValidator.validators.hexColor = {
  896. /**
  897. * Return true if the input value is a valid hex color
  898. *
  899. * @param {BootstrapValidator} validator The validator plugin instance
  900. * @param {jQuery} $field Field element
  901. * @param {Object} options Can consist of the following keys:
  902. * - message: The invalid message
  903. * @returns {Boolean}
  904. */
  905. validate: function(validator, $field, options) {
  906. var value = $field.val();
  907. if (value == '') {
  908. return true;
  909. }
  910. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  911. }
  912. };
  913. }(window.jQuery));
  914. ;(function($) {
  915. $.fn.bootstrapValidator.validators.identical = {
  916. /**
  917. * Check if input value equals to value of particular one
  918. *
  919. * @param {BootstrapValidator} validator The validator plugin instance
  920. * @param {jQuery} $field Field element
  921. * @param {Object} options Consists of the following key:
  922. * - field: The name of field that will be used to compare with current one
  923. * @returns {Boolean}
  924. */
  925. validate: function(validator, $field, options) {
  926. var value = $field.val();
  927. if (value == '') {
  928. return true;
  929. }
  930. var compareWith = validator.getFieldElements(options.field);
  931. if (compareWith == null) {
  932. return true;
  933. }
  934. if (value == compareWith.val()) {
  935. validator.updateStatus(compareWith, validator.STATUS_VALID, 'identical');
  936. return true;
  937. } else {
  938. return false;
  939. }
  940. }
  941. };
  942. }(window.jQuery));
  943. ;(function($) {
  944. $.fn.bootstrapValidator.validators.ip = {
  945. /**
  946. * Return true if the input value is a IP address.
  947. * Currently, it only supports IP v4
  948. *
  949. * @param {BootstrapValidator} validator The validator plugin instance
  950. * @param {jQuery} $field Field element
  951. * @param {Object} options Can consist of the following keys:
  952. * - message: The invalid message
  953. * @returns {Boolean}
  954. */
  955. validate: function(validator, $field, options) {
  956. var value = $field.val();
  957. if (value == '') {
  958. return true;
  959. }
  960. 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);
  961. }
  962. };
  963. }(window.jQuery));
  964. ;(function($) {
  965. $.fn.bootstrapValidator.validators.lessThan = {
  966. /**
  967. * Return true if the input value is less than or equal to given number
  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. * - value: The number used to compare to
  973. * - inclusive [optional]: Can be true or false. Default is true
  974. * - message: The invalid message
  975. * @returns {Boolean}
  976. */
  977. validate: function(validator, $field, options) {
  978. var value = $field.val();
  979. if (value == '') {
  980. return true;
  981. }
  982. value = parseFloat(value);
  983. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  984. }
  985. };
  986. }(window.jQuery));
  987. ;(function($) {
  988. $.fn.bootstrapValidator.validators.mac = {
  989. /**
  990. * Return true if the input value is a MAC address.
  991. *
  992. * @param {BootstrapValidator} validator The validator plugin instance
  993. * @param {jQuery} $field Field element
  994. * @param {Object} options Can consist of the following keys:
  995. * - message: The invalid message
  996. * @returns {Boolean}
  997. */
  998. validate: function(validator, $field, options) {
  999. var value = $field.val();
  1000. if (value == '') {
  1001. return true;
  1002. }
  1003. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1004. }
  1005. };
  1006. }(window.jQuery));
  1007. ;(function($) {
  1008. $.fn.bootstrapValidator.validators.notEmpty = {
  1009. /**
  1010. * Check if input value is empty or not
  1011. *
  1012. * @param {BootstrapValidator} validator The validator plugin instance
  1013. * @param {jQuery} $field Field element
  1014. * @param {Object} options
  1015. * @returns {Boolean}
  1016. */
  1017. validate: function(validator, $field, options) {
  1018. var type = $field.attr('type');
  1019. if ('radio' == type || 'checkbox' == type) {
  1020. return validator
  1021. .getFieldElements($field.attr('name'))
  1022. .filter(':checked')
  1023. .length > 0;
  1024. }
  1025. return $.trim($field.val()) != '';
  1026. }
  1027. };
  1028. }(window.jQuery));
  1029. ;(function($) {
  1030. $.fn.bootstrapValidator.validators.phone = {
  1031. /**
  1032. * Return true if the input value contains a valid US phone number only
  1033. *
  1034. * @param {BootstrapValidator} validator Validate plugin instance
  1035. * @param {jQuery} $field Field element
  1036. * @param {Object} options Consist of key:
  1037. * - country: The ISO 3166 country code
  1038. *
  1039. * Currently it only supports United State (US) country
  1040. * @returns {Boolean}
  1041. */
  1042. validate: function(validator, $field, options) {
  1043. var value = $field.val();
  1044. if (value == '') {
  1045. return true;
  1046. }
  1047. options.country = options.country || 'US';
  1048. switch (options.country.toUpperCase()) {
  1049. case 'US':
  1050. default:
  1051. value = value.replace(/\(|\)|\s+/g, '');
  1052. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1053. }
  1054. }
  1055. }
  1056. }(window.jQuery));
  1057. ;(function($) {
  1058. $.fn.bootstrapValidator.validators.regexp = {
  1059. /**
  1060. * Check if the element value matches given regular expression
  1061. *
  1062. * @param {BootstrapValidator} validator The validator plugin instance
  1063. * @param {jQuery} $field Field element
  1064. * @param {Object} options Consists of the following key:
  1065. * - regexp: The regular expression you need to check
  1066. * @returns {Boolean}
  1067. */
  1068. validate: function(validator, $field, options) {
  1069. var value = $field.val();
  1070. if (value == '') {
  1071. return true;
  1072. }
  1073. return options.regexp.test(value);
  1074. }
  1075. };
  1076. }(window.jQuery));
  1077. ;(function($) {
  1078. $.fn.bootstrapValidator.validators.remote = {
  1079. /**
  1080. * Request a remote server to check the input value
  1081. *
  1082. * @param {BootstrapValidator} validator Plugin instance
  1083. * @param {jQuery} $field Field element
  1084. * @param {Object} options Can consist of the following keys:
  1085. * - url
  1086. * - data [optional]: By default, it will take the value
  1087. * {
  1088. * <fieldName>: <fieldValue>
  1089. * }
  1090. * - message: The invalid message
  1091. * @returns {Boolean|Deferred}
  1092. */
  1093. validate: function(validator, $field, options) {
  1094. var value = $field.val();
  1095. if (value == '') {
  1096. return true;
  1097. }
  1098. var name = $field.attr('name'), data = options.data;
  1099. if (data == null) {
  1100. data = {};
  1101. }
  1102. // Support dynamic data
  1103. if ('function' == typeof data) {
  1104. data = data.call(this, validator);
  1105. }
  1106. data[name] = value;
  1107. var dfd = new $.Deferred();
  1108. var xhr = $.ajax({
  1109. type: 'POST',
  1110. url: options.url,
  1111. dataType: 'json',
  1112. data: data
  1113. });
  1114. xhr.then(function(response) {
  1115. dfd.resolve(response.valid === true || response.valid === 'true', 'remote');
  1116. });
  1117. dfd.fail(function() {
  1118. xhr.abort();
  1119. });
  1120. return dfd;
  1121. }
  1122. };
  1123. }(window.jQuery));
  1124. ;(function($) {
  1125. $.fn.bootstrapValidator.validators.stringLength = {
  1126. /**
  1127. * Check if the length of element value is less or more than given number
  1128. *
  1129. * @param {BootstrapValidator} validator The validator plugin instance
  1130. * @param {jQuery} $field Field element
  1131. * @param {Object} options Consists of following keys:
  1132. * - min
  1133. * - max
  1134. * At least one of two keys is required
  1135. * @returns {Boolean}
  1136. */
  1137. validate: function(validator, $field, options) {
  1138. var value = $field.val();
  1139. if (value == '') {
  1140. return true;
  1141. }
  1142. var length = $.trim(value).length;
  1143. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. };
  1149. }(window.jQuery));
  1150. ;(function($) {
  1151. $.fn.bootstrapValidator.validators.uri = {
  1152. /**
  1153. * Return true if the input value is a valid URL
  1154. *
  1155. * @param {BootstrapValidator} validator The validator plugin instance
  1156. * @param {jQuery} $field Field element
  1157. * @param {Object} options
  1158. * @returns {Boolean}
  1159. */
  1160. validate: function(validator, $field, options) {
  1161. var value = $field.val();
  1162. if (value == '') {
  1163. return true;
  1164. }
  1165. // Credit to https://gist.github.com/dperini/729294
  1166. //
  1167. // Regular Expression for URL validation
  1168. //
  1169. // Author: Diego Perini
  1170. // Updated: 2010/12/05
  1171. //
  1172. // the regular expression composed & commented
  1173. // could be easily tweaked for RFC compliance,
  1174. // it was expressly modified to fit & satisfy
  1175. // these test for an URL shortener:
  1176. //
  1177. // http://mathiasbynens.be/demo/url-regex
  1178. //
  1179. // Notes on possible differences from a standard/generic validation:
  1180. //
  1181. // - utf-8 char class take in consideration the full Unicode range
  1182. // - TLDs have been made mandatory so single names like "localhost" fails
  1183. // - protocols have been restricted to ftp, http and https only as requested
  1184. //
  1185. // Changes:
  1186. //
  1187. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1188. // first and last IP address of each class is considered invalid
  1189. // (since they are broadcast/network addresses)
  1190. //
  1191. // - Added exclusion of private, reserved and/or local networks ranges
  1192. //
  1193. // Compressed one-line versions:
  1194. //
  1195. // Javascript version
  1196. //
  1197. // /^(?:(?: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
  1198. //
  1199. // PHP version
  1200. //
  1201. // _^(?:(?: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
  1202. var urlExp = new RegExp(
  1203. "^" +
  1204. // protocol identifier
  1205. "(?:(?:https?|ftp)://)" +
  1206. // user:pass authentication
  1207. "(?:\\S+(?::\\S*)?@)?" +
  1208. "(?:" +
  1209. // IP address exclusion
  1210. // private & local networks
  1211. "(?!10(?:\\.\\d{1,3}){3})" +
  1212. "(?!127(?:\\.\\d{1,3}){3})" +
  1213. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1214. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1215. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1216. // IP address dotted notation octets
  1217. // excludes loopback network 0.0.0.0
  1218. // excludes reserved space >= 224.0.0.0
  1219. // excludes network & broacast addresses
  1220. // (first & last IP address of each class)
  1221. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1222. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1223. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1224. "|" +
  1225. // host name
  1226. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1227. // domain name
  1228. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1229. // TLD identifier
  1230. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1231. ")" +
  1232. // port number
  1233. "(?::\\d{2,5})?" +
  1234. // resource path
  1235. "(?:/[^\\s]*)?" +
  1236. "$", "i"
  1237. );
  1238. return urlExp.test(value);
  1239. }
  1240. };
  1241. }(window.jQuery));
  1242. ;(function($) {
  1243. $.fn.bootstrapValidator.validators.zipCode = {
  1244. /**
  1245. * Return true if and only if the input value is a valid country zip code
  1246. *
  1247. * @param {BootstrapValidator} validator The validator plugin instance
  1248. * @param {jQuery} $field Field element
  1249. * @param {Object} options Consist of key:
  1250. * - country: The ISO 3166 country code
  1251. *
  1252. * Currently it supports the following countries:
  1253. * - US (United State)
  1254. * - DK (Denmark)
  1255. * - SE (Sweden)
  1256. *
  1257. * @returns {Boolean}
  1258. */
  1259. validate: function(validator, $field, options) {
  1260. var value = $field.val();
  1261. if (value == '' || !options.country) {
  1262. return true;
  1263. }
  1264. options.country = options.country || 'US';
  1265. switch (options.country.toUpperCase()) {
  1266. case 'DK':
  1267. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  1268. case 'SE':
  1269. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  1270. case 'US':
  1271. default:
  1272. return /^\d{5}([\-]\d{4})?$/.test(value);
  1273. }
  1274. }
  1275. };
  1276. }(window.jQuery));