bootstrapValidator.js 98 KB

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