bootstrapValidator.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /**
  2. * BootstrapValidator v0.2.2 (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]) {
  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 (value == '') {
  372. return true;
  373. }
  374. if (options.callback && 'function' == typeof options.callback) {
  375. return options.callback.call(this, value, this);
  376. }
  377. return true;
  378. }
  379. };
  380. }(window.jQuery));
  381. ;(function($) {
  382. $.fn.bootstrapValidator.validators.creditCard = {
  383. /**
  384. * Return true if the input value is valid credit card number
  385. * Based on https://gist.github.com/DiegoSalazar/4075533
  386. *
  387. * @param {BootstrapValidator} validator The validator plugin instance
  388. * @param {jQuery} $field Field element
  389. * @param {Object} options Can consist of the following key:
  390. * - message: The invalid message
  391. * @returns {Boolean}
  392. */
  393. validate: function(validator, $field, options) {
  394. var value = $field.val();
  395. if (value == '') {
  396. return true;
  397. }
  398. // Accept only digits, dashes or spaces
  399. if (/[^0-9-\s]+/.test(value)) {
  400. return false;
  401. }
  402. // The Luhn Algorithm
  403. // http://en.wikipedia.org/wiki/Luhn
  404. value = value.replace(/\D/g, '');
  405. var check = 0, digit = 0, even = false, length = value.length;
  406. for (var n = length - 1; n >= 0; n--) {
  407. digit = parseInt(value.charAt(n), 10);
  408. if (even) {
  409. if ((digit *= 2) > 9) {
  410. digit -= 9;
  411. }
  412. }
  413. check += digit;
  414. even = !even;
  415. }
  416. return (check % 10) == 0;
  417. }
  418. };
  419. }(window.jQuery));
  420. ;(function($) {
  421. $.fn.bootstrapValidator.validators.different = {
  422. /**
  423. * Return true if the input value is different with given field's value
  424. *
  425. * @param {BootstrapValidator} validator The validator plugin instance
  426. * @param {jQuery} $field Field element
  427. * @param {Object} options Consists of the following key:
  428. * - field: The name of field that will be used to compare with current one
  429. * @returns {Boolean}
  430. */
  431. validate: function(validator, $field, options) {
  432. var value = $field.val();
  433. if (value == '') {
  434. return true;
  435. }
  436. var $compareWith = validator.getFieldElement(options.field);
  437. if ($compareWith && value != $compareWith.val()) {
  438. validator.removeError($compareWith);
  439. return true;
  440. } else {
  441. return false;
  442. }
  443. }
  444. };
  445. }(window.jQuery));
  446. ;(function($) {
  447. $.fn.bootstrapValidator.validators.digits = {
  448. /**
  449. * Return true if the input value contains digits only
  450. *
  451. * @param {BootstrapValidator} validator Validate plugin instance
  452. * @param {jQuery} $field Field element
  453. * @param {Object} options
  454. * @returns {Boolean}
  455. */
  456. validate: function(validator, $field, options) {
  457. var value = $field.val();
  458. if (value == '') {
  459. return true;
  460. }
  461. return /^\d+$/.test(value);
  462. }
  463. }
  464. }(window.jQuery));
  465. ;(function($) {
  466. $.fn.bootstrapValidator.validators.emailAddress = {
  467. /**
  468. * Return true if and only if the input value is a valid email address
  469. *
  470. * @param {BootstrapValidator} validator Validate plugin instance
  471. * @param {jQuery} $field Field element
  472. * @param {Object} options
  473. * @returns {Boolean}
  474. */
  475. validate: function(validator, $field, options) {
  476. var value = $field.val();
  477. if (value == '') {
  478. return true;
  479. }
  480. // Email address regular expression
  481. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  482. 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,}))$/;
  483. return emailRegExp.test(value);
  484. }
  485. }
  486. }(window.jQuery));
  487. ;(function($) {
  488. $.fn.bootstrapValidator.validators.greaterThan = {
  489. /**
  490. * Return true if the input value is greater than or equals to given number
  491. *
  492. * @param {BootstrapValidator} validator Validate plugin instance
  493. * @param {jQuery} $field Field element
  494. * @param {Object} options Can consist of the following keys:
  495. * - value: The number used to compare to
  496. * - inclusive [optional]: Can be true or false. Default is true
  497. * - message: The invalid message
  498. * @returns {Boolean}
  499. */
  500. validate: function(validator, $field, options) {
  501. var value = $field.val();
  502. if (value == '') {
  503. return true;
  504. }
  505. value = parseFloat(value);
  506. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  507. }
  508. }
  509. }(window.jQuery));
  510. ;(function($) {
  511. $.fn.bootstrapValidator.validators.hexColor = {
  512. /**
  513. * Return true if the input value is a valid hex color
  514. *
  515. * @param {BootstrapValidator} validator The validator plugin instance
  516. * @param {jQuery} $field Field element
  517. * @param {Object} options Can consist of the following keys:
  518. * - message: The invalid message
  519. * @returns {Boolean}
  520. */
  521. validate: function(validator, $field, options) {
  522. var value = $field.val();
  523. if (value == '') {
  524. return true;
  525. }
  526. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  527. }
  528. };
  529. }(window.jQuery));
  530. ;(function($) {
  531. $.fn.bootstrapValidator.validators.identical = {
  532. /**
  533. * Check if input value equals to value of particular one
  534. *
  535. * @param {BootstrapValidator} validator The validator plugin instance
  536. * @param {jQuery} $field Field element
  537. * @param {Object} options Consists of the following key:
  538. * - field: The name of field that will be used to compare with current one
  539. * @returns {Boolean}
  540. */
  541. validate: function(validator, $field, options) {
  542. var value = $field.val();
  543. if (value == '') {
  544. return true;
  545. }
  546. var $compareWith = validator.getFieldElement(options.field);
  547. if ($compareWith && value == $compareWith.val()) {
  548. validator.removeError($compareWith);
  549. return true;
  550. } else {
  551. return false;
  552. }
  553. }
  554. };
  555. }(window.jQuery));
  556. ;(function($) {
  557. $.fn.bootstrapValidator.validators.lessThan = {
  558. /**
  559. * Return true if the input value is less than or equal to given number
  560. *
  561. * @param {BootstrapValidator} validator The validator plugin instance
  562. * @param {jQuery} $field Field element
  563. * @param {Object} options Can consist of the following keys:
  564. * - value: The number used to compare to
  565. * - inclusive [optional]: Can be true or false. Default is true
  566. * - message: The invalid message
  567. * @returns {Boolean}
  568. */
  569. validate: function(validator, $field, options) {
  570. var value = $field.val();
  571. if (value == '') {
  572. return true;
  573. }
  574. value = parseFloat(value);
  575. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  576. }
  577. };
  578. }(window.jQuery));
  579. ;(function($) {
  580. $.fn.bootstrapValidator.validators.notEmpty = {
  581. /**
  582. * Check if input value is empty or not
  583. *
  584. * @param {BootstrapValidator} validator The validator plugin instance
  585. * @param {jQuery} $field Field element
  586. * @param {Object} options
  587. * @returns {Boolean}
  588. */
  589. validate: function(validator, $field, options) {
  590. var type = $field.attr('type');
  591. return ('checkbox' == type || 'radio' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
  592. }
  593. };
  594. }(window.jQuery));
  595. ;(function($) {
  596. $.fn.bootstrapValidator.validators.regexp = {
  597. /**
  598. * Check if the element value matches given regular expression
  599. *
  600. * @param {BootstrapValidator} validator The validator plugin instance
  601. * @param {jQuery} $field Field element
  602. * @param {Object} options Consists of the following key:
  603. * - regexp: The regular expression you need to check
  604. * @returns {Boolean}
  605. */
  606. validate: function(validator, $field, options) {
  607. var value = $field.val();
  608. if (value == '') {
  609. return true;
  610. }
  611. return options.regexp.test(value);
  612. }
  613. };
  614. }(window.jQuery));
  615. ;(function($) {
  616. $.fn.bootstrapValidator.validators.remote = {
  617. /**
  618. * Request a remote server to check the input value
  619. *
  620. * @param {BootstrapValidator} validator Plugin instance
  621. * @param {jQuery} $field Field element
  622. * @param {Object} options Can consist of the following keys:
  623. * - url
  624. * - data [optional]: By default, it will take the value
  625. * {
  626. * <fieldName>: <fieldValue>
  627. * }
  628. * - message: The invalid message
  629. * @returns {Boolean|String}
  630. */
  631. validate: function(validator, $field, options) {
  632. var value = $field.val();
  633. if (value == '') {
  634. return true;
  635. }
  636. var name = $field.attr('name'), data = options.data;
  637. if (data == null) {
  638. data = {};
  639. }
  640. data[name] = value;
  641. var xhr = $.ajax({
  642. type: 'POST',
  643. url: options.url,
  644. dataType: 'json',
  645. data: data
  646. }).success(function(response) {
  647. var isValid = response.valid === true || response.valid === 'true';
  648. validator.completeRequest($field, 'remote', isValid);
  649. });
  650. validator.startRequest($field, 'remote', xhr);
  651. return 'pending';
  652. }
  653. };
  654. }(window.jQuery));
  655. ;(function($) {
  656. $.fn.bootstrapValidator.validators.stringLength = {
  657. /**
  658. * Check if the length of element value is less or more than given number
  659. *
  660. * @param {BootstrapValidator} validator The validator plugin instance
  661. * @param {jQuery} $field Field element
  662. * @param {Object} options Consists of following keys:
  663. * - min
  664. * - max
  665. * At least one of two keys is required
  666. * @returns {Boolean}
  667. */
  668. validate: function(validator, $field, options) {
  669. var value = $field.val();
  670. if (value == '') {
  671. return true;
  672. }
  673. var length = $.trim(value).length;
  674. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  675. return false;
  676. }
  677. return true;
  678. }
  679. };
  680. }(window.jQuery));
  681. ;(function($) {
  682. $.fn.bootstrapValidator.validators.uri = {
  683. /**
  684. * Return true if the input value is a valid URL
  685. *
  686. * @param {BootstrapValidator} validator The validator plugin instance
  687. * @param {jQuery} $field Field element
  688. * @param {Object} options
  689. * @returns {Boolean}
  690. */
  691. validate: function(validator, $field, options) {
  692. var value = $field.val();
  693. if (value == '') {
  694. return true;
  695. }
  696. // Credit to https://gist.github.com/dperini/729294
  697. //
  698. // Regular Expression for URL validation
  699. //
  700. // Author: Diego Perini
  701. // Updated: 2010/12/05
  702. //
  703. // the regular expression composed & commented
  704. // could be easily tweaked for RFC compliance,
  705. // it was expressly modified to fit & satisfy
  706. // these test for an URL shortener:
  707. //
  708. // http://mathiasbynens.be/demo/url-regex
  709. //
  710. // Notes on possible differences from a standard/generic validation:
  711. //
  712. // - utf-8 char class take in consideration the full Unicode range
  713. // - TLDs have been made mandatory so single names like "localhost" fails
  714. // - protocols have been restricted to ftp, http and https only as requested
  715. //
  716. // Changes:
  717. //
  718. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  719. // first and last IP address of each class is considered invalid
  720. // (since they are broadcast/network addresses)
  721. //
  722. // - Added exclusion of private, reserved and/or local networks ranges
  723. //
  724. // Compressed one-line versions:
  725. //
  726. // Javascript version
  727. //
  728. // /^(?:(?: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
  729. //
  730. // PHP version
  731. //
  732. // _^(?:(?: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
  733. var urlExp = new RegExp(
  734. "^" +
  735. // protocol identifier
  736. "(?:(?:https?|ftp)://)" +
  737. // user:pass authentication
  738. "(?:\\S+(?::\\S*)?@)?" +
  739. "(?:" +
  740. // IP address exclusion
  741. // private & local networks
  742. "(?!10(?:\\.\\d{1,3}){3})" +
  743. "(?!127(?:\\.\\d{1,3}){3})" +
  744. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  745. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  746. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  747. // IP address dotted notation octets
  748. // excludes loopback network 0.0.0.0
  749. // excludes reserved space >= 224.0.0.0
  750. // excludes network & broacast addresses
  751. // (first & last IP address of each class)
  752. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  753. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  754. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  755. "|" +
  756. // host name
  757. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  758. // domain name
  759. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  760. // TLD identifier
  761. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  762. ")" +
  763. // port number
  764. "(?::\\d{2,5})?" +
  765. // resource path
  766. "(?:/[^\\s]*)?" +
  767. "$", "i"
  768. );
  769. return urlExp.test(value);
  770. }
  771. };
  772. }(window.jQuery));
  773. ;(function($) {
  774. $.fn.bootstrapValidator.validators.usZipCode = {
  775. /**
  776. * Return true if and only if the input value is a valid US zip code
  777. *
  778. * @param {BootstrapValidator} validator The validator plugin instance
  779. * @param {jQuery} $field Field element
  780. * @param {Object} options
  781. * @returns {Boolean}
  782. */
  783. validate: function(validateInstance, $field, options) {
  784. var value = $field.val();
  785. if (value == '') {
  786. return true;
  787. }
  788. return /^\d{5}([\-]\d{4})?$/.test(value);
  789. }
  790. };
  791. }(window.jQuery));