bootstrapValidator.js 66 KB

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