bootstrapValidator.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /**
  2. * BootstrapValidator v0.2.3 (http://github.com/nghuuphuoc/bootstrapvalidator)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @author Nguyen Huu Phuoc <phuoc@huuphuoc.me>
  7. * @copyright (c) 2013 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  14. if ('disabled' == this.options.live) {
  15. // Don't disable the submit buttons if the live validating is disabled
  16. this.options.submitButtons = null;
  17. }
  18. this.invalidFields = {};
  19. this.xhrRequests = {};
  20. this.numPendingRequests = null;
  21. this.formSubmited = false;
  22. this.submitHandlerCalled = false;
  23. this._init();
  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. // The submit buttons selector
  32. // These buttons will be disabled when the form input are invalid
  33. submitButtons: 'button[type="submit"]',
  34. // The custom submit handler
  35. // It will prevent the form from the default submitting
  36. //
  37. // submitHandler: function(validator, form) {
  38. // - validator is the BootstrapValidator instance
  39. // - form is the jQuery object present the current form
  40. // }
  41. submitHandler: null,
  42. // Live validating. Can be one of 3 values:
  43. // - enabled: The plugin validates fields as soon as they are changed
  44. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  45. // - submitted: The live validating is enabled after the form is submitted
  46. live: 'enabled',
  47. // Map the field name with validator rules
  48. fields: null
  49. };
  50. BootstrapValidator.prototype = {
  51. constructor: BootstrapValidator,
  52. /**
  53. * Init form
  54. */
  55. _init: function() {
  56. if (this.options.fields == null) {
  57. return;
  58. }
  59. var that = this;
  60. this.$form
  61. // Disable client side validation in HTML 5
  62. .attr('novalidate', 'novalidate')
  63. .addClass(this.options.elementClass)
  64. .on('submit', function(e) {
  65. that.formSubmited = true;
  66. if (that.options.fields) {
  67. for (var field in that.options.fields) {
  68. if (that.numPendingRequests > 0 || that.numPendingRequests == null) {
  69. // Check if the field is valid
  70. var $field = that.getFieldElement(field);
  71. if ($field && $field.data('bootstrapValidator.isValid') !== true) {
  72. that.validateField(field);
  73. }
  74. }
  75. }
  76. if (!that.isValid()) {
  77. that.$form.find(that.options.submitButtons).attr('disabled', 'disabled');
  78. if ('submitted' == that.options.live) {
  79. that.options.live = 'enabled';
  80. that._setLiveValidating();
  81. }
  82. // Focus to the first invalid field
  83. for (var i in that.invalidFields) {
  84. that.getFieldElement(i).focus();
  85. break;
  86. }
  87. e.preventDefault();
  88. } else {
  89. if (!that.submitHandlerCalled && that.options.submitHandler && 'function' == typeof that.options.submitHandler) {
  90. // Avoid calling submitHandler recursively
  91. // in the case user call form.submit() right inside the submitHandler()
  92. that.submitHandlerCalled = true;
  93. that.options.submitHandler.call(that, that, that.$form);
  94. return false;
  95. }
  96. }
  97. }
  98. });
  99. for (var field in this.options.fields) {
  100. this._initField(field);
  101. }
  102. this._setLiveValidating();
  103. },
  104. /**
  105. * Enable live validating
  106. */
  107. _setLiveValidating: function() {
  108. if ('enabled' == this.options.live) {
  109. var that = this;
  110. // Since this should be called once, I have to disable the live validating mode
  111. this.options.live = 'disabled';
  112. for (var field in this.options.fields) {
  113. (function(field) {
  114. var $field = that.getFieldElement(field);
  115. if ($field) {
  116. var type = $field.attr('type'),
  117. event = ('checkbox' == type || 'radio' == type || 'SELECT' == $field.get(0).tagName) ? 'change' : 'keyup';
  118. $field.on(event, function() {
  119. that.formSubmited = false;
  120. that.validateField(field);
  121. });
  122. }
  123. })(field);
  124. }
  125. }
  126. },
  127. /**
  128. * Init field
  129. *
  130. * @param {String} field The field name
  131. */
  132. _initField: function(field) {
  133. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  134. return;
  135. }
  136. var $field = this.getFieldElement(field);
  137. if (null == $field) {
  138. return;
  139. }
  140. // Create a help block element for showing the error
  141. var $parent = $field.parents('.form-group'),
  142. helpBlock = $parent.find('.help-block');
  143. if (helpBlock.length == 0) {
  144. var $small = $('<small/>').addClass('help-block').css('display', 'none').appendTo($parent);
  145. $field.data('bootstrapValidator.error', $small);
  146. // Calculate the number of columns of the label/field element
  147. // Then set offset to the help block element
  148. var label, cssClasses, offset, size;
  149. if (label = $parent.find('label').get(0)) {
  150. // The default Bootstrap form don't require class for label (http://getbootstrap.com/css/#forms)
  151. if (cssClasses = $(label).attr('class')) {
  152. cssClasses = cssClasses.split(' ');
  153. for (var i = 0; i < cssClasses.length; i++) {
  154. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
  155. offset = cssClasses[i].substr(7);
  156. size = cssClasses[i].substr(4, 2);
  157. break;
  158. }
  159. }
  160. }
  161. } else if (cssClasses = $parent.children().eq(0).attr('class')) {
  162. cssClasses = cssClasses.split(' ');
  163. for (var i = 0; i < cssClasses.length; i++) {
  164. if (/^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  165. offset = cssClasses[i].substr(14);
  166. size = cssClasses[i].substr(4, 2);
  167. break;
  168. }
  169. }
  170. }
  171. if (size && offset) {
  172. $small.addClass(['col-', size, '-offset-', offset].join(''))
  173. .addClass(['col-', size, '-', 12 - offset].join(''));
  174. }
  175. } else {
  176. $field.data('bootstrapValidator.error', helpBlock.eq(0));
  177. }
  178. },
  179. /**
  180. * Get field element
  181. *
  182. * @param {String} field The field name
  183. * @returns {jQuery}
  184. */
  185. getFieldElement: function(field) {
  186. var fields = this.$form.find('[name="' + field + '"]');
  187. return (fields.length == 0) ? null : $(fields[0]);
  188. },
  189. /**
  190. * Validate given field
  191. *
  192. * @param {String} field The field name
  193. */
  194. validateField: function(field) {
  195. var $field = this.getFieldElement(field);
  196. if (null == $field) {
  197. // Return if cannot find the field with given name
  198. return;
  199. }
  200. var that = this,
  201. validators = that.options.fields[field].validators;
  202. for (var validatorName in validators) {
  203. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  204. continue;
  205. }
  206. var isValid = $.fn.bootstrapValidator.validators[validatorName].validate(that, $field, validators[validatorName]);
  207. if (isValid === false) {
  208. that.showError($field, validatorName);
  209. break;
  210. } else if (isValid === true) {
  211. that.removeError($field);
  212. }
  213. }
  214. },
  215. /**
  216. * Show field error
  217. *
  218. * @param {jQuery} $field The field element
  219. * @param {String} validatorName
  220. */
  221. showError: function($field, validatorName) {
  222. var field = $field.attr('name'),
  223. validator = this.options.fields[field].validators[validatorName],
  224. message = validator.message || this.options.message,
  225. $parent = $field.parents('.form-group');
  226. this.invalidFields[field] = true;
  227. // Add has-error class to parent element
  228. $parent.removeClass('has-success').addClass('has-error');
  229. $field.data('bootstrapValidator.error').html(message).show();
  230. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  231. },
  232. /**
  233. * Remove error from given field
  234. *
  235. * @param {jQuery} $field The field element
  236. */
  237. removeError: function($field) {
  238. delete this.invalidFields[$field.attr('name')];
  239. $field.parents('.form-group').removeClass('has-error').addClass('has-success');
  240. $field.data('bootstrapValidator.error').hide();
  241. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  242. },
  243. /**
  244. * Start remote checking
  245. *
  246. * @param {jQuery} $field The field element
  247. * @param {String} validatorName
  248. * @param {XMLHttpRequest} xhr
  249. */
  250. startRequest: function($field, validatorName, xhr) {
  251. var field = $field.attr('name');
  252. $field.data('bootstrapValidator.isValid', false);
  253. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  254. if(this.numPendingRequests == null){
  255. this.numPendingRequests = 0;
  256. }
  257. this.numPendingRequests++;
  258. // Abort the previous request
  259. if (!this.xhrRequests[field]) {
  260. this.xhrRequests[field] = {};
  261. }
  262. if (this.xhrRequests[field][validatorName]) {
  263. this.numPendingRequests--;
  264. this.xhrRequests[field][validatorName].abort();
  265. }
  266. this.xhrRequests[field][validatorName] = xhr;
  267. },
  268. /**
  269. * Complete remote checking
  270. *
  271. * @param {jQuery} $field The field element
  272. * @param {String} validatorName
  273. * @param {Boolean} isValid
  274. */
  275. completeRequest: function($field, validatorName, isValid) {
  276. if (isValid === false) {
  277. this.showError($field, validatorName);
  278. } else if (isValid === true) {
  279. this.removeError($field);
  280. $field.data('bootstrapValidator.isValid', true);
  281. }
  282. var field = $field.attr('name');
  283. delete this.xhrRequests[field][validatorName];
  284. this.numPendingRequests--;
  285. if (this.numPendingRequests <= 0) {
  286. this.numPendingRequests = 0;
  287. if (this.formSubmited) {
  288. if (!this.submitHandlerCalled && this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  289. this.submitHandlerCalled = true;
  290. this.options.submitHandler.call(this, this, this.$form);
  291. } else {
  292. this.$form.submit();
  293. }
  294. }
  295. }
  296. },
  297. /**
  298. * Check the form validity
  299. *
  300. * @returns {Boolean}
  301. */
  302. isValid: function() {
  303. if (this.numPendingRequests > 0) {
  304. return false;
  305. }
  306. for (var field in this.invalidFields) {
  307. if (this.invalidFields[field] && !this.getFieldElement(field).is(':disabled')) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. };
  314. // Plugin definition
  315. $.fn.bootstrapValidator = function(options) {
  316. return this.each(function() {
  317. var $this = $(this), data = $this.data('bootstrapValidator');
  318. if (!data) {
  319. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  320. }
  321. });
  322. };
  323. // Available validators
  324. $.fn.bootstrapValidator.validators = {};
  325. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  326. }(window.jQuery));
  327. ;(function($) {
  328. $.fn.bootstrapValidator.validators.between = {
  329. /**
  330. * Return true if the input value is between (strictly or not) two given numbers
  331. *
  332. * @param {BootstrapValidator} validator The validator plugin instance
  333. * @param {jQuery} $field Field element
  334. * @param {Object} options Can consist of the following keys:
  335. * - min
  336. * - max
  337. * - inclusive [optional]: Can be true or false. Default is true
  338. * - message: The invalid message
  339. * @returns {Boolean}
  340. */
  341. validate: function(validator, $field, options) {
  342. var value = $field.val();
  343. if (value == '') {
  344. return true;
  345. }
  346. value = parseFloat(value);
  347. return (options.inclusive === true)
  348. ? (value > options.min && value < options.max)
  349. : (value >= options.min && value <= options.max);
  350. }
  351. };
  352. }(window.jQuery));
  353. ;(function($) {
  354. $.fn.bootstrapValidator.validators.callback = {
  355. /**
  356. * Return result from the callback method
  357. *
  358. * @param {BootstrapValidator} validator The validator plugin instance
  359. * @param {jQuery} $field Field element
  360. * @param {Object} options Can consist of the following keys:
  361. * - callback: The callback method that passes 2 parameters:
  362. * callback: function(fieldValue, validator) {
  363. * // fieldValue is the value of field
  364. * // validator is instance of BootstrapValidator
  365. * }
  366. * - message: The invalid message
  367. * @returns {Boolean}
  368. */
  369. validate: function(validator, $field, options) {
  370. var value = $field.val();
  371. if (options.callback && 'function' == typeof options.callback) {
  372. return options.callback.call(this, value, this);
  373. }
  374. return true;
  375. }
  376. };
  377. }(window.jQuery));
  378. ;(function($) {
  379. $.fn.bootstrapValidator.validators.creditCard = {
  380. /**
  381. * Return true if the input value is valid credit card number
  382. * Based on https://gist.github.com/DiegoSalazar/4075533
  383. *
  384. * @param {BootstrapValidator} validator The validator plugin instance
  385. * @param {jQuery} $field Field element
  386. * @param {Object} options Can consist of the following key:
  387. * - message: The invalid message
  388. * @returns {Boolean}
  389. */
  390. validate: function(validator, $field, options) {
  391. var value = $field.val();
  392. if (value == '') {
  393. return true;
  394. }
  395. // Accept only digits, dashes or spaces
  396. if (/[^0-9-\s]+/.test(value)) {
  397. return false;
  398. }
  399. // The Luhn Algorithm
  400. // http://en.wikipedia.org/wiki/Luhn
  401. value = value.replace(/\D/g, '');
  402. var check = 0, digit = 0, even = false, length = value.length;
  403. for (var n = length - 1; n >= 0; n--) {
  404. digit = parseInt(value.charAt(n), 10);
  405. if (even) {
  406. if ((digit *= 2) > 9) {
  407. digit -= 9;
  408. }
  409. }
  410. check += digit;
  411. even = !even;
  412. }
  413. return (check % 10) == 0;
  414. }
  415. };
  416. }(window.jQuery));
  417. ;(function($) {
  418. $.fn.bootstrapValidator.validators.different = {
  419. /**
  420. * Return true if the input value is different with given field's value
  421. *
  422. * @param {BootstrapValidator} validator The validator plugin instance
  423. * @param {jQuery} $field Field element
  424. * @param {Object} options Consists of the following key:
  425. * - field: The name of field that will be used to compare with current one
  426. * @returns {Boolean}
  427. */
  428. validate: function(validator, $field, options) {
  429. var value = $field.val();
  430. if (value == '') {
  431. return true;
  432. }
  433. var $compareWith = validator.getFieldElement(options.field);
  434. if ($compareWith && value != $compareWith.val()) {
  435. validator.removeError($compareWith);
  436. return true;
  437. } else {
  438. return false;
  439. }
  440. }
  441. };
  442. }(window.jQuery));
  443. ;(function($) {
  444. $.fn.bootstrapValidator.validators.digits = {
  445. /**
  446. * Return true if the input value contains digits only
  447. *
  448. * @param {BootstrapValidator} validator Validate plugin instance
  449. * @param {jQuery} $field Field element
  450. * @param {Object} options
  451. * @returns {Boolean}
  452. */
  453. validate: function(validator, $field, options) {
  454. var value = $field.val();
  455. if (value == '') {
  456. return true;
  457. }
  458. return /^\d+$/.test(value);
  459. }
  460. }
  461. }(window.jQuery));
  462. ;(function($) {
  463. $.fn.bootstrapValidator.validators.emailAddress = {
  464. /**
  465. * Return true if and only if the input value is a valid email address
  466. *
  467. * @param {BootstrapValidator} validator Validate plugin instance
  468. * @param {jQuery} $field Field element
  469. * @param {Object} options
  470. * @returns {Boolean}
  471. */
  472. validate: function(validator, $field, options) {
  473. var value = $field.val();
  474. if (value == '') {
  475. return true;
  476. }
  477. // Email address regular expression
  478. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  479. 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,}))$/;
  480. return emailRegExp.test(value);
  481. }
  482. }
  483. }(window.jQuery));
  484. ;(function($) {
  485. $.fn.bootstrapValidator.validators.greaterThan = {
  486. /**
  487. * Return true if the input value is greater than or equals to given number
  488. *
  489. * @param {BootstrapValidator} validator Validate plugin instance
  490. * @param {jQuery} $field Field element
  491. * @param {Object} options Can consist of the following keys:
  492. * - value: The number used to compare to
  493. * - inclusive [optional]: Can be true or false. Default is true
  494. * - message: The invalid message
  495. * @returns {Boolean}
  496. */
  497. validate: function(validator, $field, options) {
  498. var value = $field.val();
  499. if (value == '') {
  500. return true;
  501. }
  502. value = parseFloat(value);
  503. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  504. }
  505. }
  506. }(window.jQuery));
  507. ;(function($) {
  508. $.fn.bootstrapValidator.validators.hexColor = {
  509. /**
  510. * Return true if the input value is a valid hex color
  511. *
  512. * @param {BootstrapValidator} validator The validator plugin instance
  513. * @param {jQuery} $field Field element
  514. * @param {Object} options Can consist of the following keys:
  515. * - message: The invalid message
  516. * @returns {Boolean}
  517. */
  518. validate: function(validator, $field, options) {
  519. var value = $field.val();
  520. if (value == '') {
  521. return true;
  522. }
  523. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  524. }
  525. };
  526. }(window.jQuery));
  527. ;(function($) {
  528. $.fn.bootstrapValidator.validators.identical = {
  529. /**
  530. * Check if input value equals to value of particular one
  531. *
  532. * @param {BootstrapValidator} validator The validator plugin instance
  533. * @param {jQuery} $field Field element
  534. * @param {Object} options Consists of the following key:
  535. * - field: The name of field that will be used to compare with current one
  536. * @returns {Boolean}
  537. */
  538. validate: function(validator, $field, options) {
  539. var value = $field.val();
  540. if (value == '') {
  541. return true;
  542. }
  543. var $compareWith = validator.getFieldElement(options.field);
  544. if ($compareWith && value == $compareWith.val()) {
  545. validator.removeError($compareWith);
  546. return true;
  547. } else {
  548. return false;
  549. }
  550. }
  551. };
  552. }(window.jQuery));
  553. ;(function($) {
  554. $.fn.bootstrapValidator.validators.lessThan = {
  555. /**
  556. * Return true if the input value is less than or equal to given number
  557. *
  558. * @param {BootstrapValidator} validator The validator plugin instance
  559. * @param {jQuery} $field Field element
  560. * @param {Object} options Can consist of the following keys:
  561. * - value: The number used to compare to
  562. * - inclusive [optional]: Can be true or false. Default is true
  563. * - message: The invalid message
  564. * @returns {Boolean}
  565. */
  566. validate: function(validator, $field, options) {
  567. var value = $field.val();
  568. if (value == '') {
  569. return true;
  570. }
  571. value = parseFloat(value);
  572. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  573. }
  574. };
  575. }(window.jQuery));
  576. ;(function($) {
  577. $.fn.bootstrapValidator.validators.notEmpty = {
  578. /**
  579. * Check if input value is empty or not
  580. *
  581. * @param {BootstrapValidator} validator The validator plugin instance
  582. * @param {jQuery} $field Field element
  583. * @param {Object} options
  584. * @returns {Boolean}
  585. */
  586. validate: function(validator, $field, options) {
  587. var type = $field.attr('type');
  588. return ('checkbox' == type || 'radio' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
  589. }
  590. };
  591. }(window.jQuery));
  592. ;(function($) {
  593. $.fn.bootstrapValidator.validators.regexp = {
  594. /**
  595. * Check if the element value matches given regular expression
  596. *
  597. * @param {BootstrapValidator} validator The validator plugin instance
  598. * @param {jQuery} $field Field element
  599. * @param {Object} options Consists of the following key:
  600. * - regexp: The regular expression you need to check
  601. * @returns {Boolean}
  602. */
  603. validate: function(validator, $field, options) {
  604. var value = $field.val();
  605. if (value == '') {
  606. return true;
  607. }
  608. return options.regexp.test(value);
  609. }
  610. };
  611. }(window.jQuery));
  612. ;(function($) {
  613. $.fn.bootstrapValidator.validators.remote = {
  614. /**
  615. * Request a remote server to check the input value
  616. *
  617. * @param {BootstrapValidator} validator Plugin instance
  618. * @param {jQuery} $field Field element
  619. * @param {Object} options Can consist of the following keys:
  620. * - url
  621. * - data [optional]: By default, it will take the value
  622. * {
  623. * <fieldName>: <fieldValue>
  624. * }
  625. * - message: The invalid message
  626. * @returns {Boolean|String}
  627. */
  628. validate: function(validator, $field, options) {
  629. var value = $field.val();
  630. if (value == '') {
  631. return true;
  632. }
  633. var name = $field.attr('name'), data = options.data;
  634. if (data == null) {
  635. data = {};
  636. }
  637. data[name] = value;
  638. var xhr = $.ajax({
  639. type: 'POST',
  640. url: options.url,
  641. dataType: 'json',
  642. data: data
  643. }).success(function(response) {
  644. var isValid = response.valid === true || response.valid === 'true';
  645. validator.completeRequest($field, 'remote', isValid);
  646. });
  647. validator.startRequest($field, 'remote', xhr);
  648. return 'pending';
  649. }
  650. };
  651. }(window.jQuery));
  652. ;(function($) {
  653. $.fn.bootstrapValidator.validators.stringLength = {
  654. /**
  655. * Check if the length of element value is less or more than given number
  656. *
  657. * @param {BootstrapValidator} validator The validator plugin instance
  658. * @param {jQuery} $field Field element
  659. * @param {Object} options Consists of following keys:
  660. * - min
  661. * - max
  662. * At least one of two keys is required
  663. * @returns {Boolean}
  664. */
  665. validate: function(validator, $field, options) {
  666. var value = $field.val();
  667. if (value == '') {
  668. return true;
  669. }
  670. var length = $.trim(value).length;
  671. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  672. return false;
  673. }
  674. return true;
  675. }
  676. };
  677. }(window.jQuery));
  678. ;(function($) {
  679. $.fn.bootstrapValidator.validators.uri = {
  680. /**
  681. * Return true if the input value is a valid URL
  682. *
  683. * @param {BootstrapValidator} validator The validator plugin instance
  684. * @param {jQuery} $field Field element
  685. * @param {Object} options
  686. * @returns {Boolean}
  687. */
  688. validate: function(validator, $field, options) {
  689. var value = $field.val();
  690. if (value == '') {
  691. return true;
  692. }
  693. // Credit to https://gist.github.com/dperini/729294
  694. //
  695. // Regular Expression for URL validation
  696. //
  697. // Author: Diego Perini
  698. // Updated: 2010/12/05
  699. //
  700. // the regular expression composed & commented
  701. // could be easily tweaked for RFC compliance,
  702. // it was expressly modified to fit & satisfy
  703. // these test for an URL shortener:
  704. //
  705. // http://mathiasbynens.be/demo/url-regex
  706. //
  707. // Notes on possible differences from a standard/generic validation:
  708. //
  709. // - utf-8 char class take in consideration the full Unicode range
  710. // - TLDs have been made mandatory so single names like "localhost" fails
  711. // - protocols have been restricted to ftp, http and https only as requested
  712. //
  713. // Changes:
  714. //
  715. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  716. // first and last IP address of each class is considered invalid
  717. // (since they are broadcast/network addresses)
  718. //
  719. // - Added exclusion of private, reserved and/or local networks ranges
  720. //
  721. // Compressed one-line versions:
  722. //
  723. // Javascript version
  724. //
  725. // /^(?:(?: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
  726. //
  727. // PHP version
  728. //
  729. // _^(?:(?: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
  730. var urlExp = new RegExp(
  731. "^" +
  732. // protocol identifier
  733. "(?:(?:https?|ftp)://)" +
  734. // user:pass authentication
  735. "(?:\\S+(?::\\S*)?@)?" +
  736. "(?:" +
  737. // IP address exclusion
  738. // private & local networks
  739. "(?!10(?:\\.\\d{1,3}){3})" +
  740. "(?!127(?:\\.\\d{1,3}){3})" +
  741. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  742. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  743. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  744. // IP address dotted notation octets
  745. // excludes loopback network 0.0.0.0
  746. // excludes reserved space >= 224.0.0.0
  747. // excludes network & broacast addresses
  748. // (first & last IP address of each class)
  749. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  750. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  751. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  752. "|" +
  753. // host name
  754. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  755. // domain name
  756. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  757. // TLD identifier
  758. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  759. ")" +
  760. // port number
  761. "(?::\\d{2,5})?" +
  762. // resource path
  763. "(?:/[^\\s]*)?" +
  764. "$", "i"
  765. );
  766. return urlExp.test(value);
  767. }
  768. };
  769. }(window.jQuery));
  770. ;(function($) {
  771. $.fn.bootstrapValidator.validators.usZipCode = {
  772. /**
  773. * Return true if and only if the input value is a valid US zip code
  774. *
  775. * @param {BootstrapValidator} validator The validator plugin instance
  776. * @param {jQuery} $field Field element
  777. * @param {Object} options
  778. * @returns {Boolean}
  779. */
  780. validate: function(validateInstance, $field, options) {
  781. var value = $field.val();
  782. if (value == '') {
  783. return true;
  784. }
  785. return /^\d{5}([\-]\d{4})?$/.test(value);
  786. }
  787. };
  788. }(window.jQuery));