bootstrapValidator.js 29 KB

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