bootstrapValidator.js 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.4.3-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. // Determine the event that is fired when user change the field value
  23. // Most modern browsers supports input event except IE 7, 8.
  24. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  25. // In that case I will use the keydown event
  26. var el = document.createElement('div');
  27. this._changeEvent = ('oninput' in el) ? 'input' : 'keydown';
  28. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  29. this._submitIfValid = null;
  30. this._init();
  31. };
  32. // The default options
  33. BootstrapValidator.DEFAULT_OPTIONS = {
  34. // The form CSS class
  35. elementClass: 'bootstrap-validator-form',
  36. // Default invalid message
  37. message: 'This value is not valid',
  38. // Indicate fields which won't be validated
  39. // By default, the plugin will not validate the following kind of fields:
  40. // - disabled
  41. // - hidden
  42. // - invisible
  43. //
  44. // The setting consists of jQuery filters. Accept 3 formats:
  45. // - A string. Use a comma to separate filter
  46. // - An array. Each element is a filter
  47. // - An array. Each element can be a callback function
  48. // function($field, validator) {
  49. // $field is jQuery object representing the field element
  50. // validator is the BootstrapValidator instance
  51. // return true or false;
  52. // }
  53. //
  54. // The 3 following settings are equivalent:
  55. //
  56. // 1) ':disabled, :hidden, :not(:visible)'
  57. // 2) [':disabled', ':hidden', ':not(:visible)']
  58. // 3) [':disabled', ':hidden', function($field) {
  59. // return !$field.is(':visible');
  60. // }]
  61. excluded: [':disabled', ':hidden', ':not(:visible)'],
  62. // Shows ok/error/loading icons based on the field validity.
  63. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  64. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  65. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  66. //
  67. // Examples:
  68. // - Use Glyphicons icons:
  69. // feedbackIcons: {
  70. // valid: 'glyphicon glyphicon-ok',
  71. // invalid: 'glyphicon glyphicon-remove',
  72. // validating: 'glyphicon glyphicon-refresh'
  73. // }
  74. // - Use FontAwesome icons:
  75. // feedbackIcons: {
  76. // valid: 'fa fa-check',
  77. // invalid: 'fa fa-times',
  78. // validating: 'fa fa-refresh'
  79. // }
  80. feedbackIcons: {
  81. valid: null,
  82. invalid: null,
  83. validating: null
  84. },
  85. // The submit buttons selector
  86. // These buttons will be disabled to prevent the valid form from multiple submissions
  87. submitButtons: 'button[type="submit"]',
  88. // The custom submit handler
  89. // It will prevent the form from the default submission
  90. //
  91. // submitHandler: function(validator, form) {
  92. // - validator is the BootstrapValidator instance
  93. // - form is the jQuery object present the current form
  94. // }
  95. submitHandler: null,
  96. // Live validating option
  97. // Can be one of 3 values:
  98. // - enabled: The plugin validates fields as soon as they are changed
  99. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  100. // - submitted: The live validating is enabled after the form is submitted
  101. live: 'enabled',
  102. // Map the field name with validator rules
  103. fields: null
  104. };
  105. BootstrapValidator.prototype = {
  106. constructor: BootstrapValidator,
  107. /**
  108. * Init form
  109. */
  110. _init: function() {
  111. var that = this,
  112. options = {
  113. excluded: this.$form.attr('data-bv-excluded'),
  114. trigger: this.$form.attr('data-bv-trigger'),
  115. message: this.$form.attr('data-bv-message'),
  116. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  117. live: this.$form.attr('data-bv-live'),
  118. fields: {},
  119. feedbackIcons: {
  120. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  121. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  122. validating: this.$form.attr('data-bv-feedbackicons-validating')
  123. }
  124. },
  125. validator,
  126. v, // Validator name
  127. enabled,
  128. optionName,
  129. optionValue,
  130. html5AttrName,
  131. html5Attrs;
  132. this.$form
  133. // Disable client side validation in HTML 5
  134. .attr('novalidate', 'novalidate')
  135. .addClass(this.options.elementClass)
  136. // Disable the default submission first
  137. .on('submit.bv', function(e) {
  138. e.preventDefault();
  139. that.validate();
  140. })
  141. .on('click', this.options.submitButtons, function() {
  142. that.$submitButton = $(this);
  143. // The user just click the submit button
  144. that._submitIfValid = true;
  145. })
  146. // Find all fields which have either "name" or "data-bv-field" attribute
  147. .find('[name], [data-bv-field]').each(function() {
  148. var $field = $(this);
  149. // Don't initialize hidden input
  150. if ('hidden' == $field.attr('type')) {
  151. return;
  152. }
  153. var field = $field.attr('name') || $field.attr('data-bv-field');
  154. $field.attr('data-bv-field', field);
  155. options.fields[field] = $.extend({}, {
  156. trigger: $field.attr('data-bv-trigger'),
  157. message: $field.attr('data-bv-message'),
  158. container: $field.attr('data-bv-container'),
  159. selector: $field.attr('data-bv-selector'),
  160. validators: {}
  161. }, options.fields[field]);
  162. for (v in $.fn.bootstrapValidator.validators) {
  163. validator = $.fn.bootstrapValidator.validators[v];
  164. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  165. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  166. if ((html5Attrs && enabled != 'false')
  167. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  168. {
  169. // Try to parse the options via attributes
  170. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  171. options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
  172. for (html5AttrName in validator.html5Attributes) {
  173. optionName = validator.html5Attributes[html5AttrName];
  174. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  175. if (optionValue) {
  176. if ('true' == optionValue) {
  177. optionValue = true;
  178. } else if ('false' == optionValue) {
  179. optionValue = false;
  180. }
  181. options.fields[field]['validators'][v][optionName] = optionValue;
  182. }
  183. }
  184. }
  185. }
  186. });
  187. this.options = $.extend(true, this.options, options);
  188. if ('string' == typeof this.options.excluded) {
  189. this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
  190. // Trim the spaces
  191. return item.trim();
  192. });
  193. }
  194. for (var field in this.options.fields) {
  195. this._initField(field);
  196. }
  197. this.setLiveMode(this.options.live);
  198. },
  199. /**
  200. * Init field
  201. *
  202. * @param {String} field The field name
  203. */
  204. _initField: function(field) {
  205. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  206. return;
  207. }
  208. var fields = this.getFieldElements(field);
  209. // We don't need to validate non-existing fields
  210. if (fields == null) {
  211. delete this.options.fields[field];
  212. return;
  213. }
  214. for (var validatorName in this.options.fields[field].validators) {
  215. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  216. delete this.options.fields[field].validators[validatorName];
  217. }
  218. }
  219. var that = this,
  220. type = fields.attr('type'),
  221. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent,
  222. total = fields.length,
  223. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  224. for (var i = 0; i < total; i++) {
  225. var $field = $(fields[i]),
  226. $parent = $field.parents('.form-group'),
  227. // Allow user to indicate where the error messages are shown
  228. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  229. // Set the attribute to indicate the fields which are defined by selector
  230. if (!$field.attr('data-bv-field')) {
  231. $field.attr('data-bv-field', field);
  232. }
  233. // Whenever the user change the field value, mark it as not validated yet
  234. $field.on(event + '.update.bv', function() {
  235. // Reset the flag
  236. that._submitIfValid = false;
  237. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  238. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  239. });
  240. // Create help block elements for showing the error messages
  241. $field.data('bv.messages', $message);
  242. for (validatorName in this.options.fields[field].validators) {
  243. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  244. if (!updateAll || i == total - 1) {
  245. $('<small/>')
  246. .css('display', 'none')
  247. .attr('data-bv-validator', validatorName)
  248. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  249. .addClass('help-block')
  250. .appendTo($message);
  251. }
  252. }
  253. // Prepare the feedback icons
  254. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  255. if (this.options.feedbackIcons
  256. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  257. && (!updateAll || i == total - 1))
  258. {
  259. $parent.addClass('has-feedback');
  260. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($field);
  261. // The feedback icon does not render correctly if there is no label
  262. // https://github.com/twbs/bootstrap/issues/12873
  263. if ($parent.find('label').length == 0) {
  264. $icon.css('top', 0);
  265. }
  266. }
  267. }
  268. if (this.options.fields[field]['enabled'] == null) {
  269. this.options.fields[field]['enabled'] = true;
  270. }
  271. },
  272. /**
  273. * Get the element to place the error messages
  274. *
  275. * @param {jQuery} $field The field element
  276. * @returns {jQuery}
  277. */
  278. _getMessageContainer: function($field) {
  279. var $parent = $field.parent();
  280. if ($parent.hasClass('form-group')) {
  281. return $parent;
  282. }
  283. var cssClasses = $parent.attr('class');
  284. if (!cssClasses) {
  285. return this._getMessageContainer($parent);
  286. }
  287. cssClasses = cssClasses.split(' ');
  288. var n = cssClasses.length;
  289. for (var i = 0; i < n; i++) {
  290. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  291. return $parent;
  292. }
  293. }
  294. return this._getMessageContainer($parent);
  295. },
  296. /**
  297. * Called when all validations are completed
  298. */
  299. _submit: function() {
  300. if (!this.isValid()) {
  301. if ('submitted' == this.options.live) {
  302. this.setLiveMode('enabled');
  303. }
  304. // Focus to the first invalid field
  305. if (this.$invalidField) {
  306. this.$invalidField.focus();
  307. // Activate the tab containing the invalid field if exists
  308. var $tab = this.$invalidField.parents('.tab-pane'),
  309. tabId;
  310. if ($tab && (tabId = $tab.attr('id'))) {
  311. $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
  312. }
  313. }
  314. return;
  315. }
  316. // Call the custom submission if enabled
  317. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  318. // If you want to submit the form inside your submit handler, please call defaultSubmit() method
  319. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  320. } else {
  321. this.disableSubmitButtons(true).defaultSubmit();
  322. }
  323. },
  324. /**
  325. * Check if the field is excluded.
  326. * Returning true means that the field will not be validated
  327. *
  328. * @param {jQuery} $field The field element
  329. * @return {Boolean}
  330. */
  331. _isExcluded: function($field) {
  332. if (this.options.excluded) {
  333. for (var i in this.options.excluded) {
  334. if (('string' == typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
  335. || ('function' == typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) == true))
  336. {
  337. return true;
  338. }
  339. }
  340. }
  341. return false;
  342. },
  343. // --- Public methods ---
  344. /**
  345. * Retrieve the field elements by given name
  346. *
  347. * @param {String} field The field name
  348. * @returns {null|jQuery[]}
  349. */
  350. getFieldElements: function(field) {
  351. var fields = this.options.fields[field].selector ? $(this.options.fields[field].selector) : this.$form.find('[name="' + field + '"]');
  352. return (fields.length == 0) ? null : fields;
  353. },
  354. /**
  355. * Set live validating mode
  356. *
  357. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  358. * @returns {BootstrapValidator}
  359. */
  360. setLiveMode: function(mode) {
  361. this.options.live = mode;
  362. if ('submitted' == mode) {
  363. return this;
  364. }
  365. var that = this;
  366. for (var field in this.options.fields) {
  367. (function(f) {
  368. var fields = that.getFieldElements(f);
  369. if (fields) {
  370. var type = fields.attr('type'),
  371. total = fields.length,
  372. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  373. trigger = that.options.fields[field].trigger
  374. || that.options.trigger
  375. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
  376. events = $.map(trigger.split(' '), function(item) {
  377. return item + '.live.bv';
  378. }).join(' ');
  379. for (var i = 0; i < total; i++) {
  380. ('enabled' == mode)
  381. ? $(fields[i]).on(events, function() {
  382. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  383. })
  384. : $(fields[i]).off(events);
  385. }
  386. }
  387. })(field);
  388. }
  389. return this;
  390. },
  391. /**
  392. * Disable/enable submit buttons
  393. *
  394. * @param {Boolean} disabled Can be true or false
  395. * @returns {BootstrapValidator}
  396. */
  397. disableSubmitButtons: function(disabled) {
  398. if (!disabled) {
  399. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  400. } else if (this.options.live != 'disabled') {
  401. // Don't disable if the live validating mode is disabled
  402. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  403. }
  404. return this;
  405. },
  406. /**
  407. * Validate the form
  408. *
  409. * @return {BootstrapValidator}
  410. */
  411. validate: function() {
  412. if (!this.options.fields) {
  413. return this;
  414. }
  415. this.disableSubmitButtons(true);
  416. for (var field in this.options.fields) {
  417. this.validateField(field);
  418. }
  419. // Check if whether the submit button is clicked
  420. if (this.$submitButton) {
  421. this._submit();
  422. }
  423. return this;
  424. },
  425. /**
  426. * Validate given field
  427. *
  428. * @param {String} field The field name
  429. * @returns {BootstrapValidator}
  430. */
  431. validateField: function(field) {
  432. var fields = this.getFieldElements(field),
  433. type = fields.attr('type'),
  434. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  435. for (var i = 0; i < n; i++) {
  436. this.validateFieldElement($(fields[i]), (n == 1));
  437. }
  438. return this;
  439. },
  440. /**
  441. * Validate field element
  442. *
  443. * @param {jQuery} $field The field element
  444. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  445. * @returns {BootstrapValidator}
  446. */
  447. validateFieldElement: function($field, updateAll) {
  448. var that = this,
  449. field = $field.attr('data-bv-field'),
  450. validators = this.options.fields[field].validators,
  451. validatorName,
  452. validateResult;
  453. if (!this.options.fields[field]['enabled'] || this._isExcluded($field)) {
  454. return this;
  455. }
  456. for (validatorName in validators) {
  457. if ($field.data('bv.dfs.' + validatorName)) {
  458. $field.data('bv.dfs.' + validatorName).reject();
  459. }
  460. // Don't validate field if it is already done
  461. var result = $field.data('bv.result.' + validatorName);
  462. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  463. continue;
  464. }
  465. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  466. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  467. if ('object' == typeof validateResult) {
  468. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  469. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  470. $field.data('bv.dfs.' + validatorName, validateResult);
  471. validateResult.done(function($f, v, isValid) {
  472. // v is validator name
  473. $f.removeData('bv.dfs.' + v);
  474. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  475. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  476. if (isValid && that._submitIfValid == true) {
  477. // If a remote validator returns true and the form is ready to submit, then do it
  478. that._submit();
  479. }
  480. });
  481. } else if ('boolean' == typeof validateResult) {
  482. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  483. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  484. }
  485. }
  486. return this;
  487. },
  488. /**
  489. * Update all validating results of elements which have the same field name
  490. *
  491. * @param {String} field The field name
  492. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  493. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  494. * @return {BootstrapValidator}
  495. */
  496. updateStatus: function(field, status, validatorName) {
  497. var fields = this.getFieldElements(field),
  498. type = fields.attr('type'),
  499. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  500. for (var i = 0; i < n; i++) {
  501. this.updateElementStatus($(fields[i]), status, validatorName);
  502. }
  503. return this;
  504. },
  505. /**
  506. * Update validating result of given element
  507. *
  508. * @param {jQuery} $field The field element
  509. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  510. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  511. * @return {BootstrapValidator}
  512. */
  513. updateElementStatus: function($field, status, validatorName) {
  514. var that = this,
  515. field = $field.attr('data-bv-field'),
  516. $parent = $field.parents('.form-group'),
  517. $message = $field.data('bv.messages'),
  518. $rowErrors = $parent.find('.help-block[data-bv-validator]'),
  519. $errors = $message.find('.help-block[data-bv-validator]'),
  520. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  521. // Update status
  522. if (validatorName) {
  523. $field.data('bv.result.' + validatorName, status);
  524. } else {
  525. for (var v in this.options.fields[field].validators) {
  526. $field.data('bv.result.' + v, status);
  527. }
  528. }
  529. // Show/hide error elements and feedback icons
  530. switch (status) {
  531. case this.STATUS_VALIDATING:
  532. this.disableSubmitButtons(true);
  533. $parent.removeClass('has-success').removeClass('has-error');
  534. // TODO: Show validating message
  535. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  536. if ($icon) {
  537. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  538. }
  539. break;
  540. case this.STATUS_INVALID:
  541. this.disableSubmitButtons(true);
  542. $parent.removeClass('has-success').addClass('has-error');
  543. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  544. if ($icon) {
  545. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  546. }
  547. break;
  548. case this.STATUS_VALID:
  549. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  550. // If the field is valid (passes all validators)
  551. var validField = ($errors.filter(function() {
  552. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  553. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  554. }).length == 0);
  555. this.disableSubmitButtons(validField ? false : true);
  556. if ($icon) {
  557. $icon
  558. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  559. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  560. .show();
  561. }
  562. // Check if all fields in the same row are valid
  563. var validRow = ($rowErrors.filter(function() {
  564. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  565. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  566. }).length == 0);
  567. $parent.removeClass('has-error has-success').addClass(validRow ? 'has-success' : 'has-error');
  568. break;
  569. case this.STATUS_NOT_VALIDATED:
  570. default:
  571. this.disableSubmitButtons(false);
  572. $parent.removeClass('has-success').removeClass('has-error');
  573. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  574. if ($icon) {
  575. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  576. }
  577. break;
  578. }
  579. return this;
  580. },
  581. /**
  582. * Check the form validity
  583. *
  584. * @returns {Boolean}
  585. */
  586. isValid: function() {
  587. var fields, field, $field,
  588. type, status, validatorName,
  589. n, i;
  590. for (field in this.options.fields) {
  591. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  592. continue;
  593. }
  594. fields = this.getFieldElements(field);
  595. type = fields.attr('type');
  596. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  597. for (i = 0; i < n; i++) {
  598. $field = $(fields[i]);
  599. if (this._isExcluded($field)) {
  600. continue;
  601. }
  602. for (validatorName in this.options.fields[field].validators) {
  603. status = $field.data('bv.result.' + validatorName);
  604. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  605. return false;
  606. }
  607. if (status == this.STATUS_INVALID) {
  608. this.$invalidField = $field;
  609. return false;
  610. }
  611. }
  612. }
  613. }
  614. return true;
  615. },
  616. /**
  617. * Submit the form using default submission.
  618. * It also does not perform any validations when submitting the form
  619. *
  620. * It might be used when you want to submit the form right inside the submitHandler()
  621. */
  622. defaultSubmit: function() {
  623. this.$form.off('submit.bv').submit();
  624. },
  625. // Useful APIs which aren't used internally
  626. /**
  627. * Reset the form
  628. *
  629. * @param {Boolean} resetFormData Reset current form data
  630. * @return {BootstrapValidator}
  631. */
  632. resetForm: function(resetFormData) {
  633. var field, fields, total, type, validator;
  634. for (field in this.options.fields) {
  635. fields = this.getFieldElements(field);
  636. total = fields.length;
  637. for (var i = 0; i < total; i++) {
  638. for (validator in this.options.fields[field].validators) {
  639. $(fields[i]).removeData('bv.dfs.' + validator);
  640. }
  641. }
  642. // Mark field as not validated yet
  643. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  644. if (resetFormData) {
  645. type = fields.attr('type');
  646. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  647. }
  648. }
  649. this.$invalidField = null;
  650. this.$submitButton = null;
  651. // Enable submit buttons
  652. this.disableSubmitButtons(false);
  653. return this;
  654. },
  655. /**
  656. * Enable/Disable all validators to given field
  657. *
  658. * @param {String} field The field name
  659. * @param {Boolean} enabled Enable/Disable field validators
  660. * @return {BootstrapValidator}
  661. */
  662. enableFieldValidators: function(field, enabled) {
  663. this.options.fields[field]['enabled'] = enabled;
  664. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  665. return this;
  666. }
  667. };
  668. // Plugin definition
  669. $.fn.bootstrapValidator = function(options) {
  670. return this.each(function() {
  671. var $this = $(this), data = $this.data('bootstrapValidator');
  672. if (!data) {
  673. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  674. }
  675. if ('string' == typeof options) {
  676. data[options]();
  677. }
  678. });
  679. };
  680. // Available validators
  681. $.fn.bootstrapValidator.validators = {};
  682. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  683. }(window.jQuery));
  684. ;(function($) {
  685. $.fn.bootstrapValidator.validators.base64 = {
  686. /**
  687. * Return true if the input value is a base 64 encoded string.
  688. *
  689. * @param {BootstrapValidator} validator The validator plugin instance
  690. * @param {jQuery} $field Field element
  691. * @param {Object} options Can consist of the following keys:
  692. * - message: The invalid message
  693. * @returns {Boolean}
  694. */
  695. validate: function(validator, $field, options) {
  696. var value = $field.val();
  697. if (value == '') {
  698. return true;
  699. }
  700. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  701. }
  702. };
  703. }(window.jQuery));
  704. ;(function($) {
  705. $.fn.bootstrapValidator.validators.between = {
  706. html5Attributes: {
  707. message: 'message',
  708. min: 'min',
  709. max: 'max',
  710. inclusive: 'inclusive'
  711. },
  712. enableByHtml5: function($field) {
  713. if ('range' == $field.attr('type')) {
  714. return {
  715. min: $field.attr('min'),
  716. max: $field.attr('max')
  717. };
  718. }
  719. return false;
  720. },
  721. /**
  722. * Return true if the input value is between (strictly or not) two given numbers
  723. *
  724. * @param {BootstrapValidator} validator The validator plugin instance
  725. * @param {jQuery} $field Field element
  726. * @param {Object} options Can consist of the following keys:
  727. * - min
  728. * - max
  729. * - inclusive [optional]: Can be true or false. Default is true
  730. * - message: The invalid message
  731. * @returns {Boolean}
  732. */
  733. validate: function(validator, $field, options) {
  734. var value = $field.val();
  735. if (value == '') {
  736. return true;
  737. }
  738. value = parseFloat(value);
  739. return (options.inclusive === true)
  740. ? (value > options.min && value < options.max)
  741. : (value >= options.min && value <= options.max);
  742. }
  743. };
  744. }(window.jQuery));
  745. ;(function($) {
  746. $.fn.bootstrapValidator.validators.callback = {
  747. /**
  748. * Return result from the callback method
  749. *
  750. * @param {BootstrapValidator} validator The validator plugin instance
  751. * @param {jQuery} $field Field element
  752. * @param {Object} options Can consist of the following keys:
  753. * - callback: The callback method that passes 2 parameters:
  754. * callback: function(fieldValue, validator) {
  755. * // fieldValue is the value of field
  756. * // validator is instance of BootstrapValidator
  757. * }
  758. * - message: The invalid message
  759. * @returns {Boolean|Deferred}
  760. */
  761. validate: function(validator, $field, options) {
  762. var value = $field.val();
  763. if (options.callback && 'function' == typeof options.callback) {
  764. var dfd = new $.Deferred();
  765. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  766. return dfd;
  767. }
  768. return true;
  769. }
  770. };
  771. }(window.jQuery));
  772. ;(function($) {
  773. $.fn.bootstrapValidator.validators.choice = {
  774. html5Attributes: {
  775. message: 'message',
  776. min: 'min',
  777. max: 'max'
  778. },
  779. /**
  780. * Check if the number of checked boxes are less or more than a given number
  781. *
  782. * @param {BootstrapValidator} validator The validator plugin instance
  783. * @param {jQuery} $field Field element
  784. * @param {Object} options Consists of following keys:
  785. * - min
  786. * - max
  787. * At least one of two keys is required
  788. * - message: The invalid message
  789. * @returns {Boolean}
  790. */
  791. validate: function(validator, $field, options) {
  792. var numChoices = validator
  793. .getFieldElements($field.attr('data-bv-field'))
  794. .filter(':checked')
  795. .length;
  796. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  797. return false;
  798. }
  799. return true;
  800. }
  801. };
  802. }(window.jQuery));
  803. ;(function($) {
  804. $.fn.bootstrapValidator.validators.creditCard = {
  805. /**
  806. * Return true if the input value is valid credit card number
  807. * Based on https://gist.github.com/DiegoSalazar/4075533
  808. *
  809. * @param {BootstrapValidator} validator The validator plugin instance
  810. * @param {jQuery} $field Field element
  811. * @param {Object} options Can consist of the following key:
  812. * - message: The invalid message
  813. * @returns {Boolean}
  814. */
  815. validate: function(validator, $field, options) {
  816. var value = $field.val();
  817. if (value == '') {
  818. return true;
  819. }
  820. // Accept only digits, dashes or spaces
  821. if (/[^0-9-\s]+/.test(value)) {
  822. return false;
  823. }
  824. value = value.replace(/\D/g, '');
  825. // Validate the check sum
  826. // The Luhn Algorithm
  827. // http://en.wikipedia.org/wiki/Luhn
  828. var check = 0, digit = 0, even = false, length = value.length;
  829. for (var n = length - 1; n >= 0; n--) {
  830. digit = parseInt(value.charAt(n), 10);
  831. if (even) {
  832. if ((digit *= 2) > 9) {
  833. digit -= 9;
  834. }
  835. }
  836. check += digit;
  837. even = !even;
  838. }
  839. if ((check % 10) != 0) {
  840. return false;
  841. }
  842. // Validate the card number based on prefix (IIN ranges) and length
  843. var cards = {
  844. AMERICAN_EXPRESS: {
  845. length: [15],
  846. prefix: ['34', '37']
  847. },
  848. DINERS_CLUB: {
  849. length: [14],
  850. prefix: ['300', '301', '302', '303', '304', '305', '36']
  851. },
  852. DINERS_CLUB_US: {
  853. length: [16],
  854. prefix: ['54', '55']
  855. },
  856. DISCOVER: {
  857. length: [16],
  858. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  859. '62214', '62215', '62216', '62217', '62218', '62219',
  860. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  861. '62290', '62291', '622920', '622921', '622922', '622923',
  862. '622924', '622925', '644', '645', '646', '647', '648',
  863. '649', '65']
  864. },
  865. JCB: {
  866. length: [16],
  867. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  868. },
  869. LASER: {
  870. length: [16, 17, 18, 19],
  871. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  872. },
  873. MAESTRO: {
  874. length: [12, 13, 14, 15, 16, 17, 18, 19],
  875. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  876. },
  877. MASTERCARD: {
  878. length: [16],
  879. prefix: ['51', '52', '53', '54', '55']
  880. },
  881. SOLO: {
  882. length: [16, 18, 19],
  883. prefix: ['6334', '6767']
  884. },
  885. UNIONPAY: {
  886. length: [16, 17, 18, 19],
  887. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  888. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  889. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  890. '622920', '622921', '622922', '622923', '622924', '622925']
  891. },
  892. VISA: {
  893. length: [16],
  894. prefix: ['4']
  895. }
  896. };
  897. var type, i;
  898. for (type in cards) {
  899. for (i in cards[type]['prefix']) {
  900. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  901. && cards[type]['length'].indexOf(value.length) != -1) // and length
  902. {
  903. return true;
  904. }
  905. }
  906. }
  907. return false;
  908. }
  909. };
  910. }(window.jQuery));
  911. ;(function($) {
  912. $.fn.bootstrapValidator.validators.cvv = {
  913. html5Attributes: {
  914. message: 'message',
  915. ccfield: 'creditCardField'
  916. },
  917. /**
  918. * Return true if the input value is a valid CVV number.
  919. *
  920. * @param {BootstrapValidator} validator The validator plugin instance
  921. * @param {jQuery} $field Field element
  922. * @param {Object} options Can consist of the following keys:
  923. * - creditCardField: The credit card number field. It can be null
  924. * - message: The invalid message
  925. * @returns {Boolean}
  926. */
  927. validate: function(validator, $field, options) {
  928. var value = $field.val();
  929. if (value == '') {
  930. return true;
  931. }
  932. if (!/^[0-9]{3,4}$/.test(value)) {
  933. return false;
  934. }
  935. if (!options.creditCardField) {
  936. return true;
  937. }
  938. // Get the credit card number
  939. var creditCard = validator.getFieldElements(options.creditCardField).val();
  940. if (creditCard == '') {
  941. return true;
  942. }
  943. // Supported credit card types
  944. var cards = {
  945. AMERICAN_EXPRESS: {
  946. length: [15],
  947. prefix: ['34', '37']
  948. },
  949. DINERS_CLUB: {
  950. length: [14],
  951. prefix: ['300', '301', '302', '303', '304', '305', '36']
  952. },
  953. DINERS_CLUB_US: {
  954. length: [16],
  955. prefix: ['54', '55']
  956. },
  957. DISCOVER: {
  958. length: [16],
  959. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  960. '62214', '62215', '62216', '62217', '62218', '62219',
  961. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  962. '62290', '62291', '622920', '622921', '622922', '622923',
  963. '622924', '622925', '644', '645', '646', '647', '648',
  964. '649', '65']
  965. },
  966. JCB: {
  967. length: [16],
  968. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  969. },
  970. LASER: {
  971. length: [16, 17, 18, 19],
  972. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  973. },
  974. MAESTRO: {
  975. length: [12, 13, 14, 15, 16, 17, 18, 19],
  976. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  977. },
  978. MASTERCARD: {
  979. length: [16],
  980. prefix: ['51', '52', '53', '54', '55']
  981. },
  982. SOLO: {
  983. length: [16, 18, 19],
  984. prefix: ['6334', '6767']
  985. },
  986. UNIONPAY: {
  987. length: [16, 17, 18, 19],
  988. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  989. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  990. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  991. '622920', '622921', '622922', '622923', '622924', '622925']
  992. },
  993. VISA: {
  994. length: [16],
  995. prefix: ['4']
  996. }
  997. };
  998. var type, i, creditCardType = null;
  999. for (type in cards) {
  1000. for (i in cards[type]['prefix']) {
  1001. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  1002. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  1003. {
  1004. creditCardType = type;
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. return (creditCardType == null)
  1010. ? false
  1011. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  1012. }
  1013. };
  1014. }(window.jQuery));
  1015. ;(function($) {
  1016. $.fn.bootstrapValidator.validators.date = {
  1017. html5Attributes: {
  1018. message: 'message',
  1019. format: 'format'
  1020. },
  1021. /**
  1022. * Return true if the input value is valid date
  1023. *
  1024. * @param {BootstrapValidator} validator The validator plugin instance
  1025. * @param {jQuery} $field Field element
  1026. * @param {Object} options Can consist of the following keys:
  1027. * - format: The date format. Default is MM/DD/YYYY
  1028. * Support the following formats:
  1029. * YYYY/DD/MM
  1030. * YYYY/DD/MM h:m A
  1031. * YYYY/MM/DD
  1032. * YYYY/MM/DD h:m A
  1033. *
  1034. * YYYY-DD-MM
  1035. * YYYY-DD-MM h:m A
  1036. * YYYY-MM-DD
  1037. * YYYY-MM-DD h:m A
  1038. *
  1039. * MM/DD/YYYY
  1040. * MM/DD/YYYY h:m A
  1041. * DD/MM/YYYY
  1042. * DD/MM/YYYY h:m A
  1043. *
  1044. * MM-DD-YYYY
  1045. * MM-DD-YYYY h:m A
  1046. * DD-MM-YYYY
  1047. * DD-MM-YYYY h:m A
  1048. * - message: The invalid message
  1049. * @returns {Boolean}
  1050. */
  1051. validate: function(validator, $field, options) {
  1052. var value = $field.val();
  1053. if (value == '') {
  1054. return true;
  1055. }
  1056. // Determine the separator
  1057. options.format = options.format || 'MM/DD/YYYY';
  1058. var separator = (options.format.indexOf('/') != -1)
  1059. ? '/'
  1060. : ((options.format.indexOf('-') != -1) ? '-' : null);
  1061. if (separator == null) {
  1062. return false;
  1063. }
  1064. var month, day, year, minutes = null, hours = null, matches;
  1065. switch (true) {
  1066. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  1067. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  1068. year = matches[1]; day = matches[2]; month = matches[3];
  1069. break;
  1070. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  1071. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  1072. day = matches[1]; month = matches[2]; year = matches[3];
  1073. break;
  1074. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1075. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1076. year = matches[1]; month = matches[2]; day = matches[3];
  1077. break;
  1078. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1079. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1080. month = matches[1]; day = matches[2]; year = matches[3];
  1081. break;
  1082. 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'):
  1083. 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'):
  1084. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1085. break;
  1086. 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'):
  1087. 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'):
  1088. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1089. break;
  1090. 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'):
  1091. 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'):
  1092. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1093. break;
  1094. 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'):
  1095. 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'):
  1096. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1097. break;
  1098. default:
  1099. return false;
  1100. }
  1101. // Validate hours and minutes
  1102. if (hours && minutes) {
  1103. hours = parseInt(hours, 10);
  1104. minutes = parseInt(minutes, 10);
  1105. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1106. return false;
  1107. }
  1108. }
  1109. // Validate day, month, and year
  1110. day = parseInt(day, 10);
  1111. month = parseInt(month, 10);
  1112. year = parseInt(year, 10);
  1113. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1114. return false;
  1115. }
  1116. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1117. // Update the number of days in Feb of leap year
  1118. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1119. numDays[1] = 29;
  1120. }
  1121. // Check the day
  1122. return (day > 0 && day <= numDays[month - 1]);
  1123. }
  1124. };
  1125. }(window.jQuery));
  1126. ;(function($) {
  1127. $.fn.bootstrapValidator.validators.different = {
  1128. html5Attributes: {
  1129. message: 'message',
  1130. field: 'field'
  1131. },
  1132. /**
  1133. * Return true if the input value is different with given field's value
  1134. *
  1135. * @param {BootstrapValidator} validator The validator plugin instance
  1136. * @param {jQuery} $field Field element
  1137. * @param {Object} options Consists of the following key:
  1138. * - field: The name of field that will be used to compare with current one
  1139. * - message: The invalid message
  1140. * @returns {Boolean}
  1141. */
  1142. validate: function(validator, $field, options) {
  1143. var value = $field.val();
  1144. if (value == '') {
  1145. return true;
  1146. }
  1147. var compareWith = validator.getFieldElements(options.field);
  1148. if (compareWith == null) {
  1149. return true;
  1150. }
  1151. if (value != compareWith.val()) {
  1152. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1153. return true;
  1154. } else {
  1155. return false;
  1156. }
  1157. }
  1158. };
  1159. }(window.jQuery));
  1160. ;(function($) {
  1161. $.fn.bootstrapValidator.validators.digits = {
  1162. /**
  1163. * Return true if the input value contains digits only
  1164. *
  1165. * @param {BootstrapValidator} validator Validate plugin instance
  1166. * @param {jQuery} $field Field element
  1167. * @param {Object} options
  1168. * @returns {Boolean}
  1169. */
  1170. validate: function(validator, $field, options) {
  1171. var value = $field.val();
  1172. if (value == '') {
  1173. return true;
  1174. }
  1175. return /^\d+$/.test(value);
  1176. }
  1177. }
  1178. }(window.jQuery));
  1179. ;(function($) {
  1180. $.fn.bootstrapValidator.validators.emailAddress = {
  1181. enableByHtml5: function($field) {
  1182. return ('email' == $field.attr('type'));
  1183. },
  1184. /**
  1185. * Return true if and only if the input value is a valid email address
  1186. *
  1187. * @param {BootstrapValidator} validator Validate plugin instance
  1188. * @param {jQuery} $field Field element
  1189. * @param {Object} options
  1190. * @returns {Boolean}
  1191. */
  1192. validate: function(validator, $field, options) {
  1193. var value = $field.val();
  1194. if (value == '') {
  1195. return true;
  1196. }
  1197. // Email address regular expression
  1198. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1199. 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,}))$/;
  1200. return emailRegExp.test(value);
  1201. }
  1202. }
  1203. }(window.jQuery));
  1204. ;(function($) {
  1205. $.fn.bootstrapValidator.validators.greaterThan = {
  1206. html5Attributes: {
  1207. message: 'message',
  1208. value: 'value',
  1209. inclusive: 'inclusive'
  1210. },
  1211. enableByHtml5: function($field) {
  1212. var min = $field.attr('min');
  1213. if (min) {
  1214. return {
  1215. value: min
  1216. };
  1217. }
  1218. return false;
  1219. },
  1220. /**
  1221. * Return true if the input value is greater than or equals to given number
  1222. *
  1223. * @param {BootstrapValidator} validator Validate plugin instance
  1224. * @param {jQuery} $field Field element
  1225. * @param {Object} options Can consist of the following keys:
  1226. * - value: The number used to compare to
  1227. * - inclusive [optional]: Can be true or false. Default is true
  1228. * - message: The invalid message
  1229. * @returns {Boolean}
  1230. */
  1231. validate: function(validator, $field, options) {
  1232. var value = $field.val();
  1233. if (value == '') {
  1234. return true;
  1235. }
  1236. value = parseFloat(value);
  1237. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1238. }
  1239. }
  1240. }(window.jQuery));
  1241. ;(function($) {
  1242. $.fn.bootstrapValidator.validators.hex = {
  1243. /**
  1244. * Return true if and only if the input value is a valid hexadecimal number
  1245. *
  1246. * @param {BootstrapValidator} validator The validator plugin instance
  1247. * @param {jQuery} $field Field element
  1248. * @param {Object} options Consist of key:
  1249. * - message: The invalid message
  1250. * @returns {Boolean}
  1251. */
  1252. validate: function(validator, $field, options) {
  1253. var value = $field.val();
  1254. if (value == '') {
  1255. return true;
  1256. }
  1257. return /^[0-9a-fA-F]+$/.test(value);
  1258. }
  1259. };
  1260. }(window.jQuery));
  1261. ;(function($) {
  1262. $.fn.bootstrapValidator.validators.hexColor = {
  1263. enableByHtml5: function($field) {
  1264. return ('color' == $field.attr('type'));
  1265. },
  1266. /**
  1267. * Return true if the input value is a valid hex color
  1268. *
  1269. * @param {BootstrapValidator} validator The validator plugin instance
  1270. * @param {jQuery} $field Field element
  1271. * @param {Object} options Can consist of the following keys:
  1272. * - message: The invalid message
  1273. * @returns {Boolean}
  1274. */
  1275. validate: function(validator, $field, options) {
  1276. var value = $field.val();
  1277. if (value == '') {
  1278. return true;
  1279. }
  1280. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1281. }
  1282. };
  1283. }(window.jQuery));
  1284. ;(function($) {
  1285. $.fn.bootstrapValidator.validators.iban = {
  1286. html5Attributes: {
  1287. message: 'message',
  1288. country: 'country'
  1289. },
  1290. /**
  1291. * Validate an International Bank Account Number (IBAN)
  1292. * To test it, take the sample IBAN from
  1293. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1294. *
  1295. * @param {BootstrapValidator} validator The validator plugin instance
  1296. * @param {jQuery} $field Field element
  1297. * @param {Object} options Can consist of the following keys:
  1298. * - message: The invalid message
  1299. * - country: The ISO 3166-1 country code
  1300. * @returns {Boolean}
  1301. */
  1302. validate: function(validator, $field, options) {
  1303. var value = $field.val();
  1304. if (value == '') {
  1305. return true;
  1306. }
  1307. // See
  1308. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1309. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1310. var ibanRegex = {
  1311. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1312. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1313. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1314. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1315. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1316. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1317. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1318. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1319. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1320. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1321. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1322. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1323. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1324. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1325. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1326. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1327. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1328. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1329. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1330. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1331. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1332. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1333. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1334. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1335. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1336. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1337. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1338. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1339. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1340. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1341. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1342. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1343. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1344. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1345. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1346. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1347. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1348. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1349. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1350. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1351. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1352. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1353. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1354. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1355. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1356. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1357. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1358. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1359. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1360. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1361. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1362. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1363. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1364. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1365. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1366. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1367. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1368. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1369. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1370. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1371. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1372. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1373. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1374. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1375. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1376. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1377. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1378. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1379. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1380. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1381. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1382. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1383. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1384. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1385. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1386. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1387. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1388. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1389. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1390. };
  1391. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1392. var country = options.country || value.substr(0, 2);
  1393. if (!ibanRegex[country]) {
  1394. return false;
  1395. }
  1396. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1397. return false;
  1398. }
  1399. value = value.substr(4) + value.substr(0, 4);
  1400. value = value.split('').map(function(n) {
  1401. var code = n.charCodeAt(0);
  1402. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1403. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1404. ? (code - 'A'.charCodeAt(0) + 10)
  1405. : n;
  1406. }).join('');
  1407. var temp = parseInt(value.substr(0, 1), 10),
  1408. length = value.length;
  1409. for (var i = 1; i < length; ++i) {
  1410. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1411. }
  1412. return (temp == 1);
  1413. }
  1414. };
  1415. }(window.jQuery));
  1416. ;(function($) {
  1417. $.fn.bootstrapValidator.validators.identical = {
  1418. html5Attributes: {
  1419. message: 'message',
  1420. field: 'field'
  1421. },
  1422. /**
  1423. * Check if input value equals to value of particular one
  1424. *
  1425. * @param {BootstrapValidator} validator The validator plugin instance
  1426. * @param {jQuery} $field Field element
  1427. * @param {Object} options Consists of the following key:
  1428. * - field: The name of field that will be used to compare with current one
  1429. * @returns {Boolean}
  1430. */
  1431. validate: function(validator, $field, options) {
  1432. var value = $field.val();
  1433. if (value == '') {
  1434. return true;
  1435. }
  1436. var compareWith = validator.getFieldElements(options.field);
  1437. if (compareWith == null) {
  1438. return true;
  1439. }
  1440. if (value == compareWith.val()) {
  1441. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1442. return true;
  1443. } else {
  1444. return false;
  1445. }
  1446. }
  1447. };
  1448. }(window.jQuery));
  1449. ;(function($) {
  1450. $.fn.bootstrapValidator.validators.integer = {
  1451. enableByHtml5: function($field) {
  1452. return ('number' == $field.attr('type'));
  1453. },
  1454. /**
  1455. * Return true if the input value is an integer
  1456. *
  1457. * @param {BootstrapValidator} validator The validator plugin instance
  1458. * @param {jQuery} $field Field element
  1459. * @param {Object} options Can consist of the following key:
  1460. * - message: The invalid message
  1461. * @returns {Boolean}
  1462. */
  1463. validate: function(validator, $field, options) {
  1464. var value = $field.val();
  1465. if (value == '') {
  1466. return true;
  1467. }
  1468. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1469. }
  1470. };
  1471. }(window.jQuery));
  1472. ;(function($) {
  1473. $.fn.bootstrapValidator.validators.ip = {
  1474. html5Attributes: {
  1475. message: 'message',
  1476. ipv4: 'ipv4',
  1477. ipv6: 'ipv6'
  1478. },
  1479. /**
  1480. * Return true if the input value is a IP address.
  1481. *
  1482. * @param {BootstrapValidator} validator The validator plugin instance
  1483. * @param {jQuery} $field Field element
  1484. * @param {Object} options Can consist of the following keys:
  1485. * - ipv4: Enable IPv4 validator, default to true
  1486. * - ipv6: Enable IPv6 validator, default to true
  1487. * - message: The invalid message
  1488. * @returns {Boolean}
  1489. */
  1490. validate: function(validator, $field, options) {
  1491. var value = $field.val();
  1492. if (value == '') {
  1493. return true;
  1494. }
  1495. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1496. if (options.ipv4) {
  1497. 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);
  1498. } else if (options.ipv6) {
  1499. 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);
  1500. }
  1501. return false;
  1502. }
  1503. };
  1504. }(window.jQuery));
  1505. ;(function($) {
  1506. $.fn.bootstrapValidator.validators.isbn = {
  1507. /**
  1508. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1509. *
  1510. * @param {BootstrapValidator} validator The validator plugin instance
  1511. * @param {jQuery} $field Field element
  1512. * @param {Object} options Can consist of the following keys:
  1513. * - message: The invalid message
  1514. * @returns {Boolean}
  1515. */
  1516. validate: function(validator, $field, options) {
  1517. var value = $field.val();
  1518. if (value == '') {
  1519. return true;
  1520. }
  1521. // Replace all special characters except digits and X
  1522. value = value.replace(/[^\dX]/gi, '');
  1523. var chars = value.split(''),
  1524. sum = 0,
  1525. checksum;
  1526. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1527. switch (chars.length) {
  1528. // ISBN 10
  1529. case 10:
  1530. sum = 0;
  1531. for (var i = 0; i < 9; i++) {
  1532. sum += ((10 - i) * parseInt(chars[i]));
  1533. }
  1534. checksum = 11 - (sum % 11);
  1535. if (checksum == 11) {
  1536. checksum = 0;
  1537. } else if (checksum == 10) {
  1538. checksum = 'X';
  1539. }
  1540. return (checksum == chars[9]);
  1541. // ISBN 13
  1542. case 13:
  1543. sum = 0;
  1544. for (var i = 0; i < 12; i++) {
  1545. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1546. }
  1547. checksum = 10 - (sum % 10);
  1548. if (checksum == 10) {
  1549. checksum = '0';
  1550. }
  1551. return (checksum == chars[12]);
  1552. default:
  1553. return false;
  1554. }
  1555. }
  1556. };
  1557. }(window.jQuery));
  1558. ;(function($) {
  1559. $.fn.bootstrapValidator.validators.lessThan = {
  1560. html5Attributes: {
  1561. message: 'message',
  1562. value: 'value',
  1563. inclusive: 'inclusive'
  1564. },
  1565. enableByHtml5: function($field) {
  1566. var max = $field.attr('max');
  1567. if (max) {
  1568. return {
  1569. value: max
  1570. };
  1571. }
  1572. return false;
  1573. },
  1574. /**
  1575. * Return true if the input value is less than or equal to given number
  1576. *
  1577. * @param {BootstrapValidator} validator The validator plugin instance
  1578. * @param {jQuery} $field Field element
  1579. * @param {Object} options Can consist of the following keys:
  1580. * - value: The number used to compare to
  1581. * - inclusive [optional]: Can be true or false. Default is true
  1582. * - message: The invalid message
  1583. * @returns {Boolean}
  1584. */
  1585. validate: function(validator, $field, options) {
  1586. var value = $field.val();
  1587. if (value == '') {
  1588. return true;
  1589. }
  1590. value = parseFloat(value);
  1591. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1592. }
  1593. };
  1594. }(window.jQuery));
  1595. ;(function($) {
  1596. $.fn.bootstrapValidator.validators.mac = {
  1597. /**
  1598. * Return true if the input value is a MAC address.
  1599. *
  1600. * @param {BootstrapValidator} validator The validator plugin instance
  1601. * @param {jQuery} $field Field element
  1602. * @param {Object} options Can consist of the following keys:
  1603. * - message: The invalid message
  1604. * @returns {Boolean}
  1605. */
  1606. validate: function(validator, $field, options) {
  1607. var value = $field.val();
  1608. if (value == '') {
  1609. return true;
  1610. }
  1611. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1612. }
  1613. };
  1614. }(window.jQuery));
  1615. ;(function($) {
  1616. $.fn.bootstrapValidator.validators.notEmpty = {
  1617. enableByHtml5: function($field) {
  1618. var required = $field.attr('required') + '';
  1619. return ('required' == required || 'true' == required);
  1620. },
  1621. /**
  1622. * Check if input value is empty or not
  1623. *
  1624. * @param {BootstrapValidator} validator The validator plugin instance
  1625. * @param {jQuery} $field Field element
  1626. * @param {Object} options
  1627. * @returns {Boolean}
  1628. */
  1629. validate: function(validator, $field, options) {
  1630. var type = $field.attr('type');
  1631. if ('radio' == type || 'checkbox' == type) {
  1632. return validator
  1633. .getFieldElements($field.attr('data-bv-field'))
  1634. .filter(':checked')
  1635. .length > 0;
  1636. }
  1637. return $.trim($field.val()) != '';
  1638. }
  1639. };
  1640. }(window.jQuery));
  1641. ;(function($) {
  1642. $.fn.bootstrapValidator.validators.numeric = {
  1643. /**
  1644. * Validate decimal number
  1645. *
  1646. * @param {BootstrapValidator} validator The validator plugin instance
  1647. * @param {jQuery} $field Field element
  1648. * @param {Object} options Consist of key:
  1649. * - message: The invalid message
  1650. * @returns {Boolean}
  1651. */
  1652. validate: function(validator, $field, options) {
  1653. var value = $field.val();
  1654. if (value == '') {
  1655. return true;
  1656. }
  1657. return !isNaN(parseFloat(value)) && isFinite(value);
  1658. }
  1659. };
  1660. }(window.jQuery));
  1661. ;(function($) {
  1662. $.fn.bootstrapValidator.validators.phone = {
  1663. html5Attributes: {
  1664. message: 'message',
  1665. country: 'country'
  1666. },
  1667. /**
  1668. * Return true if the input value contains a valid US phone number only
  1669. *
  1670. * @param {BootstrapValidator} validator Validate plugin instance
  1671. * @param {jQuery} $field Field element
  1672. * @param {Object} options Consist of key:
  1673. * - message: The invalid message
  1674. * - country: The ISO 3166 country code
  1675. * Currently it only supports United State (US) country
  1676. * @returns {Boolean}
  1677. */
  1678. validate: function(validator, $field, options) {
  1679. var value = $field.val();
  1680. if (value == '') {
  1681. return true;
  1682. }
  1683. var country = (options.country || 'US').toUpperCase();
  1684. switch (country) {
  1685. case 'US':
  1686. default:
  1687. // Make sure US phone numbers have 10 digits
  1688. // May start with 1, +1, or 1-; should discard
  1689. // Area code may be delimited with (), & sections may be delimited with . or -
  1690. // Test: http://regexr.com/38mqi
  1691. value = value.replace(/\D/g, '');
  1692. return (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length == 10);
  1693. }
  1694. }
  1695. }
  1696. }(window.jQuery));
  1697. ;(function($) {
  1698. $.fn.bootstrapValidator.validators.regexp = {
  1699. html5Attributes: {
  1700. message: 'message',
  1701. regexp: 'regexp'
  1702. },
  1703. enableByHtml5: function($field) {
  1704. var pattern = $field.attr('pattern');
  1705. if (pattern) {
  1706. return {
  1707. regexp: pattern
  1708. };
  1709. }
  1710. return false;
  1711. },
  1712. /**
  1713. * Check if the element value matches given regular expression
  1714. *
  1715. * @param {BootstrapValidator} validator The validator plugin instance
  1716. * @param {jQuery} $field Field element
  1717. * @param {Object} options Consists of the following key:
  1718. * - regexp: The regular expression you need to check
  1719. * @returns {Boolean}
  1720. */
  1721. validate: function(validator, $field, options) {
  1722. var value = $field.val();
  1723. if (value == '') {
  1724. return true;
  1725. }
  1726. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1727. return regexp.test(value);
  1728. }
  1729. };
  1730. }(window.jQuery));
  1731. ;(function ($) {
  1732. $.fn.bootstrapValidator.validators.remote = {
  1733. html5Attributes: {
  1734. message: 'message',
  1735. url: 'url',
  1736. name: 'name'
  1737. },
  1738. /**
  1739. * Request a remote server to check the input value
  1740. *
  1741. * @param {BootstrapValidator} validator Plugin instance
  1742. * @param {jQuery} $field Field element
  1743. * @param {Object} options Can consist of the following keys:
  1744. * - url
  1745. * - data [optional]: By default, it will take the value
  1746. * {
  1747. * <fieldName>: <fieldValue>
  1748. * }
  1749. * - name [optional]: Override the field name for the request.
  1750. * - message: The invalid message
  1751. * @returns {Boolean|Deferred}
  1752. */
  1753. validate: function(validator, $field, options) {
  1754. var value = $field.val();
  1755. if (value == '') {
  1756. return true;
  1757. }
  1758. var name = $field.attr('data-bv-field'), data = options.data;
  1759. if (data == null) {
  1760. data = {};
  1761. }
  1762. // Support dynamic data
  1763. if ('function' == typeof data) {
  1764. data = data.call(this, validator);
  1765. }
  1766. data[options.name || name] = value;
  1767. var dfd = new $.Deferred();
  1768. var xhr = $.ajax({
  1769. type: 'POST',
  1770. url: options.url,
  1771. dataType: 'json',
  1772. data: data
  1773. });
  1774. xhr.then(function(response) {
  1775. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1776. });
  1777. dfd.fail(function() {
  1778. xhr.abort();
  1779. });
  1780. return dfd;
  1781. }
  1782. };
  1783. }(window.jQuery));
  1784. ;(function($) {
  1785. $.fn.bootstrapValidator.validators.siren = {
  1786. /**
  1787. * Check if a string is a siren number
  1788. *
  1789. * @param {BootstrapValidator} validator The validator plugin instance
  1790. * @param {jQuery} $field Field element
  1791. * @param {Object} options Consist of key:
  1792. * - message: The invalid message
  1793. * @returns {Boolean}
  1794. */
  1795. validate: function(validator, $field, options) {
  1796. var value = $field.val();
  1797. if (value == '') {
  1798. return true;
  1799. }
  1800. var sum = 0,
  1801. length = value.length,
  1802. tmp;
  1803. for (var i = 0; i < length; i++) {
  1804. tmp = parseInt(value.charAt(i));
  1805. if ((i % 2) == 1) {
  1806. tmp = tmp * 2;
  1807. if (tmp > 9) {
  1808. tmp -= 9;
  1809. }
  1810. }
  1811. sum += tmp;
  1812. }
  1813. return ((sum % 10) == 0);
  1814. }
  1815. };
  1816. }(window.jQuery));
  1817. ;(function($) {
  1818. $.fn.bootstrapValidator.validators.siret = {
  1819. /**
  1820. * Check if a string is a siret number
  1821. *
  1822. * @param {BootstrapValidator} validator The validator plugin instance
  1823. * @param {jQuery} $field Field element
  1824. * @param {Object} options Consist of key:
  1825. * - message: The invalid message
  1826. * @returns {Boolean}
  1827. */
  1828. validate: function(validator, $field, options) {
  1829. var value = $field.val();
  1830. if (value == '') {
  1831. return true;
  1832. }
  1833. var sum = 0,
  1834. length = value.length,
  1835. tmp;
  1836. for (var i = 0; i < length; i++) {
  1837. tmp = parseInt(value.charAt(i));
  1838. if ((i % 2) == 0) {
  1839. tmp = tmp * 2;
  1840. if (tmp > 9) {
  1841. tmp -= 9;
  1842. }
  1843. }
  1844. sum += tmp;
  1845. }
  1846. return ((sum % 10) == 0);
  1847. }
  1848. };
  1849. }(window.jQuery));
  1850. ;(function($) {
  1851. $.fn.bootstrapValidator.validators.step = {
  1852. html5Attributes: {
  1853. message: 'message',
  1854. base: 'baseValue',
  1855. step: 'step'
  1856. },
  1857. /**
  1858. * Return true if the input value is valid step one
  1859. *
  1860. * @param {BootstrapValidator} validator The validator plugin instance
  1861. * @param {jQuery} $field Field element
  1862. * @param {Object} options Can consist of the following keys:
  1863. * - baseValue: The base value
  1864. * - step: The step
  1865. * - message: The invalid message
  1866. * @returns {Boolean}
  1867. */
  1868. validate: function(validator, $field, options) {
  1869. var value = $field.val();
  1870. if (value == '') {
  1871. return true;
  1872. }
  1873. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1874. value = parseFloat(value);
  1875. if (isNaN(value) || !isFinite(value)) {
  1876. return false;
  1877. }
  1878. var round = function(x, precision) {
  1879. var m = Math.pow(10, precision);
  1880. x = x * m;
  1881. var sign = (x > 0) | -(x < 0),
  1882. isHalf = (x % 1 === 0.5 * sign);
  1883. if (isHalf) {
  1884. return (Math.floor(x) + (sign > 0)) / m;
  1885. } else {
  1886. return Math.round(x) / m;
  1887. }
  1888. },
  1889. floatMod = function(x, y) {
  1890. if (y == 0.0) {
  1891. return 1.0;
  1892. }
  1893. var dotX = (x + '').split('.'),
  1894. dotY = (y + '').split('.'),
  1895. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1896. return round(x - y * Math.floor(x / y), precision);
  1897. };
  1898. var mod = floatMod(value - options.baseValue, options.step);
  1899. return (mod == 0.0 || mod == options.step);
  1900. }
  1901. };
  1902. }(window.jQuery));
  1903. ;(function($) {
  1904. $.fn.bootstrapValidator.validators.stringCase = {
  1905. html5Attributes: {
  1906. message: 'message',
  1907. 'case': 'case'
  1908. },
  1909. /**
  1910. * Check if a string is a lower or upper case one
  1911. *
  1912. * @param {BootstrapValidator} validator The validator plugin instance
  1913. * @param {jQuery} $field Field element
  1914. * @param {Object} options Consist of key:
  1915. * - message: The invalid message
  1916. * - case: Can be 'lower' (default) or 'upper'
  1917. * @returns {Boolean}
  1918. */
  1919. validate: function(validator, $field, options) {
  1920. var value = $field.val();
  1921. if (value == '') {
  1922. return true;
  1923. }
  1924. var stringCase = (options['case'] || 'lower').toLowerCase();
  1925. switch (stringCase) {
  1926. case 'upper':
  1927. return value === value.toUpperCase();
  1928. case 'lower':
  1929. default:
  1930. return value === value.toLowerCase();
  1931. }
  1932. }
  1933. };
  1934. }(window.jQuery));
  1935. ;(function($) {
  1936. $.fn.bootstrapValidator.validators.stringLength = {
  1937. html5Attributes: {
  1938. message: 'message',
  1939. min: 'min',
  1940. max: 'max'
  1941. },
  1942. enableByHtml5: function($field) {
  1943. var maxLength = $field.attr('maxlength');
  1944. if (maxLength) {
  1945. return {
  1946. max: parseInt(maxLength, 10)
  1947. };
  1948. }
  1949. return false;
  1950. },
  1951. /**
  1952. * Check if the length of element value is less or more than given number
  1953. *
  1954. * @param {BootstrapValidator} validator The validator plugin instance
  1955. * @param {jQuery} $field Field element
  1956. * @param {Object} options Consists of following keys:
  1957. * - min
  1958. * - max
  1959. * At least one of two keys is required
  1960. * - message: The invalid message
  1961. * @returns {Boolean}
  1962. */
  1963. validate: function(validator, $field, options) {
  1964. var value = $field.val();
  1965. if (value == '') {
  1966. return true;
  1967. }
  1968. var length = $.trim(value).length;
  1969. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1970. return false;
  1971. }
  1972. return true;
  1973. }
  1974. };
  1975. }(window.jQuery));
  1976. ;(function($) {
  1977. $.fn.bootstrapValidator.validators.uri = {
  1978. enableByHtml5: function($field) {
  1979. return ('url' == $field.attr('type'));
  1980. },
  1981. /**
  1982. * Return true if the input value is a valid URL
  1983. *
  1984. * @param {BootstrapValidator} validator The validator plugin instance
  1985. * @param {jQuery} $field Field element
  1986. * @param {Object} options
  1987. * @returns {Boolean}
  1988. */
  1989. validate: function(validator, $field, options) {
  1990. var value = $field.val();
  1991. if (value == '') {
  1992. return true;
  1993. }
  1994. // Credit to https://gist.github.com/dperini/729294
  1995. //
  1996. // Regular Expression for URL validation
  1997. //
  1998. // Author: Diego Perini
  1999. // Updated: 2010/12/05
  2000. //
  2001. // the regular expression composed & commented
  2002. // could be easily tweaked for RFC compliance,
  2003. // it was expressly modified to fit & satisfy
  2004. // these test for an URL shortener:
  2005. //
  2006. // http://mathiasbynens.be/demo/url-regex
  2007. //
  2008. // Notes on possible differences from a standard/generic validation:
  2009. //
  2010. // - utf-8 char class take in consideration the full Unicode range
  2011. // - TLDs have been made mandatory so single names like "localhost" fails
  2012. // - protocols have been restricted to ftp, http and https only as requested
  2013. //
  2014. // Changes:
  2015. //
  2016. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  2017. // first and last IP address of each class is considered invalid
  2018. // (since they are broadcast/network addresses)
  2019. //
  2020. // - Added exclusion of private, reserved and/or local networks ranges
  2021. //
  2022. // Compressed one-line versions:
  2023. //
  2024. // Javascript version
  2025. //
  2026. // /^(?:(?: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
  2027. //
  2028. // PHP version
  2029. //
  2030. // _^(?:(?: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
  2031. var urlExp = new RegExp(
  2032. "^" +
  2033. // protocol identifier
  2034. "(?:(?:https?|ftp)://)" +
  2035. // user:pass authentication
  2036. "(?:\\S+(?::\\S*)?@)?" +
  2037. "(?:" +
  2038. // IP address exclusion
  2039. // private & local networks
  2040. "(?!10(?:\\.\\d{1,3}){3})" +
  2041. "(?!127(?:\\.\\d{1,3}){3})" +
  2042. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  2043. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  2044. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  2045. // IP address dotted notation octets
  2046. // excludes loopback network 0.0.0.0
  2047. // excludes reserved space >= 224.0.0.0
  2048. // excludes network & broacast addresses
  2049. // (first & last IP address of each class)
  2050. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  2051. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  2052. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  2053. "|" +
  2054. // host name
  2055. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  2056. // domain name
  2057. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  2058. // TLD identifier
  2059. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  2060. ")" +
  2061. // port number
  2062. "(?::\\d{2,5})?" +
  2063. // resource path
  2064. "(?:/[^\\s]*)?" +
  2065. "$", "i"
  2066. );
  2067. return urlExp.test(value);
  2068. }
  2069. };
  2070. }(window.jQuery));
  2071. ;(function($) {
  2072. $.fn.bootstrapValidator.validators.uuid = {
  2073. html5Attributes: {
  2074. message: 'message',
  2075. version: 'version'
  2076. },
  2077. /**
  2078. * Return true if and only if the input value is a valid UUID string
  2079. *
  2080. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  2081. * @param {BootstrapValidator} validator The validator plugin instance
  2082. * @param {jQuery} $field Field element
  2083. * @param {Object} options Consist of key:
  2084. * - message: The invalid message
  2085. * - version: Can be 3, 4, 5, null
  2086. * @returns {Boolean}
  2087. */
  2088. validate: function(validator, $field, options) {
  2089. var value = $field.val();
  2090. if (value == '') {
  2091. return true;
  2092. }
  2093. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  2094. var patterns = {
  2095. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  2096. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2097. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2098. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  2099. },
  2100. version = options.version ? (options.version + '') : 'all';
  2101. return (null == patterns[version]) ? true : patterns[version].test(value);
  2102. }
  2103. };
  2104. }(window.jQuery));
  2105. ;(function($) {
  2106. $.fn.bootstrapValidator.validators.vin = {
  2107. /**
  2108. * Validate an US VIN (Vehicle Identification Number)
  2109. *
  2110. * @param {BootstrapValidator} validator The validator plugin instance
  2111. * @param {jQuery} $field Field element
  2112. * @param {Object} options Consist of key:
  2113. * - message: The invalid message
  2114. * @returns {Boolean}
  2115. */
  2116. validate: function(validator, $field, options) {
  2117. var value = $field.val();
  2118. if (value == '') {
  2119. return true;
  2120. }
  2121. // Don't accept I, O, Q characters
  2122. if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
  2123. return false;
  2124. }
  2125. value = value.toUpperCase();
  2126. var chars = {
  2127. A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
  2128. J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9,
  2129. S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9,
  2130. '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
  2131. },
  2132. weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
  2133. sum = 0,
  2134. length = value.length;
  2135. for (var i = 0; i < length; i++) {
  2136. sum += chars[value.charAt(i) + ''] * weights[i];
  2137. }
  2138. var reminder = sum % 11;
  2139. if (reminder == 10) {
  2140. reminder = 'X';
  2141. }
  2142. return reminder == value.charAt(8);
  2143. }
  2144. };
  2145. }(window.jQuery));
  2146. ;(function($) {
  2147. $.fn.bootstrapValidator.validators.zipCode = {
  2148. html5Attributes: {
  2149. message: 'message',
  2150. country: 'country'
  2151. },
  2152. /**
  2153. * Return true if and only if the input value is a valid country zip code
  2154. *
  2155. * @param {BootstrapValidator} validator The validator plugin instance
  2156. * @param {jQuery} $field Field element
  2157. * @param {Object} options Consist of key:
  2158. * - message: The invalid message
  2159. * - country: The ISO 3166 country code
  2160. *
  2161. * Currently it supports the following countries:
  2162. * - US (United State)
  2163. * - CA (Canada)
  2164. * - DK (Denmark)
  2165. * - SE (Sweden)
  2166. * @returns {Boolean}
  2167. */
  2168. validate: function(validator, $field, options) {
  2169. var value = $field.val();
  2170. if (value == '' || !options.country) {
  2171. return true;
  2172. }
  2173. var country = (options.country || 'US').toUpperCase();
  2174. switch (country) {
  2175. case 'CA':
  2176. return /(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}/i.test(value);
  2177. case 'DK':
  2178. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  2179. case 'SE':
  2180. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  2181. case 'US':
  2182. default:
  2183. return /^\d{4,5}([\-]\d{4})?$/.test(value);
  2184. }
  2185. }
  2186. };
  2187. }(window.jQuery));