bootstrapValidator.js 74 KB

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