bootstrapValidator.js 74 KB

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