bootstrapValidator.js 75 KB

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