bootstrapValidator.js 97 KB

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