bootstrapValidator.js 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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
  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} [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} [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. $rowErrors = $parent.find('.help-block[data-bv-validator]'),
  453. $errors = $message.find('.help-block[data-bv-validator]'),
  454. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  455. // Update status
  456. if (validatorName) {
  457. $field.data('bv.result.' + validatorName, status);
  458. } else {
  459. for (var v in this.options.fields[field].validators) {
  460. $field.data('bv.result.' + v, status);
  461. }
  462. }
  463. // Show/hide error elements and feedback icons
  464. switch (status) {
  465. case this.STATUS_VALIDATING:
  466. this.disableSubmitButtons(true);
  467. $parent.removeClass('has-success').removeClass('has-error');
  468. // TODO: Show validating message
  469. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  470. if ($icon) {
  471. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  472. }
  473. break;
  474. case this.STATUS_INVALID:
  475. this.disableSubmitButtons(true);
  476. $parent.removeClass('has-success').addClass('has-error');
  477. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  478. if ($icon) {
  479. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  480. }
  481. break;
  482. case this.STATUS_VALID:
  483. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  484. // If the field is valid (passes all validators)
  485. var validField = ($errors.filter(function() {
  486. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  487. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  488. }).length == 0);
  489. this.disableSubmitButtons(validField ? false : true);
  490. if ($icon) {
  491. $icon
  492. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  493. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  494. .show();
  495. }
  496. // Check if all fields in the same row are valid
  497. var validRow = ($rowErrors.filter(function() {
  498. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  499. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  500. }).length == 0);
  501. $parent.removeClass('has-error has-success').addClass(validRow ? 'has-success' : 'has-error');
  502. break;
  503. case this.STATUS_NOT_VALIDATED:
  504. default:
  505. this.disableSubmitButtons(false);
  506. $parent.removeClass('has-success').removeClass('has-error');
  507. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  508. if ($icon) {
  509. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  510. }
  511. break;
  512. }
  513. return this;
  514. },
  515. /**
  516. * Check the form validity
  517. *
  518. * @returns {Boolean}
  519. */
  520. isValid: function() {
  521. var fields, field, $field,
  522. type, status, validatorName,
  523. n, i;
  524. for (field in this.options.fields) {
  525. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  526. continue;
  527. }
  528. fields = this.getFieldElements(field);
  529. type = fields.attr('type');
  530. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  531. for (i = 0; i < n; i++) {
  532. $field = $(fields[i]);
  533. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  534. continue;
  535. }
  536. for (validatorName in this.options.fields[field].validators) {
  537. status = $field.data('bv.result.' + validatorName);
  538. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  539. return false;
  540. }
  541. if (status == this.STATUS_INVALID) {
  542. this.$invalidField = $field;
  543. return false;
  544. }
  545. }
  546. }
  547. }
  548. return true;
  549. },
  550. // Useful APIs which aren't used internally
  551. /**
  552. * Reset the form
  553. *
  554. * @param {Boolean} resetFormData Reset current form data
  555. * @return {BootstrapValidator}
  556. */
  557. resetForm: function(resetFormData) {
  558. var field, fields, total, type, validator;
  559. for (field in this.options.fields) {
  560. fields = this.getFieldElements(field);
  561. total = fields.length;
  562. for (var i = 0; i < total; i++) {
  563. for (validator in this.options.fields[field].validators) {
  564. $(fields[i]).removeData('bv.dfs.' + validator);
  565. }
  566. }
  567. // Mark field as not validated yet
  568. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  569. if (resetFormData) {
  570. type = fields.attr('type');
  571. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  572. }
  573. }
  574. this.$invalidField = null;
  575. this.$submitButton = null;
  576. // Enable submit buttons
  577. this.disableSubmitButtons(false);
  578. return this;
  579. },
  580. /**
  581. * Enable/Disable all validators to given field
  582. *
  583. * @param {String} field The field name
  584. * @param {Boolean} enabled Enable/Disable field validators
  585. * @return {BootstrapValidator}
  586. */
  587. enableFieldValidators: function(field, enabled) {
  588. this.options.fields[field]['enabled'] = enabled;
  589. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  590. return this;
  591. }
  592. };
  593. // Plugin definition
  594. $.fn.bootstrapValidator = function(options) {
  595. return this.each(function() {
  596. var $this = $(this), data = $this.data('bootstrapValidator');
  597. if (!data) {
  598. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  599. }
  600. if ('string' == typeof options) {
  601. data[options]();
  602. }
  603. });
  604. };
  605. // Available validators
  606. $.fn.bootstrapValidator.validators = {};
  607. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  608. }(window.jQuery));
  609. ;(function($) {
  610. $.fn.bootstrapValidator.validators.base64 = {
  611. /**
  612. * Return true if the input value is a base 64 encoded string.
  613. *
  614. * @param {BootstrapValidator} validator The validator plugin instance
  615. * @param {jQuery} $field Field element
  616. * @param {Object} options Can consist of the following keys:
  617. * - message: The invalid message
  618. * @returns {Boolean}
  619. */
  620. validate: function(validator, $field, options) {
  621. var value = $field.val();
  622. if (value == '') {
  623. return true;
  624. }
  625. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  626. }
  627. };
  628. }(window.jQuery));
  629. ;(function($) {
  630. $.fn.bootstrapValidator.validators.between = {
  631. html5Attributes: {
  632. message: 'message',
  633. min: 'min',
  634. max: 'max',
  635. inclusive: 'inclusive'
  636. },
  637. enableByHtml5: function($field) {
  638. if ('range' == $field.attr('type')) {
  639. return {
  640. min: $field.attr('min'),
  641. max: $field.attr('max')
  642. };
  643. }
  644. return false;
  645. },
  646. /**
  647. * Return true if the input value is between (strictly or not) two given numbers
  648. *
  649. * @param {BootstrapValidator} validator The validator plugin instance
  650. * @param {jQuery} $field Field element
  651. * @param {Object} options Can consist of the following keys:
  652. * - min
  653. * - max
  654. * - inclusive [optional]: Can be true or false. Default is true
  655. * - message: The invalid message
  656. * @returns {Boolean}
  657. */
  658. validate: function(validator, $field, options) {
  659. var value = $field.val();
  660. if (value == '') {
  661. return true;
  662. }
  663. value = parseFloat(value);
  664. return (options.inclusive === true)
  665. ? (value > options.min && value < options.max)
  666. : (value >= options.min && value <= options.max);
  667. }
  668. };
  669. }(window.jQuery));
  670. ;(function($) {
  671. $.fn.bootstrapValidator.validators.callback = {
  672. /**
  673. * Return result from the callback method
  674. *
  675. * @param {BootstrapValidator} validator The validator plugin instance
  676. * @param {jQuery} $field Field element
  677. * @param {Object} options Can consist of the following keys:
  678. * - callback: The callback method that passes 2 parameters:
  679. * callback: function(fieldValue, validator) {
  680. * // fieldValue is the value of field
  681. * // validator is instance of BootstrapValidator
  682. * }
  683. * - message: The invalid message
  684. * @returns {Boolean|Deferred}
  685. */
  686. validate: function(validator, $field, options) {
  687. var value = $field.val();
  688. if (options.callback && 'function' == typeof options.callback) {
  689. var dfd = new $.Deferred();
  690. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  691. return dfd;
  692. }
  693. return true;
  694. }
  695. };
  696. }(window.jQuery));
  697. ;(function($) {
  698. $.fn.bootstrapValidator.validators.choice = {
  699. html5Attributes: {
  700. message: 'message',
  701. min: 'min',
  702. max: 'max'
  703. },
  704. /**
  705. * Check if the number of checked boxes are less or more than a given number
  706. *
  707. * @param {BootstrapValidator} validator The validator plugin instance
  708. * @param {jQuery} $field Field element
  709. * @param {Object} options Consists of following keys:
  710. * - min
  711. * - max
  712. * At least one of two keys is required
  713. * - message: The invalid message
  714. * @returns {Boolean}
  715. */
  716. validate: function(validator, $field, options) {
  717. var numChoices = validator
  718. .getFieldElements($field.attr('data-bv-field'))
  719. .filter(':checked')
  720. .length;
  721. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  722. return false;
  723. }
  724. return true;
  725. }
  726. };
  727. }(window.jQuery));
  728. ;(function($) {
  729. $.fn.bootstrapValidator.validators.creditCard = {
  730. /**
  731. * Return true if the input value is valid credit card number
  732. * Based on https://gist.github.com/DiegoSalazar/4075533
  733. *
  734. * @param {BootstrapValidator} validator The validator plugin instance
  735. * @param {jQuery} $field Field element
  736. * @param {Object} options Can consist of the following key:
  737. * - message: The invalid message
  738. * @returns {Boolean}
  739. */
  740. validate: function(validator, $field, options) {
  741. var value = $field.val();
  742. if (value == '') {
  743. return true;
  744. }
  745. // Accept only digits, dashes or spaces
  746. if (/[^0-9-\s]+/.test(value)) {
  747. return false;
  748. }
  749. value = value.replace(/\D/g, '');
  750. // Validate the check sum
  751. // The Luhn Algorithm
  752. // http://en.wikipedia.org/wiki/Luhn
  753. var check = 0, digit = 0, even = false, length = value.length;
  754. for (var n = length - 1; n >= 0; n--) {
  755. digit = parseInt(value.charAt(n), 10);
  756. if (even) {
  757. if ((digit *= 2) > 9) {
  758. digit -= 9;
  759. }
  760. }
  761. check += digit;
  762. even = !even;
  763. }
  764. if ((check % 10) != 0) {
  765. return false;
  766. }
  767. // Validate the card number based on prefix (IIN ranges) and length
  768. var cards = {
  769. AMERICAN_EXPRESS: {
  770. length: [15],
  771. prefix: ['34', '37']
  772. },
  773. DINERS_CLUB: {
  774. length: [14],
  775. prefix: ['300', '301', '302', '303', '304', '305', '36']
  776. },
  777. DINERS_CLUB_US: {
  778. length: [16],
  779. prefix: ['54', '55']
  780. },
  781. DISCOVER: {
  782. length: [16],
  783. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  784. '62214', '62215', '62216', '62217', '62218', '62219',
  785. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  786. '62290', '62291', '622920', '622921', '622922', '622923',
  787. '622924', '622925', '644', '645', '646', '647', '648',
  788. '649', '65']
  789. },
  790. JCB: {
  791. length: [16],
  792. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  793. },
  794. LASER: {
  795. length: [16, 17, 18, 19],
  796. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  797. },
  798. MAESTRO: {
  799. length: [12, 13, 14, 15, 16, 17, 18, 19],
  800. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  801. },
  802. MASTERCARD: {
  803. length: [16],
  804. prefix: ['51', '52', '53', '54', '55']
  805. },
  806. SOLO: {
  807. length: [16, 18, 19],
  808. prefix: ['6334', '6767']
  809. },
  810. UNIONPAY: {
  811. length: [16, 17, 18, 19],
  812. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  813. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  814. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  815. '622920', '622921', '622922', '622923', '622924', '622925']
  816. },
  817. VISA: {
  818. length: [16],
  819. prefix: ['4']
  820. }
  821. };
  822. var type, i;
  823. for (type in cards) {
  824. for (i in cards[type]['prefix']) {
  825. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  826. && cards[type]['length'].indexOf(value.length) != -1) // and length
  827. {
  828. return true;
  829. }
  830. }
  831. }
  832. return false;
  833. }
  834. };
  835. }(window.jQuery));
  836. ;(function($) {
  837. $.fn.bootstrapValidator.validators.cvv = {
  838. html5Attributes: {
  839. message: 'message',
  840. ccfield: 'creditCardField'
  841. },
  842. /**
  843. * Return true if the input value is a valid CVV number.
  844. *
  845. * @param {BootstrapValidator} validator The validator plugin instance
  846. * @param {jQuery} $field Field element
  847. * @param {Object} options Can consist of the following keys:
  848. * - creditCardField: The credit card number field. It can be null
  849. * - message: The invalid message
  850. * @returns {Boolean}
  851. */
  852. validate: function(validator, $field, options) {
  853. var value = $field.val();
  854. if (value == '') {
  855. return true;
  856. }
  857. if (!/^[0-9]{3,4}$/.test(value)) {
  858. return false;
  859. }
  860. if (!options.creditCardField) {
  861. return true;
  862. }
  863. // Get the credit card number
  864. var creditCard = validator.getFieldElements(options.creditCardField).val();
  865. if (creditCard == '') {
  866. return true;
  867. }
  868. // Supported credit card types
  869. var cards = {
  870. AMERICAN_EXPRESS: {
  871. length: [15],
  872. prefix: ['34', '37']
  873. },
  874. DINERS_CLUB: {
  875. length: [14],
  876. prefix: ['300', '301', '302', '303', '304', '305', '36']
  877. },
  878. DINERS_CLUB_US: {
  879. length: [16],
  880. prefix: ['54', '55']
  881. },
  882. DISCOVER: {
  883. length: [16],
  884. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  885. '62214', '62215', '62216', '62217', '62218', '62219',
  886. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  887. '62290', '62291', '622920', '622921', '622922', '622923',
  888. '622924', '622925', '644', '645', '646', '647', '648',
  889. '649', '65']
  890. },
  891. JCB: {
  892. length: [16],
  893. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  894. },
  895. LASER: {
  896. length: [16, 17, 18, 19],
  897. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  898. },
  899. MAESTRO: {
  900. length: [12, 13, 14, 15, 16, 17, 18, 19],
  901. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  902. },
  903. MASTERCARD: {
  904. length: [16],
  905. prefix: ['51', '52', '53', '54', '55']
  906. },
  907. SOLO: {
  908. length: [16, 18, 19],
  909. prefix: ['6334', '6767']
  910. },
  911. UNIONPAY: {
  912. length: [16, 17, 18, 19],
  913. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  914. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  915. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  916. '622920', '622921', '622922', '622923', '622924', '622925']
  917. },
  918. VISA: {
  919. length: [16],
  920. prefix: ['4']
  921. }
  922. };
  923. var type, i, creditCardType = null;
  924. for (type in cards) {
  925. for (i in cards[type]['prefix']) {
  926. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  927. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  928. {
  929. creditCardType = type;
  930. break;
  931. }
  932. }
  933. }
  934. return (creditCardType == null)
  935. ? false
  936. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  937. }
  938. };
  939. }(window.jQuery));
  940. ;(function($) {
  941. $.fn.bootstrapValidator.validators.date = {
  942. html5Attributes: {
  943. message: 'message',
  944. format: 'format'
  945. },
  946. /**
  947. * Return true if the input value is valid date
  948. *
  949. * @param {BootstrapValidator} validator The validator plugin instance
  950. * @param {jQuery} $field Field element
  951. * @param {Object} options Can consist of the following keys:
  952. * - format: The date format. Default is MM/DD/YYYY
  953. * Support the following formats:
  954. * YYYY/DD/MM
  955. * YYYY/DD/MM h:m A
  956. * YYYY/MM/DD
  957. * YYYY/MM/DD h:m A
  958. *
  959. * YYYY-DD-MM
  960. * YYYY-DD-MM h:m A
  961. * YYYY-MM-DD
  962. * YYYY-MM-DD h:m A
  963. *
  964. * MM/DD/YYYY
  965. * MM/DD/YYYY h:m A
  966. * DD/MM/YYYY
  967. * DD/MM/YYYY h:m A
  968. *
  969. * MM-DD-YYYY
  970. * MM-DD-YYYY h:m A
  971. * DD-MM-YYYY
  972. * DD-MM-YYYY h:m A
  973. * - message: The invalid message
  974. * @returns {Boolean}
  975. */
  976. validate: function(validator, $field, options) {
  977. var value = $field.val();
  978. if (value == '') {
  979. return true;
  980. }
  981. // Determine the separator
  982. options.format = options.format || 'MM/DD/YYYY';
  983. var separator = (options.format.indexOf('/') != -1)
  984. ? '/'
  985. : ((options.format.indexOf('-') != -1) ? '-' : null);
  986. if (separator == null) {
  987. return false;
  988. }
  989. var month, day, year, minutes = null, hours = null, matches;
  990. switch (true) {
  991. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  992. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  993. year = matches[1]; day = matches[2]; month = matches[3];
  994. break;
  995. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  996. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  997. day = matches[1]; month = matches[2]; year = matches[3];
  998. break;
  999. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1000. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1001. year = matches[1]; month = matches[2]; day = matches[3];
  1002. break;
  1003. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1004. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1005. month = matches[1]; day = matches[2]; year = matches[3];
  1006. break;
  1007. 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'):
  1008. 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'):
  1009. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1010. break;
  1011. 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'):
  1012. 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'):
  1013. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1014. break;
  1015. 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'):
  1016. 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'):
  1017. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1018. break;
  1019. 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'):
  1020. 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'):
  1021. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1022. break;
  1023. default:
  1024. return false;
  1025. }
  1026. // Validate hours and minutes
  1027. if (hours && minutes) {
  1028. hours = parseInt(hours, 10);
  1029. minutes = parseInt(minutes, 10);
  1030. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1031. return false;
  1032. }
  1033. }
  1034. // Validate day, month, and year
  1035. day = parseInt(day, 10);
  1036. month = parseInt(month, 10);
  1037. year = parseInt(year, 10);
  1038. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1039. return false;
  1040. }
  1041. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1042. // Update the number of days in Feb of leap year
  1043. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1044. numDays[1] = 29;
  1045. }
  1046. // Check the day
  1047. return (day > 0 && day <= numDays[month - 1]);
  1048. }
  1049. };
  1050. }(window.jQuery));
  1051. ;(function($) {
  1052. $.fn.bootstrapValidator.validators.different = {
  1053. html5Attributes: {
  1054. message: 'message',
  1055. field: 'field'
  1056. },
  1057. /**
  1058. * Return true if the input value is different with given field's value
  1059. *
  1060. * @param {BootstrapValidator} validator The validator plugin instance
  1061. * @param {jQuery} $field Field element
  1062. * @param {Object} options Consists of the following key:
  1063. * - field: The name of field that will be used to compare with current one
  1064. * - message: The invalid message
  1065. * @returns {Boolean}
  1066. */
  1067. validate: function(validator, $field, options) {
  1068. var value = $field.val();
  1069. if (value == '') {
  1070. return true;
  1071. }
  1072. var compareWith = validator.getFieldElements(options.field);
  1073. if (compareWith == null) {
  1074. return true;
  1075. }
  1076. if (value != compareWith.val()) {
  1077. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1078. return true;
  1079. } else {
  1080. return false;
  1081. }
  1082. }
  1083. };
  1084. }(window.jQuery));
  1085. ;(function($) {
  1086. $.fn.bootstrapValidator.validators.digits = {
  1087. /**
  1088. * Return true if the input value contains digits only
  1089. *
  1090. * @param {BootstrapValidator} validator Validate plugin instance
  1091. * @param {jQuery} $field Field element
  1092. * @param {Object} options
  1093. * @returns {Boolean}
  1094. */
  1095. validate: function(validator, $field, options) {
  1096. var value = $field.val();
  1097. if (value == '') {
  1098. return true;
  1099. }
  1100. return /^\d+$/.test(value);
  1101. }
  1102. }
  1103. }(window.jQuery));
  1104. ;(function($) {
  1105. $.fn.bootstrapValidator.validators.emailAddress = {
  1106. enableByHtml5: function($field) {
  1107. return ('email' == $field.attr('type'));
  1108. },
  1109. /**
  1110. * Return true if and only if the input value is a valid email address
  1111. *
  1112. * @param {BootstrapValidator} validator Validate plugin instance
  1113. * @param {jQuery} $field Field element
  1114. * @param {Object} options
  1115. * @returns {Boolean}
  1116. */
  1117. validate: function(validator, $field, options) {
  1118. var value = $field.val();
  1119. if (value == '') {
  1120. return true;
  1121. }
  1122. // Email address regular expression
  1123. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1124. 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,}))$/;
  1125. return emailRegExp.test(value);
  1126. }
  1127. }
  1128. }(window.jQuery));
  1129. ;(function($) {
  1130. $.fn.bootstrapValidator.validators.greaterThan = {
  1131. html5Attributes: {
  1132. message: 'message',
  1133. value: 'value',
  1134. inclusive: 'inclusive'
  1135. },
  1136. enableByHtml5: function($field) {
  1137. var min = $field.attr('min');
  1138. if (min) {
  1139. return {
  1140. value: min
  1141. };
  1142. }
  1143. return false;
  1144. },
  1145. /**
  1146. * Return true if the input value is greater than or equals to given number
  1147. *
  1148. * @param {BootstrapValidator} validator Validate plugin instance
  1149. * @param {jQuery} $field Field element
  1150. * @param {Object} options Can consist of the following keys:
  1151. * - value: The number used to compare to
  1152. * - inclusive [optional]: Can be true or false. Default is true
  1153. * - message: The invalid message
  1154. * @returns {Boolean}
  1155. */
  1156. validate: function(validator, $field, options) {
  1157. var value = $field.val();
  1158. if (value == '') {
  1159. return true;
  1160. }
  1161. value = parseFloat(value);
  1162. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1163. }
  1164. }
  1165. }(window.jQuery));
  1166. ;(function($) {
  1167. $.fn.bootstrapValidator.validators.hex = {
  1168. /**
  1169. * Return true if and only if the input value is a valid hexadecimal number
  1170. *
  1171. * @param {BootstrapValidator} validator The validator plugin instance
  1172. * @param {jQuery} $field Field element
  1173. * @param {Object} options Consist of key:
  1174. * - message: The invalid message
  1175. * @returns {Boolean}
  1176. */
  1177. validate: function(validator, $field, options) {
  1178. var value = $field.val();
  1179. if (value == '') {
  1180. return true;
  1181. }
  1182. return /^[0-9a-fA-F]+$/.test(value);
  1183. }
  1184. };
  1185. }(window.jQuery));
  1186. ;(function($) {
  1187. $.fn.bootstrapValidator.validators.hexColor = {
  1188. enableByHtml5: function($field) {
  1189. return ('color' == $field.attr('type'));
  1190. },
  1191. /**
  1192. * Return true if the input value is a valid hex color
  1193. *
  1194. * @param {BootstrapValidator} validator The validator plugin instance
  1195. * @param {jQuery} $field Field element
  1196. * @param {Object} options Can consist of the following keys:
  1197. * - message: The invalid message
  1198. * @returns {Boolean}
  1199. */
  1200. validate: function(validator, $field, options) {
  1201. var value = $field.val();
  1202. if (value == '') {
  1203. return true;
  1204. }
  1205. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1206. }
  1207. };
  1208. }(window.jQuery));
  1209. ;(function($) {
  1210. $.fn.bootstrapValidator.validators.iban = {
  1211. html5Attributes: {
  1212. message: 'message',
  1213. country: 'country'
  1214. },
  1215. /**
  1216. * Validate an International Bank Account Number (IBAN)
  1217. * To test it, take the sample IBAN from
  1218. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1219. *
  1220. * @param {BootstrapValidator} validator The validator plugin instance
  1221. * @param {jQuery} $field Field element
  1222. * @param {Object} options Can consist of the following keys:
  1223. * - message: The invalid message
  1224. * - country: The ISO 3166-1 country code
  1225. * @returns {Boolean}
  1226. */
  1227. validate: function(validator, $field, options) {
  1228. var value = $field.val();
  1229. if (value == '') {
  1230. return true;
  1231. }
  1232. // See
  1233. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1234. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1235. var ibanRegex = {
  1236. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1237. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1238. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1239. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1240. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1241. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1242. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1243. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1244. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1245. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1246. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1247. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1248. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1249. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1250. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1251. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1252. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1253. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1254. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1255. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1256. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1257. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1258. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1259. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1260. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1261. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1262. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1263. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1264. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1265. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1266. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1267. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1268. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1269. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1270. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1271. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1272. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1273. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1274. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1275. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1276. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1277. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1278. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1279. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1280. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1281. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1282. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1283. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1284. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1285. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1286. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1287. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1288. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1289. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1290. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1291. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1292. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1293. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1294. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1295. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1296. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1297. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1298. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1299. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1300. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1301. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1302. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1303. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1304. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1305. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1306. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1307. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1308. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1309. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1310. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1311. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1312. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1313. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1314. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1315. };
  1316. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1317. var country = options.country || value.substr(0, 2);
  1318. if (!ibanRegex[country]) {
  1319. return false;
  1320. }
  1321. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1322. return false;
  1323. }
  1324. value = value.substr(4) + value.substr(0, 4);
  1325. value = value.split('').map(function(n) {
  1326. var code = n.charCodeAt(0);
  1327. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1328. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1329. ? (code - 'A'.charCodeAt(0) + 10)
  1330. : n;
  1331. }).join('');
  1332. var temp = parseInt(value.substr(0, 1), 10),
  1333. length = value.length;
  1334. for (var i = 1; i < length; ++i) {
  1335. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1336. }
  1337. return (temp == 1);
  1338. }
  1339. };
  1340. }(window.jQuery));
  1341. ;(function($) {
  1342. $.fn.bootstrapValidator.validators.identical = {
  1343. html5Attributes: {
  1344. message: 'message',
  1345. field: 'field'
  1346. },
  1347. /**
  1348. * Check if input value equals to value of particular one
  1349. *
  1350. * @param {BootstrapValidator} validator The validator plugin instance
  1351. * @param {jQuery} $field Field element
  1352. * @param {Object} options Consists of the following key:
  1353. * - field: The name of field that will be used to compare with current one
  1354. * @returns {Boolean}
  1355. */
  1356. validate: function(validator, $field, options) {
  1357. var value = $field.val();
  1358. if (value == '') {
  1359. return true;
  1360. }
  1361. var compareWith = validator.getFieldElements(options.field);
  1362. if (compareWith == null) {
  1363. return true;
  1364. }
  1365. if (value == compareWith.val()) {
  1366. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1367. return true;
  1368. } else {
  1369. return false;
  1370. }
  1371. }
  1372. };
  1373. }(window.jQuery));
  1374. ;(function($) {
  1375. $.fn.bootstrapValidator.validators.integer = {
  1376. enableByHtml5: function($field) {
  1377. return ('number' == $field.attr('type'));
  1378. },
  1379. /**
  1380. * Return true if the input value is an integer
  1381. *
  1382. * @param {BootstrapValidator} validator The validator plugin instance
  1383. * @param {jQuery} $field Field element
  1384. * @param {Object} options Can consist of the following key:
  1385. * - message: The invalid message
  1386. * @returns {Boolean}
  1387. */
  1388. validate: function(validator, $field, options) {
  1389. var value = $field.val();
  1390. if (value == '') {
  1391. return true;
  1392. }
  1393. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1394. }
  1395. };
  1396. }(window.jQuery));
  1397. ;(function($) {
  1398. $.fn.bootstrapValidator.validators.ip = {
  1399. html5Attributes: {
  1400. message: 'message',
  1401. ipv4: 'ipv4',
  1402. ipv6: 'ipv6'
  1403. },
  1404. /**
  1405. * Return true if the input value is a IP address.
  1406. *
  1407. * @param {BootstrapValidator} validator The validator plugin instance
  1408. * @param {jQuery} $field Field element
  1409. * @param {Object} options Can consist of the following keys:
  1410. * - ipv4: Enable IPv4 validator, default to true
  1411. * - ipv6: Enable IPv6 validator, default to true
  1412. * - message: The invalid message
  1413. * @returns {Boolean}
  1414. */
  1415. validate: function(validator, $field, options) {
  1416. var value = $field.val();
  1417. if (value == '') {
  1418. return true;
  1419. }
  1420. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1421. if (options.ipv4) {
  1422. 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);
  1423. } else if (options.ipv6) {
  1424. 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);
  1425. }
  1426. return false;
  1427. }
  1428. };
  1429. }(window.jQuery));
  1430. ;(function($) {
  1431. $.fn.bootstrapValidator.validators.isbn = {
  1432. /**
  1433. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1434. *
  1435. * @param {BootstrapValidator} validator The validator plugin instance
  1436. * @param {jQuery} $field Field element
  1437. * @param {Object} options Can consist of the following keys:
  1438. * - message: The invalid message
  1439. * @returns {Boolean}
  1440. */
  1441. validate: function(validator, $field, options) {
  1442. var value = $field.val();
  1443. if (value == '') {
  1444. return true;
  1445. }
  1446. // Replace all special characters except digits and X
  1447. value = value.replace(/[^\dX]/gi, '');
  1448. var chars = value.split(''),
  1449. sum = 0,
  1450. checksum;
  1451. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1452. switch (chars.length) {
  1453. // ISBN 10
  1454. case 10:
  1455. sum = 0;
  1456. for (var i = 0; i < 9; i++) {
  1457. sum += ((10 - i) * parseInt(chars[i]));
  1458. }
  1459. checksum = 11 - (sum % 11);
  1460. if (checksum == 11) {
  1461. checksum = 0;
  1462. } else if (checksum == 10) {
  1463. checksum = 'X';
  1464. }
  1465. return (checksum == chars[9]);
  1466. // ISBN 13
  1467. case 13:
  1468. sum = 0;
  1469. for (var i = 0; i < 12; i++) {
  1470. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1471. }
  1472. checksum = 10 - (sum % 10);
  1473. if (checksum == 10) {
  1474. checksum = '0';
  1475. }
  1476. return (checksum == chars[12]);
  1477. default:
  1478. return false;
  1479. }
  1480. }
  1481. };
  1482. }(window.jQuery));
  1483. ;(function($) {
  1484. $.fn.bootstrapValidator.validators.lessThan = {
  1485. html5Attributes: {
  1486. message: 'message',
  1487. value: 'value',
  1488. inclusive: 'inclusive'
  1489. },
  1490. enableByHtml5: function($field) {
  1491. var max = $field.attr('max');
  1492. if (max) {
  1493. return {
  1494. value: max
  1495. };
  1496. }
  1497. return false;
  1498. },
  1499. /**
  1500. * Return true if the input value is less than or equal to given number
  1501. *
  1502. * @param {BootstrapValidator} validator The validator plugin instance
  1503. * @param {jQuery} $field Field element
  1504. * @param {Object} options Can consist of the following keys:
  1505. * - value: The number used to compare to
  1506. * - inclusive [optional]: Can be true or false. Default is true
  1507. * - message: The invalid message
  1508. * @returns {Boolean}
  1509. */
  1510. validate: function(validator, $field, options) {
  1511. var value = $field.val();
  1512. if (value == '') {
  1513. return true;
  1514. }
  1515. value = parseFloat(value);
  1516. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1517. }
  1518. };
  1519. }(window.jQuery));
  1520. ;(function($) {
  1521. $.fn.bootstrapValidator.validators.mac = {
  1522. /**
  1523. * Return true if the input value is a MAC address.
  1524. *
  1525. * @param {BootstrapValidator} validator The validator plugin instance
  1526. * @param {jQuery} $field Field element
  1527. * @param {Object} options Can consist of the following keys:
  1528. * - message: The invalid message
  1529. * @returns {Boolean}
  1530. */
  1531. validate: function(validator, $field, options) {
  1532. var value = $field.val();
  1533. if (value == '') {
  1534. return true;
  1535. }
  1536. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1537. }
  1538. };
  1539. }(window.jQuery));
  1540. ;(function($) {
  1541. $.fn.bootstrapValidator.validators.notEmpty = {
  1542. enableByHtml5: function($field) {
  1543. var required = $field.attr('required') + '';
  1544. return ('required' == required || 'true' == required);
  1545. },
  1546. /**
  1547. * Check if input value is empty or not
  1548. *
  1549. * @param {BootstrapValidator} validator The validator plugin instance
  1550. * @param {jQuery} $field Field element
  1551. * @param {Object} options
  1552. * @returns {Boolean}
  1553. */
  1554. validate: function(validator, $field, options) {
  1555. var type = $field.attr('type');
  1556. if ('radio' == type || 'checkbox' == type) {
  1557. return validator
  1558. .getFieldElements($field.attr('data-bv-field'))
  1559. .filter(':checked')
  1560. .length > 0;
  1561. }
  1562. return $.trim($field.val()) != '';
  1563. }
  1564. };
  1565. }(window.jQuery));
  1566. ;(function($) {
  1567. $.fn.bootstrapValidator.validators.numeric = {
  1568. /**
  1569. * Validate decimal number
  1570. *
  1571. * @param {BootstrapValidator} validator The validator plugin instance
  1572. * @param {jQuery} $field Field element
  1573. * @param {Object} options Consist of key:
  1574. * - message: The invalid message
  1575. * @returns {Boolean}
  1576. */
  1577. validate: function(validator, $field, options) {
  1578. var value = $field.val();
  1579. if (value == '') {
  1580. return true;
  1581. }
  1582. return !isNaN(parseFloat(value)) && isFinite(value);
  1583. }
  1584. };
  1585. }(window.jQuery));
  1586. ;(function($) {
  1587. $.fn.bootstrapValidator.validators.phone = {
  1588. html5Attributes: {
  1589. message: 'message',
  1590. country: 'country'
  1591. },
  1592. /**
  1593. * Return true if the input value contains a valid US phone number only
  1594. *
  1595. * @param {BootstrapValidator} validator Validate plugin instance
  1596. * @param {jQuery} $field Field element
  1597. * @param {Object} options Consist of key:
  1598. * - message: The invalid message
  1599. * - country: The ISO 3166 country code
  1600. * Currently it only supports United State (US) country
  1601. * @returns {Boolean}
  1602. */
  1603. validate: function(validator, $field, options) {
  1604. var value = $field.val();
  1605. if (value == '') {
  1606. return true;
  1607. }
  1608. options.country = options.country || 'US';
  1609. switch (options.country.toUpperCase()) {
  1610. case 'US':
  1611. default:
  1612. value = value.replace(/\(|\)|\s+/g, '');
  1613. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1614. }
  1615. }
  1616. }
  1617. }(window.jQuery));
  1618. ;(function($) {
  1619. $.fn.bootstrapValidator.validators.regexp = {
  1620. html5Attributes: {
  1621. message: 'message',
  1622. regexp: 'regexp'
  1623. },
  1624. enableByHtml5: function($field) {
  1625. var pattern = $field.attr('pattern');
  1626. if (pattern) {
  1627. return {
  1628. regexp: pattern
  1629. };
  1630. }
  1631. return false;
  1632. },
  1633. /**
  1634. * Check if the element value matches given regular expression
  1635. *
  1636. * @param {BootstrapValidator} validator The validator plugin instance
  1637. * @param {jQuery} $field Field element
  1638. * @param {Object} options Consists of the following key:
  1639. * - regexp: The regular expression you need to check
  1640. * @returns {Boolean}
  1641. */
  1642. validate: function(validator, $field, options) {
  1643. var value = $field.val();
  1644. if (value == '') {
  1645. return true;
  1646. }
  1647. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1648. return regexp.test(value);
  1649. }
  1650. };
  1651. }(window.jQuery));
  1652. ;(function($) {
  1653. $.fn.bootstrapValidator.validators.remote = {
  1654. html5Attributes: {
  1655. message: 'message',
  1656. url: 'url'
  1657. },
  1658. /**
  1659. * Request a remote server to check the input value
  1660. *
  1661. * @param {BootstrapValidator} validator Plugin instance
  1662. * @param {jQuery} $field Field element
  1663. * @param {Object} options Can consist of the following keys:
  1664. * - url
  1665. * - data [optional]: By default, it will take the value
  1666. * {
  1667. * <fieldName>: <fieldValue>
  1668. * }
  1669. * - message: The invalid message
  1670. * @returns {Boolean|Deferred}
  1671. */
  1672. validate: function(validator, $field, options) {
  1673. var value = $field.val();
  1674. if (value == '') {
  1675. return true;
  1676. }
  1677. var name = $field.attr('data-bv-field'), data = options.data;
  1678. if (data == null) {
  1679. data = {};
  1680. }
  1681. // Support dynamic data
  1682. if ('function' == typeof data) {
  1683. data = data.call(this, validator);
  1684. }
  1685. data[name] = value;
  1686. var dfd = new $.Deferred();
  1687. var xhr = $.ajax({
  1688. type: 'POST',
  1689. url: options.url,
  1690. dataType: 'json',
  1691. data: data
  1692. });
  1693. xhr.then(function(response) {
  1694. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1695. });
  1696. dfd.fail(function() {
  1697. xhr.abort();
  1698. });
  1699. return dfd;
  1700. }
  1701. };
  1702. }(window.jQuery));
  1703. ;(function($) {
  1704. $.fn.bootstrapValidator.validators.step = {
  1705. html5Attributes: {
  1706. message: 'message',
  1707. base: 'baseValue',
  1708. step: 'step'
  1709. },
  1710. /**
  1711. * Return true if the input value is valid step one
  1712. *
  1713. * @param {BootstrapValidator} validator The validator plugin instance
  1714. * @param {jQuery} $field Field element
  1715. * @param {Object} options Can consist of the following keys:
  1716. * - baseValue: The base value
  1717. * - step: The step
  1718. * - message: The invalid message
  1719. * @returns {Boolean}
  1720. */
  1721. validate: function(validator, $field, options) {
  1722. var value = $field.val();
  1723. if (value == '') {
  1724. return true;
  1725. }
  1726. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1727. value = parseFloat(value);
  1728. if (isNaN(value) || !isFinite(value)) {
  1729. return false;
  1730. }
  1731. var round = function(x, precision) {
  1732. var m = Math.pow(10, precision);
  1733. x = x * m;
  1734. var sign = (x > 0) | -(x < 0),
  1735. isHalf = (x % 1 === 0.5 * sign);
  1736. if (isHalf) {
  1737. return (Math.floor(x) + (sign > 0)) / m;
  1738. } else {
  1739. return Math.round(x) / m;
  1740. }
  1741. },
  1742. floatMod = function(x, y) {
  1743. if (y == 0.0) {
  1744. return 1.0;
  1745. }
  1746. var dotX = (x + '').split('.'),
  1747. dotY = (y + '').split('.'),
  1748. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1749. return round(x - y * Math.floor(x / y), precision);
  1750. };
  1751. var mod = floatMod(value - options.baseValue, options.step);
  1752. return (mod == 0.0 || mod == options.step);
  1753. }
  1754. };
  1755. }(window.jQuery));
  1756. ;(function($) {
  1757. $.fn.bootstrapValidator.validators.stringCase = {
  1758. html5Attributes: {
  1759. message: 'message',
  1760. case: 'case'
  1761. },
  1762. /**
  1763. * Check if a string is a lower or upper case one
  1764. *
  1765. * @param {BootstrapValidator} validator The validator plugin instance
  1766. * @param {jQuery} $field Field element
  1767. * @param {Object} options Consist of key:
  1768. * - message: The invalid message
  1769. * - case: Can be 'lower' (default) or 'upper'
  1770. * @returns {Boolean}
  1771. */
  1772. validate: function(validator, $field, options) {
  1773. var value = $field.val();
  1774. if (value == '') {
  1775. return true;
  1776. }
  1777. var stringCase = (options.case || 'lower').toLowerCase();
  1778. switch (stringCase) {
  1779. case 'upper':
  1780. return value === value.toUpperCase();
  1781. case 'lower':
  1782. default:
  1783. return value === value.toLowerCase();
  1784. }
  1785. }
  1786. };
  1787. }(window.jQuery));
  1788. ;(function($) {
  1789. $.fn.bootstrapValidator.validators.stringLength = {
  1790. html5Attributes: {
  1791. message: 'message',
  1792. min: 'min',
  1793. max: 'max'
  1794. },
  1795. enableByHtml5: function($field) {
  1796. var maxLength = $field.attr('maxlength');
  1797. if (maxLength) {
  1798. return {
  1799. max: parseInt(maxLength, 10)
  1800. };
  1801. }
  1802. return false;
  1803. },
  1804. /**
  1805. * Check if the length of element value is less or more than given number
  1806. *
  1807. * @param {BootstrapValidator} validator The validator plugin instance
  1808. * @param {jQuery} $field Field element
  1809. * @param {Object} options Consists of following keys:
  1810. * - min
  1811. * - max
  1812. * At least one of two keys is required
  1813. * - message: The invalid message
  1814. * @returns {Boolean}
  1815. */
  1816. validate: function(validator, $field, options) {
  1817. var value = $field.val();
  1818. if (value == '') {
  1819. return true;
  1820. }
  1821. var length = $.trim(value).length;
  1822. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1823. return false;
  1824. }
  1825. return true;
  1826. }
  1827. };
  1828. }(window.jQuery));
  1829. ;(function($) {
  1830. $.fn.bootstrapValidator.validators.uri = {
  1831. enableByHtml5: function($field) {
  1832. return ('url' == $field.attr('type'));
  1833. },
  1834. /**
  1835. * Return true if the input value is a valid URL
  1836. *
  1837. * @param {BootstrapValidator} validator The validator plugin instance
  1838. * @param {jQuery} $field Field element
  1839. * @param {Object} options
  1840. * @returns {Boolean}
  1841. */
  1842. validate: function(validator, $field, options) {
  1843. var value = $field.val();
  1844. if (value == '') {
  1845. return true;
  1846. }
  1847. // Credit to https://gist.github.com/dperini/729294
  1848. //
  1849. // Regular Expression for URL validation
  1850. //
  1851. // Author: Diego Perini
  1852. // Updated: 2010/12/05
  1853. //
  1854. // the regular expression composed & commented
  1855. // could be easily tweaked for RFC compliance,
  1856. // it was expressly modified to fit & satisfy
  1857. // these test for an URL shortener:
  1858. //
  1859. // http://mathiasbynens.be/demo/url-regex
  1860. //
  1861. // Notes on possible differences from a standard/generic validation:
  1862. //
  1863. // - utf-8 char class take in consideration the full Unicode range
  1864. // - TLDs have been made mandatory so single names like "localhost" fails
  1865. // - protocols have been restricted to ftp, http and https only as requested
  1866. //
  1867. // Changes:
  1868. //
  1869. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1870. // first and last IP address of each class is considered invalid
  1871. // (since they are broadcast/network addresses)
  1872. //
  1873. // - Added exclusion of private, reserved and/or local networks ranges
  1874. //
  1875. // Compressed one-line versions:
  1876. //
  1877. // Javascript version
  1878. //
  1879. // /^(?:(?: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
  1880. //
  1881. // PHP version
  1882. //
  1883. // _^(?:(?: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
  1884. var urlExp = new RegExp(
  1885. "^" +
  1886. // protocol identifier
  1887. "(?:(?:https?|ftp)://)" +
  1888. // user:pass authentication
  1889. "(?:\\S+(?::\\S*)?@)?" +
  1890. "(?:" +
  1891. // IP address exclusion
  1892. // private & local networks
  1893. "(?!10(?:\\.\\d{1,3}){3})" +
  1894. "(?!127(?:\\.\\d{1,3}){3})" +
  1895. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1896. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1897. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1898. // IP address dotted notation octets
  1899. // excludes loopback network 0.0.0.0
  1900. // excludes reserved space >= 224.0.0.0
  1901. // excludes network & broacast addresses
  1902. // (first & last IP address of each class)
  1903. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1904. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1905. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1906. "|" +
  1907. // host name
  1908. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1909. // domain name
  1910. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1911. // TLD identifier
  1912. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1913. ")" +
  1914. // port number
  1915. "(?::\\d{2,5})?" +
  1916. // resource path
  1917. "(?:/[^\\s]*)?" +
  1918. "$", "i"
  1919. );
  1920. return urlExp.test(value);
  1921. }
  1922. };
  1923. }(window.jQuery));
  1924. ;(function($) {
  1925. $.fn.bootstrapValidator.validators.uuid = {
  1926. html5Attributes: {
  1927. message: 'message',
  1928. version: 'version'
  1929. },
  1930. /**
  1931. * Return true if and only if the input value is a valid UUID string
  1932. *
  1933. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  1934. * @param {BootstrapValidator} validator The validator plugin instance
  1935. * @param {jQuery} $field Field element
  1936. * @param {Object} options Consist of key:
  1937. * - message: The invalid message
  1938. * - version: Can be 3, 4, 5, null
  1939. * @returns {Boolean}
  1940. */
  1941. validate: function(validator, $field, options) {
  1942. var value = $field.val();
  1943. if (value == '') {
  1944. return true;
  1945. }
  1946. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  1947. var patterns = {
  1948. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  1949. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1950. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1951. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  1952. },
  1953. version = options.version ? (options.version + '') : 'all';
  1954. return (null == patterns[version]) ? true : patterns[version].test(value);
  1955. }
  1956. };
  1957. }(window.jQuery));
  1958. ;(function($) {
  1959. $.fn.bootstrapValidator.validators.zipCode = {
  1960. html5Attributes: {
  1961. message: 'message',
  1962. country: 'country'
  1963. },
  1964. /**
  1965. * Return true if and only if the input value is a valid country zip code
  1966. *
  1967. * @param {BootstrapValidator} validator The validator plugin instance
  1968. * @param {jQuery} $field Field element
  1969. * @param {Object} options Consist of key:
  1970. * - message: The invalid message
  1971. * - country: The ISO 3166 country code
  1972. *
  1973. * Currently it supports the following countries:
  1974. * - US (United State)
  1975. * - DK (Denmark)
  1976. * - SE (Sweden)
  1977. * @returns {Boolean}
  1978. */
  1979. validate: function(validator, $field, options) {
  1980. var value = $field.val();
  1981. if (value == '' || !options.country) {
  1982. return true;
  1983. }
  1984. options.country = options.country || 'US';
  1985. switch (options.country.toUpperCase()) {
  1986. case 'DK':
  1987. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  1988. case 'SE':
  1989. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  1990. case 'US':
  1991. default:
  1992. return /^\d{5}([\-]\d{4})?$/.test(value);
  1993. }
  1994. }
  1995. };
  1996. }(window.jQuery));