bootstrapValidator.js 30 KB

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