bootstrapValidator.js 78 KB

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