bootstrapValidator.js 73 KB

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