bootstrapValidator.js 73 KB

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