spec.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. describe('api', function() {
  2. // Override the options
  3. $.extend($.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  4. feedbackIcons: {
  5. valid: 'glyphicon glyphicon-ok',
  6. invalid: 'glyphicon glyphicon-remove',
  7. validating: 'glyphicon glyphicon-refresh'
  8. }
  9. });
  10. beforeEach(function() {
  11. $([
  12. '<div class="container">',
  13. '<form class="form-horizontal" id="apiForm">',
  14. '<div class="form-group">',
  15. '<input type="text" name="username" data-bv-notempty />',
  16. '</div>',
  17. '<div class="form-group">',
  18. '<input type="text" name="email" data-bv-emailaddress />',
  19. '</div>',
  20. '</form>',
  21. '</div>'
  22. ].join('\n')).appendTo('body');
  23. $('#apiForm').bootstrapValidator();
  24. this.bv = $('#apiForm').data('bootstrapValidator');
  25. this.$email = this.bv.getFieldElements('email');
  26. });
  27. afterEach(function() {
  28. $('#apiForm').bootstrapValidator('destroy').parent().remove();
  29. });
  30. it('call revalidateField()', function() {
  31. this.$email.val('email@domain.com');
  32. this.bv.validate();
  33. expect(this.bv.isValidField('email')).toBeTruthy();
  34. this.$email.val('invalid#email.address');
  35. this.bv.revalidateField('email');
  36. expect(this.bv.isValidField(this.$email)).toEqual(false);
  37. });
  38. it('call destroy()', function() {
  39. this.bv.destroy();
  40. expect($('#apiForm').data('bootstrapValidator')).toBeUndefined();
  41. expect($('#apiForm').find('i[data-bv-icon-for]').length).toEqual(0);
  42. expect($('#apiForm').find('.help-block[data-bv-for]').length).toEqual(0);
  43. expect($('#apiForm').find('.has-feedback').length).toEqual(0);
  44. expect($('#apiForm').find('.has-success').length).toEqual(0);
  45. expect($('#apiForm').find('.has-error').length).toEqual(0);
  46. expect($('#apiForm').find('[data-bv-field]').length).toEqual(0);
  47. });
  48. });
  49. describe('container form option', function() {
  50. beforeEach(function() {
  51. $([
  52. '<form id="containerForm" class="form-horizontal">',
  53. '<div class="form-group">',
  54. '<label class="col-lg-3 control-label">Full name</label>',
  55. '<div class="col-lg-4">',
  56. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" />',
  57. '</div>',
  58. '<div class="col-lg-4">',
  59. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  60. '</div>',
  61. '</div>',
  62. '<div id="errors"></div>',
  63. '</form>'
  64. ].join('')).appendTo('body');
  65. });
  66. afterEach(function() {
  67. $('#containerForm').bootstrapValidator('destroy').remove();
  68. });
  69. it('form container declarative', function() {
  70. $('#containerForm')
  71. .attr('data-bv-container', '#errors')
  72. .bootstrapValidator();
  73. this.bv = $('#containerForm').data('bootstrapValidator');
  74. this.$firstName = this.bv.getFieldElements('firstName');
  75. this.$lastName = this.bv.getFieldElements('lastName');
  76. expect($('#errors').find('.help-block').length).toBeGreaterThan(0);
  77. this.$firstName.val('First');
  78. this.$lastName.val('');
  79. this.bv.validate();
  80. expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toEqual(0);
  81. expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toBeGreaterThan(0);
  82. });
  83. it('form container programmatically', function() {
  84. $('#containerForm').bootstrapValidator({
  85. container: '#errors'
  86. });
  87. this.bv = $('#containerForm').data('bootstrapValidator');
  88. this.$firstName = this.bv.getFieldElements('firstName');
  89. this.$lastName = this.bv.getFieldElements('lastName');
  90. expect($('#errors').find('.help-block').length).toBeGreaterThan(0);
  91. this.$firstName.val('');
  92. this.$lastName.val('Last');
  93. this.bv.validate();
  94. expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toBeGreaterThan(0);
  95. expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toEqual(0);
  96. this.bv.resetForm();
  97. this.$firstName.val('First');
  98. this.$lastName.val('Last');
  99. this.bv.validate();
  100. expect($('#errors').find('.help-block:visible').length).toEqual(0);
  101. });
  102. });
  103. describe('container field option', function() {
  104. beforeEach(function() {
  105. $([
  106. '<form id="containerForm" class="form-horizontal">',
  107. '<div class="form-group">',
  108. '<label class="col-lg-3 control-label">Full name</label>',
  109. '<div class="col-lg-4">',
  110. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" data-bv-container="#firstNameMessage" />',
  111. '<span class="help-block" id="firstNameMessage" />',
  112. '</div>',
  113. '<div class="col-lg-4">',
  114. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  115. '<span class="help-block lastNameMessage" />',
  116. '</div>',
  117. '</div>',
  118. '</form>'
  119. ].join('')).appendTo('body');
  120. $('#containerForm').bootstrapValidator({
  121. feedbackIcons: {
  122. valid: 'glyphicon glyphicon-ok',
  123. invalid: 'glyphicon glyphicon-remove',
  124. validating: 'glyphicon glyphicon-refresh'
  125. },
  126. fields: {
  127. lastName: {
  128. container: '.lastNameMessage'
  129. }
  130. }
  131. });
  132. this.bv = $('#containerForm').data('bootstrapValidator');
  133. this.$firstName = this.bv.getFieldElements('firstName');
  134. this.$lastName = this.bv.getFieldElements('lastName');
  135. });
  136. afterEach(function() {
  137. $('#containerForm').bootstrapValidator('destroy').remove();
  138. });
  139. it('field container declarative', function() {
  140. expect($.trim($('#firstNameMessage').text())).toEqual('The first name is required');
  141. expect($.trim($('.lastNameMessage').text())).toEqual('The last name is required');
  142. });
  143. it('field container programmatically', function() {
  144. this.$firstName.val('First');
  145. this.$lastName.val('');
  146. this.bv.validate();
  147. expect($('#firstNameMessage').find('.help-block:visible').length).toEqual(0);
  148. expect($('.lastNameMessage').find('.help-block:visible').length).toBeGreaterThan(0);
  149. this.bv.resetForm();
  150. this.$firstName.val('');
  151. this.$lastName.val('Last');
  152. this.bv.validate();
  153. expect($('#firstNameMessage').find('.help-block:visible').length).toBeGreaterThan(0);
  154. expect($('.lastNameMessage').find('.help-block:visible').length).toEqual(0);
  155. });
  156. });
  157. describe('container tooltip/popover', function() {
  158. beforeEach(function() {
  159. $([
  160. '<form id="containerForm" class="form-horizontal">',
  161. '<div class="form-group">',
  162. '<label class="col-lg-3 control-label">Full name</label>',
  163. '<div class="col-lg-4">',
  164. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" />',
  165. '</div>',
  166. '<div class="col-lg-4">',
  167. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  168. '</div>',
  169. '</div>',
  170. '<div id="errors"></div>',
  171. '</form>'
  172. ].join('')).appendTo('body');
  173. });
  174. afterEach(function() {
  175. $('#containerForm').bootstrapValidator('destroy').remove();
  176. });
  177. it('container declarative', function() {
  178. $('#containerForm')
  179. .attr('data-bv-container', 'tooltip')
  180. .find('[name="lastName"]')
  181. .attr('data-bv-container', 'popover')
  182. .end()
  183. .bootstrapValidator();
  184. this.bv = $('#containerForm').data('bootstrapValidator');
  185. this.$firstName = this.bv.getFieldElements('firstName');
  186. this.$lastName = this.bv.getFieldElements('lastName');
  187. this.bv.validate();
  188. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  189. expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip');
  190. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  191. expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover');
  192. this.bv.resetForm();
  193. this.$firstName.val('First');
  194. this.$lastName.val('Last');
  195. this.bv.validate();
  196. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeUndefined();
  197. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeUndefined();
  198. });
  199. it('container programmatically', function() {
  200. $('#containerForm').bootstrapValidator({
  201. container: 'tooltip',
  202. fields: {
  203. lastName: {
  204. container: 'popover'
  205. }
  206. }
  207. });
  208. this.bv = $('#containerForm').data('bootstrapValidator');
  209. this.$firstName = this.bv.getFieldElements('firstName');
  210. this.$lastName = this.bv.getFieldElements('lastName');
  211. this.bv.validate();
  212. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  213. expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip');
  214. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  215. expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover');
  216. this.bv.resetForm();
  217. this.$firstName.val('First');
  218. this.$lastName.val('Last');
  219. this.bv.validate();
  220. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeUndefined();
  221. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeUndefined();
  222. });
  223. });
  224. describe('enable validators', function() {
  225. beforeEach(function() {
  226. $([
  227. '<form class="form-horizontal" id="enableForm">',
  228. '<div class="form-group">',
  229. '<input type="text" name="fullName" class="form-control" />',
  230. '</div>',
  231. '</form>'
  232. ].join('\n')).appendTo('body');
  233. $('#enableForm').bootstrapValidator({
  234. fields: {
  235. fullName: {
  236. validators: {
  237. notEmpty: {
  238. message: 'The full name is required and cannot be empty'
  239. },
  240. stringLength: {
  241. min: 8,
  242. max: 40,
  243. message: 'The full name must be more than 8 and less than 40 characters long'
  244. },
  245. regexp: {
  246. enabled: false,
  247. regexp: /^[a-zA-Z\s]+$/,
  248. message: 'The username can only consist of alphabetical, number, and space'
  249. }
  250. }
  251. }
  252. }
  253. });
  254. this.bv = $('#enableForm').data('bootstrapValidator');
  255. this.$fullName = this.bv.getFieldElements('fullName');
  256. });
  257. afterEach(function() {
  258. $('#enableForm').bootstrapValidator('destroy').remove();
  259. });
  260. it('enable all validators', function() {
  261. this.$fullName.val('@ $full N@m3');
  262. this.bv.validate();
  263. expect(this.bv.isValid()).toBeTruthy();
  264. this.bv.resetForm();
  265. this.$fullName.val('Contain#$@');
  266. this.bv.enableFieldValidators('fullName', true);
  267. this.bv.validate();
  268. expect(this.bv.isValidField('fullName')).toEqual(false);
  269. expect(this.bv.isValid()).toEqual(false);
  270. });
  271. it('disable all validators', function() {
  272. this.bv.resetForm();
  273. this.bv.enableFieldValidators('fullName', false);
  274. this.bv.validate();
  275. expect(this.bv.isValid()).toBeTruthy();
  276. });
  277. it('enabled option particular validator', function() {
  278. this.$fullName.val('Contain@#$');
  279. this.bv.validate();
  280. expect(this.bv.isValid()).toBeTruthy();
  281. var messages = this.bv.getMessages('fullName');
  282. expect(messages.length).toEqual(0);
  283. });
  284. it('enable particular validators', function() {
  285. // Enable stringLength validator
  286. this.bv.resetForm();
  287. this.bv.enableFieldValidators('fullName', true, 'stringLength');
  288. this.bv.enableFieldValidators('fullName', true, 'regexp');
  289. this.$fullName.val('Full@');
  290. this.bv.validate();
  291. expect(this.bv.isValid()).toEqual(false);
  292. var messages = this.bv.getMessages('fullName');
  293. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1);
  294. expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
  295. });
  296. it('disable particular validators', function() {
  297. // Disable stringLength validator
  298. this.bv.enableFieldValidators('fullName', false, 'stringLength');
  299. this.$fullName.val('Full');
  300. this.bv.validate();
  301. expect(this.bv.isValid()).toBeTruthy();
  302. var messages = this.bv.getMessages('fullName');
  303. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toEqual(-1);
  304. // Disable regexp validator
  305. this.bv.enableFieldValidators('fullName', false, 'regexp');
  306. this.$fullName.val('Special@#$');
  307. this.bv.validate();
  308. expect(this.bv.isValid()).toBeTruthy();
  309. var messages = this.bv.getMessages('fullName');
  310. expect($.inArray('The username can only consist of alphabetical, number, and space', messages)).toEqual(-1);
  311. });
  312. });
  313. var My = {
  314. NameSpace: {
  315. onEmailValid: function(e, data) {
  316. $('#msg').html('My.NameSpace.onEmailValid() called, ' + data.field + ' is valid');
  317. },
  318. onEmailInvalid: function(e, data) {
  319. $('#msg').html('My.NameSpace.onEmailInvalid() called, ' + data.field + ' is invalid');
  320. },
  321. onFormValid: function(e) {
  322. $('#msg').html('My.NameSpace.onFormValid() called, form ' + $(e.target).attr('id') + ' is valid');
  323. },
  324. onFormInvalid: function(e) {
  325. $('#msg').html('My.NameSpace.onFormInvalid() called, form ' + $(e.target).attr('id') + ' is invalid');
  326. }
  327. }
  328. };
  329. // ---
  330. // Form events
  331. // ---
  332. function onFormValid(e) {
  333. $('#msg').html('form ' + $(e.target).attr('id') + ' is valid');
  334. };
  335. function onFormInvalid(e) {
  336. $('#msg').html('form ' + $(e.target).attr('id') + ' is invalid');
  337. };
  338. describe('event form attribute callback global', function() {
  339. beforeEach(function() {
  340. $([
  341. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="onFormValid" data-bv-onerror="onFormInvalid" >',
  342. '<div id="msg"></div>',
  343. '<div class="form-group">',
  344. '<input type="text" name="email" required data-bv-emailaddress />',
  345. '</div>',
  346. '</form>'
  347. ].join('\n')).appendTo('body');
  348. $('#eventForm').bootstrapValidator();
  349. this.bv = $('#eventForm').data('bootstrapValidator');
  350. this.$email = this.bv.getFieldElements('email');
  351. });
  352. afterEach(function() {
  353. $('#eventForm').bootstrapValidator('destroy').remove();
  354. });
  355. it('call data-bv-onsuccess', function() {
  356. this.$email.val('email@domain.com');
  357. this.bv.validate();
  358. expect($('#msg').html()).toEqual('form eventForm is valid');
  359. });
  360. it('call data-bv-onerror', function() {
  361. this.$email.val('email@domain');
  362. this.bv.validate();
  363. expect($('#msg').html()).toEqual('form eventForm is invalid');
  364. });
  365. });
  366. describe('event form attribute callback namespace', function() {
  367. beforeEach(function() {
  368. $([
  369. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="My.NameSpace.onFormValid" data-bv-onerror="My.NameSpace.onFormInvalid" >',
  370. '<div id="msg"></div>',
  371. '<div class="form-group">',
  372. '<input type="text" name="email" required data-bv-emailaddress />',
  373. '</div>',
  374. '</form>'
  375. ].join('\n')).appendTo('body');
  376. $('#eventForm').bootstrapValidator();
  377. this.bv = $('#eventForm').data('bootstrapValidator');
  378. this.$email = this.bv.getFieldElements('email');
  379. });
  380. afterEach(function() {
  381. $('#eventForm').bootstrapValidator('destroy').remove();
  382. });
  383. it('call data-bv-onsuccess', function() {
  384. this.$email.val('email@domain.com');
  385. this.bv.validate();
  386. expect($('#msg').html()).toEqual('My.NameSpace.onFormValid() called, form eventForm is valid');
  387. });
  388. it('call data-bv-onerror', function() {
  389. this.$email.val('email@domain');
  390. this.bv.validate();
  391. expect($('#msg').html()).toEqual('My.NameSpace.onFormInvalid() called, form eventForm is invalid');
  392. });
  393. });
  394. describe('event form trigger', function() {
  395. beforeEach(function() {
  396. $([
  397. '<form class="form-horizontal" id="eventForm">',
  398. '<div id="msg"></div>',
  399. '<div class="form-group">',
  400. '<input type="text" name="email" data-bv-emailaddress />',
  401. '</div>',
  402. '</form>'
  403. ].join('\n')).appendTo('body');
  404. $('#eventForm')
  405. .bootstrapValidator()
  406. .on('success.form.bv', function(e) {
  407. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  408. })
  409. .on('error.form.bv', function(e) {
  410. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  411. });
  412. this.bv = $('#eventForm').data('bootstrapValidator');
  413. this.$email = this.bv.getFieldElements('email');
  414. });
  415. afterEach(function() {
  416. $('#eventForm').bootstrapValidator('destroy').remove();
  417. });
  418. it('trigger success.form.bv', function() {
  419. this.$email.val('email@domain.com');
  420. this.bv.validate();
  421. expect($('#msg').html()).toEqual('form eventForm triggered success.form.bv event');
  422. });
  423. it('trigger error.form.bv', function() {
  424. this.$email.val('email@domain');
  425. this.bv.validate();
  426. expect($('#msg').html()).toEqual('form eventForm triggered error.form.bv event');
  427. });
  428. });
  429. describe('event form programmatically', function() {
  430. beforeEach(function() {
  431. $([
  432. '<form class="form-horizontal" id="eventForm">',
  433. '<div id="msg"></div>',
  434. '<div class="form-group">',
  435. '<input type="text" name="email" data-bv-emailaddress />',
  436. '</div>',
  437. '</form>'
  438. ].join('\n')).appendTo('body');
  439. $('#eventForm').bootstrapValidator({
  440. onSuccess: function(e) {
  441. $('#msg').html('onSuccess() called');
  442. },
  443. onError: function(e) {
  444. $('#msg').html('onError() called');
  445. }
  446. });
  447. this.bv = $('#eventForm').data('bootstrapValidator');
  448. this.$email = this.bv.getFieldElements('email');
  449. });
  450. afterEach(function() {
  451. $('#eventForm').bootstrapValidator('destroy').remove();
  452. });
  453. it('call onSuccess()', function() {
  454. this.$email.val('email@domain.com');
  455. this.bv.validate();
  456. expect($('#msg').html()).toEqual('onSuccess() called');
  457. });
  458. it('call onError()', function() {
  459. this.$email.val('email@domain');
  460. this.bv.validate();
  461. expect($('#msg').html()).toEqual('onError() called');
  462. });
  463. });
  464. // ---
  465. // Field events
  466. // ---
  467. function onEmailValid(e, data) {
  468. $('#msg').html(data.field + ' is valid');
  469. };
  470. function onEmailInvalid(e, data) {
  471. $('#msg').html(data.field + ' is invalid');
  472. };
  473. describe('event field attribute callback global', function() {
  474. beforeEach(function() {
  475. $([
  476. '<form class="form-horizontal" id="eventForm">',
  477. '<div id="msg"></div>',
  478. '<div class="form-group">',
  479. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="onEmailValid" data-bv-onerror="onEmailInvalid" />',
  480. '</div>',
  481. '</form>'
  482. ].join('\n')).appendTo('body');
  483. $('#eventForm').bootstrapValidator();
  484. this.bv = $('#eventForm').data('bootstrapValidator');
  485. this.$email = this.bv.getFieldElements('email');
  486. });
  487. afterEach(function() {
  488. $('#eventForm').bootstrapValidator('destroy').remove();
  489. });
  490. it('call data-bv-onsuccess', function() {
  491. this.$email.val('email@domain.com');
  492. this.bv.validate();
  493. expect($('#msg').html()).toEqual('email is valid');
  494. });
  495. it('call data-bv-onerror', function() {
  496. this.$email.val('email@domain');
  497. this.bv.validate();
  498. expect($('#msg').html()).toEqual('email is invalid');
  499. });
  500. });
  501. describe('event field attribute callback namespace', function() {
  502. beforeEach(function() {
  503. $([
  504. '<form class="form-horizontal" id="eventForm">',
  505. '<div id="msg"></div>',
  506. '<div class="form-group">',
  507. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="My.NameSpace.onEmailValid" data-bv-onerror="My.NameSpace.onEmailInvalid" />',
  508. '</div>',
  509. '</form>'
  510. ].join('\n')).appendTo('body');
  511. $('#eventForm').bootstrapValidator();
  512. this.bv = $('#eventForm').data('bootstrapValidator');
  513. this.$email = this.bv.getFieldElements('email');
  514. });
  515. afterEach(function() {
  516. $('#eventForm').bootstrapValidator('destroy').remove();
  517. });
  518. it('call data-bv-onsuccess', function() {
  519. this.$email.val('email@domain.com');
  520. this.bv.validate();
  521. expect($('#msg').html()).toEqual('My.NameSpace.onEmailValid() called, email is valid');
  522. });
  523. it('call data-bv-onerror', function() {
  524. this.$email.val('email@domain');
  525. this.bv.validate();
  526. expect($('#msg').html()).toEqual('My.NameSpace.onEmailInvalid() called, email is invalid');
  527. });
  528. });
  529. describe('event field trigger', function() {
  530. beforeEach(function() {
  531. $([
  532. '<form class="form-horizontal" id="eventForm">',
  533. '<div id="msg"></div>',
  534. '<div class="form-group">',
  535. '<input type="text" name="email" data-bv-emailaddress />',
  536. '</div>',
  537. '</form>'
  538. ].join('\n')).appendTo('body');
  539. $('#eventForm')
  540. .bootstrapValidator()
  541. .on('success.field.bv', '[name="email"]', function(e, data) {
  542. $('#msg').html('triggered success.field.bv on ' + data.field);
  543. })
  544. .on('error.field.bv', '[name="email"]', function(e, data) {
  545. $('#msg').html('triggered error.field.bv on ' + data.field);
  546. });
  547. this.bv = $('#eventForm').data('bootstrapValidator');
  548. this.$email = this.bv.getFieldElements('email');
  549. });
  550. afterEach(function() {
  551. $('#eventForm').bootstrapValidator('destroy').remove();
  552. });
  553. it('trigger success.field.bv', function() {
  554. this.$email.val('email@domain.com');
  555. this.bv.validate();
  556. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  557. });
  558. it('trigger error.field.bv', function() {
  559. this.$email.val('email@domain');
  560. this.bv.validate();
  561. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  562. });
  563. });
  564. describe('event field programmatically', function() {
  565. beforeEach(function() {
  566. $([
  567. '<form class="form-horizontal" id="eventForm">',
  568. '<div id="msg"></div>',
  569. '<div class="form-group">',
  570. '<input type="text" name="email" data-bv-emailaddress />',
  571. '</div>',
  572. '</form>'
  573. ].join('\n')).appendTo('body');
  574. $('#eventForm').bootstrapValidator({
  575. fields: {
  576. email: {
  577. onSuccess: function(e, data) {
  578. $('#msg').html('onSuccess() called');
  579. },
  580. onError: function(e, data) {
  581. $('#msg').html('onError() called');
  582. },
  583. validator: {
  584. emailAddress: {}
  585. }
  586. }
  587. }
  588. });
  589. this.bv = $('#eventForm').data('bootstrapValidator');
  590. this.$email = this.bv.getFieldElements('email');
  591. });
  592. afterEach(function() {
  593. $('#eventForm').bootstrapValidator('destroy').remove();
  594. });
  595. it('call onSuccess()', function() {
  596. this.$email.val('email@domain.com');
  597. this.bv.validate();
  598. expect($('#msg').html()).toEqual('onSuccess() called');
  599. });
  600. it('call onError()', function() {
  601. this.$email.val('email@domain');
  602. this.bv.validate();
  603. expect($('#msg').html()).toEqual('onError() called');
  604. });
  605. });
  606. describe('excluded', function() {
  607. beforeEach(function() {
  608. $([
  609. '<div class="container">',
  610. '<form class="form-horizontal" id="excludedForm" data-bv-excluded="[name=\'email\']">',
  611. '<div class="form-group">',
  612. '<input type="text" name="username" required />',
  613. '</div>',
  614. '<div class="form-group">',
  615. '<input type="text" name="email" required data-bv-emailaddress />',
  616. '</div>',
  617. '</form>',
  618. '</div>'
  619. ].join('')).appendTo('body');
  620. $('#excludedForm').bootstrapValidator();
  621. this.bv = $('#excludedForm').data('bootstrapValidator');
  622. this.$username = this.bv.getFieldElements('username');
  623. this.$email = this.bv.getFieldElements('email');
  624. });
  625. afterEach(function() {
  626. $('#excludedForm').bootstrapValidator('destroy').parent().remove();
  627. });
  628. it('excluded form declarative', function() {
  629. this.bv.validate();
  630. expect(this.bv.isValid()).toEqual(false);
  631. this.bv.resetForm();
  632. this.$username.val('your_user_name');
  633. this.$email.val('');
  634. this.bv.validate();
  635. expect(this.bv.isValid()).toBeTruthy();
  636. });
  637. it('excluded form programmatically', function() {
  638. this.bv.destroy();
  639. $('#excludedForm').removeAttr('data-bv-excluded');
  640. $('#excludedForm').bootstrapValidator({
  641. excluded: '[name="username"]'
  642. });
  643. this.bv = $('#excludedForm').data('bootstrapValidator');
  644. this.$username = this.bv.getFieldElements('username');
  645. this.$email = this.bv.getFieldElements('email');
  646. this.$username.val('');
  647. this.$email.val('invalid#email.com');
  648. this.bv.validate();
  649. expect(this.bv.isValid()).toEqual(false);
  650. this.bv.resetForm();
  651. this.$email.val('valid@email.com');
  652. this.bv.validate();
  653. expect(this.bv.isValid()).toBeTruthy();
  654. });
  655. it('excluded field declarative', function() {
  656. this.bv.destroy();
  657. $('#excludedForm').removeAttr('data-bv-excluded');
  658. $('#excludedForm').find('[name="username"]').attr('data-bv-excluded', 'true');
  659. $('#excludedForm').find('[name="email"]').attr('data-bv-excluded', 'false');
  660. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  661. this.$username = this.bv.getFieldElements('username');
  662. this.$email = this.bv.getFieldElements('email');
  663. this.$username.val('');
  664. this.$email.val('');
  665. this.bv.validate();
  666. expect(this.bv.isValid()).toEqual(false);
  667. this.bv.resetForm();
  668. this.$email.val('invalid#email.com');
  669. this.bv.validate();
  670. expect(this.bv.isValid()).toEqual(false);
  671. this.bv.resetForm();
  672. this.$email.val('valid@email.com');
  673. this.bv.validate();
  674. expect(this.bv.isValid()).toBeTruthy();
  675. });
  676. it('excluded field programmatically true/false', function() {
  677. this.bv.destroy();
  678. $('#excludedForm').removeAttr('data-bv-excluded');
  679. $('#excludedForm').bootstrapValidator({
  680. fields: {
  681. username: {
  682. excluded: true
  683. },
  684. email: {
  685. excluded: false
  686. }
  687. }
  688. });
  689. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  690. this.$username = this.bv.getFieldElements('username');
  691. this.$email = this.bv.getFieldElements('email');
  692. this.$username.val('');
  693. this.$email.val('');
  694. this.bv.validate();
  695. expect(this.bv.isValid()).toEqual(false);
  696. this.bv.resetForm();
  697. this.$email.val('invalid#email.com');
  698. this.bv.validate();
  699. expect(this.bv.isValid()).toEqual(false);
  700. this.bv.resetForm();
  701. this.$email.val('valid@email.com');
  702. this.bv.validate();
  703. expect(this.bv.isValid()).toBeTruthy();
  704. });
  705. it('excluded field programmatically "true"/"false"', function() {
  706. this.bv.destroy();
  707. $('#excludedForm').removeAttr('data-bv-excluded');
  708. $('#excludedForm').bootstrapValidator({
  709. fields: {
  710. username: {
  711. excluded: 'false'
  712. },
  713. email: {
  714. excluded: 'true'
  715. }
  716. }
  717. });
  718. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  719. this.$username = this.bv.getFieldElements('username');
  720. this.$email = this.bv.getFieldElements('email');
  721. this.$username.val('');
  722. this.$email.val('valid@email.com');
  723. this.bv.validate();
  724. expect(this.bv.isValid()).toEqual(false);
  725. this.bv.resetForm();
  726. this.$username.val('your_user_name');
  727. this.$email.val('invalid#email.com');
  728. this.bv.validate();
  729. expect(this.bv.isValid()).toBeTruthy();
  730. });
  731. });
  732. describe('group option', function() {
  733. beforeEach(function() {
  734. $([
  735. '<form id="groupForm" method="post" class="form-horizontal">',
  736. '<div class="form-group">',
  737. '<div class="firstNameGroup">',
  738. '<label class="col-sm-2 control-label">First name</label>',
  739. '<div class="col-sm-4">',
  740. '<input type="text" class="form-control" name="firstName" />',
  741. '</div>',
  742. '</div>',
  743. '<div class="lastNameGroup">',
  744. '<label class="col-sm-2 control-label">Last name</label>',
  745. '<div class="col-sm-4">',
  746. '<input type="text" class="form-control" name="lastName" data-bv-group=".lastNameGroup" />',
  747. '</div>',
  748. '</div>',
  749. '</div>',
  750. '<div class="form-group">',
  751. '<label class="col-sm-2 control-label">Username</label>',
  752. '<div class="col-sm-5">',
  753. '<input type="text" class="form-control" name="username" />',
  754. '</div>',
  755. '</div>',
  756. '</form>'
  757. ].join('')).appendTo('body');
  758. $('#groupForm').bootstrapValidator({
  759. fields: {
  760. firstName: {
  761. group: '.firstNameGroup',
  762. validators: {
  763. notEmpty: {
  764. message: 'The first name is required and cannot be empty'
  765. }
  766. }
  767. },
  768. lastName: {
  769. validators: {
  770. notEmpty: {
  771. message: 'The last name is required and cannot be empty'
  772. }
  773. }
  774. },
  775. username: {
  776. validators: {
  777. notEmpty: {
  778. message: 'The username is required and cannot be empty'
  779. },
  780. stringLength: {
  781. min: 6,
  782. max: 30,
  783. message: 'The username must be more than 6 and less than 30 characters long'
  784. },
  785. regexp: {
  786. regexp: /^[a-zA-Z0-9_\.]+$/,
  787. message: 'The username can only consist of alphabetical, number, dot and underscore'
  788. }
  789. }
  790. }
  791. }
  792. });
  793. this.bv = $('#groupForm').data('bootstrapValidator');
  794. this.$firstName = this.bv.getFieldElements('firstName');
  795. this.$lastName = this.bv.getFieldElements('lastName');
  796. this.$username = this.bv.getFieldElements('username');
  797. });
  798. afterEach(function() {
  799. $('#groupForm').bootstrapValidator('destroy').remove();
  800. });
  801. it('group default', function() {
  802. this.$username.val('123@#$');
  803. this.bv.validate();
  804. expect(this.$username.parents('.form-group').hasClass('has-error')).toBeTruthy();
  805. expect(this.$username.parents('.form-group').hasClass('has-success')).toEqual(false);
  806. this.bv.resetForm();
  807. this.$username.val('validUser.Name');
  808. this.bv.validate();
  809. expect(this.$username.parents('.form-group').hasClass('has-success')).toBeTruthy();
  810. expect(this.$username.parents('.form-group').hasClass('has-error')).toEqual(false);
  811. });
  812. it('group programmatically', function() {
  813. this.$firstName.val('');
  814. this.bv.validate();
  815. expect(this.$firstName.parents('.firstNameGroup').hasClass('has-error')).toBeTruthy();
  816. expect(this.$firstName.parents('.firstNameGroup').hasClass('has-success')).toEqual(false);
  817. expect(this.$firstName.parents('.form-group').hasClass('has-error')).toEqual(false);
  818. });
  819. it('group declarative', function() {
  820. this.$firstName.val('First');
  821. this.$lastName.val('Last');
  822. this.bv.validate();
  823. expect(this.$lastName.parents('.lastNameGroup').hasClass('has-success')).toBeTruthy();
  824. expect(this.$lastName.parents('.lastNameGroup').hasClass('has-error')).toEqual(false);
  825. expect(this.$lastName.parents('.form-group').hasClass('has-success')).toEqual(false);
  826. expect(this.$lastName.parents('.form-group').hasClass('has-error')).toEqual(false);
  827. });
  828. });
  829. describe('i18n', function() {
  830. beforeEach(function() {
  831. $([
  832. '<form id="i18nForm" class="form-horizontal">',
  833. '<div class="form-group">',
  834. '<label class="col-lg-3 control-label">Full name</label>',
  835. '<div class="col-lg-5">',
  836. '<input type="text" class="form-control" name="fullName" />',
  837. '</div>',
  838. '</div>',
  839. '<div class="form-group">',
  840. '<label class="col-lg-3 control-label">Username</label>',
  841. '<div class="col-lg-5">',
  842. '<input type="text" class="form-control" name="username" />',
  843. '</div>',
  844. '</div>',
  845. '<div class="form-group">',
  846. '<label class="col-lg-3 control-label">Email address</label>',
  847. '<div class="col-lg-5">',
  848. '<input type="text" class="form-control" name="email" />',
  849. '</div>',
  850. '</div>',
  851. '<div class="form-group">',
  852. '<label class="col-lg-3 control-label">Password</label>',
  853. '<div class="col-lg-5">',
  854. '<input type="password" class="form-control" name="password" />',
  855. '</div>',
  856. '</div>',
  857. '<div class="form-group">',
  858. '<label class="col-lg-3 control-label">Retype password</label>',
  859. '<div class="col-lg-5">',
  860. '<input type="password" class="form-control" name="confirmPassword" />',
  861. '</div>',
  862. '</div>',
  863. '<div class="form-group">',
  864. '<label class="col-lg-3 control-label">Gender</label>',
  865. '<div class="col-lg-5">',
  866. '<div class="radio">',
  867. '<label><input type="radio" name="gender" value="male" /> Male</label>',
  868. '</div>',
  869. '<div class="radio">',
  870. '<label><input type="radio" name="gender" value="female" /> Female</label>',
  871. '</div>',
  872. '<div class="radio">',
  873. '<label><input type="radio" name="gender" value="other" /> Other</label>',
  874. '</div>',
  875. '</div>',
  876. '</div>',
  877. '<div class="form-group">',
  878. '<label class="col-lg-3 control-label">Age</label>',
  879. '<div class="col-lg-3">',
  880. '<input type="text" class="form-control" name="age" />',
  881. '</div>',
  882. '</div>',
  883. '<div class="form-group">',
  884. '<label class="col-lg-3 control-label">Website</label>',
  885. '<div class="col-lg-5">',
  886. '<input type="text" class="form-control" name="website" />',
  887. '</div>',
  888. '</div>',
  889. '<div class="form-group">',
  890. '<label class="col-lg-3 control-label">Phone number</label>',
  891. '<div class="col-lg-5">',
  892. '<input type="text" class="form-control" name="phoneNumber" />',
  893. '</div>',
  894. '</div>',
  895. '<div class="form-group">',
  896. '<label class="col-lg-3 control-label">Languages</label>',
  897. '<div class="col-lg-5">',
  898. '<div class="checkbox">',
  899. '<label><input type="checkbox" name="languages[]" value="english" /> English</label>',
  900. '</div>',
  901. '<div class="checkbox">',
  902. '<label><input type="checkbox" name="languages[]" value="french" /> French</label>',
  903. '</div>',
  904. '<div class="checkbox">',
  905. '<label><input type="checkbox" name="languages[]" value="german" /> German</label>',
  906. '</div>',
  907. '<div class="checkbox">',
  908. '<label><input type="checkbox" name="languages[]" value="russian" /> Russian</label>',
  909. '</div>',
  910. '<div class="checkbox">',
  911. '<label><input type="checkbox" name="languages[]" value="other" /> Other</label>',
  912. '</div>',
  913. '</div>',
  914. '</div>',
  915. '<div class="form-group">',
  916. '<label class="col-lg-3 control-label">Programming Languages</label>',
  917. '<div class="col-lg-5">',
  918. '<div class="checkbox">',
  919. '<label><input type="checkbox" name="programs[]" value="net" /> .Net</label>',
  920. '</div>',
  921. '<div class="checkbox">',
  922. '<label><input type="checkbox" name="programs[]" value="java" /> Java</label>',
  923. '</div>',
  924. '<div class="checkbox">',
  925. '<label><input type="checkbox" name="programs[]" value="c" /> C/C++</label>',
  926. '</div>',
  927. '<div class="checkbox">',
  928. '<label><input type="checkbox" name="programs[]" value="php" /> PHP</label>',
  929. '</div>',
  930. '<div class="checkbox">',
  931. '<label><input type="checkbox" name="programs[]" value="perl" /> Perl</label>',
  932. '</div>',
  933. '<div class="checkbox">',
  934. '<label><input type="checkbox" name="programs[]" value="ruby" /> Ruby</label>',
  935. '</div>',
  936. '<div class="checkbox">',
  937. '<label><input type="checkbox" name="programs[]" value="python" /> Python</label>',
  938. '</div>',
  939. '<div class="checkbox">',
  940. '<label><input type="checkbox" name="programs[]" value="javascript" /> Javascript</label>',
  941. '</div>',
  942. '</div>',
  943. '</div>',
  944. '</form>'
  945. ].join('')).appendTo('body');
  946. $('#i18nForm').bootstrapValidator({
  947. feedbackIcons: {
  948. valid: 'glyphicon glyphicon-ok',
  949. invalid: 'glyphicon glyphicon-remove',
  950. validating: 'glyphicon glyphicon-refresh'
  951. },
  952. fields: {
  953. fullName: {
  954. validators: {
  955. notEmpty: {},
  956. stringCase: {
  957. 'case': 'upper'
  958. }
  959. }
  960. },
  961. username: {
  962. validators: {
  963. notEmpty: {},
  964. stringLength: {
  965. min: 6,
  966. max: 20
  967. },
  968. regexp: {
  969. regexp: /^[a-zA-Z0-9_\.]+$/
  970. },
  971. different: {
  972. field: 'password'
  973. }
  974. }
  975. },
  976. email: {
  977. validators: {
  978. emailAddress: {}
  979. }
  980. },
  981. password: {
  982. validators: {
  983. notEmpty: {},
  984. identical: {
  985. field: 'confirmPassword'
  986. },
  987. different: {
  988. field: 'username'
  989. }
  990. }
  991. },
  992. confirmPassword: {
  993. validators: {
  994. notEmpty: {},
  995. identical: {
  996. field: 'password'
  997. },
  998. different: {
  999. field: 'username'
  1000. }
  1001. }
  1002. },
  1003. age: {
  1004. validators: {
  1005. notEmpty: {},
  1006. digits: {},
  1007. greaterThan: {
  1008. value: 18
  1009. },
  1010. lessThan: {
  1011. value: 100
  1012. }
  1013. }
  1014. },
  1015. website: {
  1016. validators: {
  1017. notEmpty: {},
  1018. uri: {}
  1019. }
  1020. },
  1021. phoneNumber: {
  1022. validators: {
  1023. notEmpty: {},
  1024. digits: {},
  1025. phone: {
  1026. country: 'US'
  1027. }
  1028. }
  1029. },
  1030. gender: {
  1031. validators: {
  1032. notEmpty: {}
  1033. }
  1034. },
  1035. 'languages[]': {
  1036. validators: {
  1037. notEmpty: {}
  1038. }
  1039. },
  1040. 'programs[]': {
  1041. validators: {
  1042. choice: {
  1043. min: 2,
  1044. max: 4
  1045. }
  1046. }
  1047. }
  1048. }
  1049. });
  1050. this.bv = $('#i18nForm').data('bootstrapValidator');
  1051. this.$fullName = this.bv.getFieldElements('fullName');
  1052. this.$email = this.bv.getFieldElements('email');
  1053. this.$userName = this.bv.getFieldElements('username');
  1054. this.$password = this.bv.getFieldElements('password');
  1055. this.$confirm = this.bv.getFieldElements('confirmPassword');
  1056. this.$age = this.bv.getFieldElements('age');
  1057. this.$website = this.bv.getFieldElements('website');
  1058. this.$phone = this.bv.getFieldElements('phoneNumber');
  1059. this.$program = this.bv.getFieldElements('programs[]');
  1060. });
  1061. afterEach(function() {
  1062. $('#i18nForm').bootstrapValidator('destroy').remove();
  1063. });
  1064. it('default message', function() {
  1065. this.bv.validate();
  1066. expect(this.bv.getMessages(this.$fullName, 'notEmpty')[0]).toEqual($.fn.bootstrapValidator.i18n.notEmpty['default']);
  1067. this.$fullName.val('lowerName');
  1068. this.bv.revalidateField('fullName');
  1069. expect(this.bv.getMessages('fullName', 'stringCase')[0]).toEqual($.fn.bootstrapValidator.i18n.stringCase.upper);
  1070. this.bv.resetForm();
  1071. this.$userName.val('123');
  1072. this.bv.validate();
  1073. expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual($.fn.bootstrapValidator.i18n.stringLength.getMessage({ min: 6, max: 20 }));
  1074. this.bv.resetForm();
  1075. this.$userName.val('contain@#$');
  1076. this.bv.validate();
  1077. expect(this.bv.getMessages(this.$userName, 'regexp')[0]).toEqual($.fn.bootstrapValidator.i18n.regexp['default']);
  1078. this.bv.resetForm();
  1079. this.$userName.val('validUserName');
  1080. this.$password.val('validUserName');
  1081. this.bv.validate();
  1082. expect(this.bv.getMessages('username', 'different')[0]).toEqual($.fn.bootstrapValidator.i18n.different['default']);
  1083. this.bv.resetForm();
  1084. this.$email.val('invalid#email@address');
  1085. this.bv.validate();
  1086. expect(this.bv.getMessages(this.$email, 'emailAddress')[0]).toEqual($.fn.bootstrapValidator.i18n.emailAddress['default']);
  1087. this.bv.resetForm();
  1088. this.$password.val('@S3cur3P@@w0rd');
  1089. this.$confirm.val('notMatch');
  1090. this.bv.validate();
  1091. expect(this.bv.getMessages('password', 'identical')[0]).toEqual($.fn.bootstrapValidator.i18n.identical['default']);
  1092. this.bv.resetForm();
  1093. this.$age.val('notDigit');
  1094. this.bv.validate();
  1095. expect(this.bv.getMessages('age', 'digits')[0]).toEqual($.fn.bootstrapValidator.i18n.digits['default']);
  1096. this.bv.resetForm();
  1097. this.$age.val(10);
  1098. this.bv.validate();
  1099. expect(this.bv.getMessages(this.$age, 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.i18n.greaterThan.getMessage({ value: 18 }));
  1100. this.bv.resetForm();
  1101. this.$age.val(120);
  1102. this.bv.validate();
  1103. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.i18n.lessThan.getMessage({ value: 100 }));
  1104. this.bv.resetForm();
  1105. this.$website.val('http://invalidWebsite');
  1106. this.bv.validate();
  1107. expect(this.bv.getMessages('website', 'uri')[0]).toEqual($.fn.bootstrapValidator.i18n.uri['default']);
  1108. this.bv.resetForm();
  1109. this.$phone.val('123456');
  1110. this.bv.validate();
  1111. expect(this.bv.getMessages('phoneNumber', 'phone')[0]).toEqual($.fn.bootstrapValidator.i18n.phone.getMessage({ country: 'US' }));
  1112. this.bv.resetForm();
  1113. this.$program.eq(0).prop('checked', 'checked');
  1114. this.bv.validate();
  1115. expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual($.fn.bootstrapValidator.i18n.choice.getMessage({ min: 2, max: 4 }));
  1116. this.bv.resetForm();
  1117. this.$program.prop('checked', 'checked');
  1118. this.bv.validate();
  1119. expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual($.fn.bootstrapValidator.i18n.choice.getMessage({ min: 2, max: 4 }));
  1120. });
  1121. });
  1122. describe('message', function() {
  1123. beforeEach(function() {
  1124. var html = [
  1125. '<div class="container">',
  1126. '<form class="form-horizontal" id="messageForm">',
  1127. '<div class="form-group">',
  1128. '<input type="password" class="form-control" name="password" placeholder="Enter secure password" />',
  1129. '</div>',
  1130. '</form>',
  1131. '</div>'
  1132. ].join('\n');
  1133. $(html).appendTo('body');
  1134. $('#messageForm').bootstrapValidator({
  1135. fields: {
  1136. password: {
  1137. validators: {
  1138. notEmpty: {
  1139. message: 'The password is required'
  1140. },
  1141. callback: {
  1142. callback: function(value, validator) {
  1143. // Check the password strength
  1144. if (value.length < 6) {
  1145. return {
  1146. valid: false,
  1147. message: 'The password must be more than 6 characters'
  1148. }
  1149. }
  1150. if (value === value.toLowerCase()) {
  1151. return {
  1152. valid: false,
  1153. message: 'The password must contain at least one upper case character'
  1154. }
  1155. }
  1156. if (value === value.toUpperCase()) {
  1157. return {
  1158. valid: false,
  1159. message: 'The password must contain at least one lower case character'
  1160. }
  1161. }
  1162. if (value.search(/[0-9]/) < 0) {
  1163. return {
  1164. valid: false,
  1165. message: 'The password must contain at least one digit'
  1166. }
  1167. }
  1168. return true;
  1169. }
  1170. }
  1171. }
  1172. }
  1173. }
  1174. });
  1175. this.bv = $('#messageForm').data('bootstrapValidator');
  1176. this.$password = this.bv.getFieldElements('password');
  1177. });
  1178. afterEach(function() {
  1179. $('#messageForm').bootstrapValidator('destroy').parent().remove();
  1180. });
  1181. it('update message from callback', function() {
  1182. this.bv.resetForm();
  1183. this.$password.val('123');
  1184. this.bv.validate();
  1185. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must be more than 6 characters');
  1186. this.bv.resetForm();
  1187. this.$password.val('no_upper_case!@#');
  1188. this.bv.validate();
  1189. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one upper case character');
  1190. this.bv.resetForm();
  1191. this.$password.val('NO_LOWER_CASE123');
  1192. this.bv.validate();
  1193. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one lower case character');
  1194. this.bv.resetForm();
  1195. this.$password.val('NoDigits!@#');
  1196. this.bv.validate();
  1197. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one digit');
  1198. });
  1199. it('call updateMessage()', function() {
  1200. this.bv.updateStatus('password', this.bv.STATUS_INVALID, 'callback');
  1201. this.bv.updateMessage('password', 'callback', 'The password is weak');
  1202. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password is weak');
  1203. this.bv.updateMessage(this.$password, 'callback', 'The password is not strong');
  1204. expect(this.bv.getMessages(this.$password, 'callback')[0]).toEqual('The password is not strong');
  1205. });
  1206. });
  1207. function validateCaptcha(value, validator, $field) {
  1208. var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
  1209. return value === sum + '';
  1210. };
  1211. describe('callback', function() {
  1212. beforeEach(function() {
  1213. $(['<div class="container">',
  1214. '<form class="form-horizontal" id="callbackForm">',
  1215. '<div class="form-group">',
  1216. '<label class="col-md-3 control-label" id="captchaOperation"></label>',
  1217. '<div class="col-md-2">',
  1218. '<input type="text" class="form-control" name="captcha" />',
  1219. '</div>',
  1220. '</div>',
  1221. '<div class="form-group">',
  1222. '<div class="col-md-2 col-md-offset-3">',
  1223. '<input type="text" class="form-control" name="declarativeCaptcha" data-bv-callback data-bv-callback-callback="validateCaptcha" />',
  1224. '</div>',
  1225. '</div>',
  1226. '</form>',
  1227. '</div>'
  1228. ].join('\n')).appendTo('body');
  1229. $('#callbackForm').bootstrapValidator({
  1230. fields: {
  1231. captcha: {
  1232. validators: {
  1233. callback: {
  1234. message: 'Wrong answer',
  1235. callback: function(value, validator, $field) {
  1236. return validateCaptcha(value, validator, $field);
  1237. }
  1238. }
  1239. }
  1240. }
  1241. }
  1242. });
  1243. this.bv = $('#callbackForm').data('bootstrapValidator');
  1244. this.$captcha = this.bv.getFieldElements('captcha');
  1245. this.$declarativeCaptcha = this.bv.getFieldElements('declarativeCaptcha');
  1246. });
  1247. afterEach(function() {
  1248. $('#callbackForm').bootstrapValidator('destroy').parent().remove();
  1249. });
  1250. it('execute the callback', function() {
  1251. $('#captchaOperation').html('1 + 2');
  1252. this.$captcha.val('3');
  1253. this.bv.validate();
  1254. expect(this.bv.isValidField('captcha')).toBeTruthy();
  1255. this.bv.resetForm();
  1256. this.$captcha.val('5');
  1257. this.bv.validate();
  1258. expect(this.bv.isValidField('captcha')).toEqual(false);
  1259. });
  1260. it('callback declarative', function() {
  1261. $('#captchaOperation').html('10 + 20');
  1262. this.$declarativeCaptcha.val('40');
  1263. this.bv.validate();
  1264. expect(this.bv.isValidField('declarativeCaptcha')).toEqual(false);
  1265. this.bv.resetForm();
  1266. this.$declarativeCaptcha.val('30');
  1267. this.bv.validate();
  1268. expect(this.bv.isValidField('declarativeCaptcha')).toBeTruthy();
  1269. });
  1270. });
  1271. describe('creditCard', function() {
  1272. // Get the fake credit card number at http://www.getcreditcardnumbers.com/
  1273. beforeEach(function() {
  1274. var html = [
  1275. '<div class="container">',
  1276. '<form class="form-horizontal" id="ccForm">',
  1277. '<div class="form-group">',
  1278. '<input type="text" name="cc" data-bv-creditcard />',
  1279. '</div>',
  1280. '</form>',
  1281. '</div>'
  1282. ].join('\n');
  1283. $(html).appendTo('body');
  1284. $('#ccForm').bootstrapValidator();
  1285. this.bv = $('#ccForm').data('bootstrapValidator');
  1286. this.$creditCard = this.bv.getFieldElements('cc');
  1287. });
  1288. afterEach(function() {
  1289. $('#ccForm').bootstrapValidator('destroy').parent().remove();
  1290. });
  1291. it('accept spaces', function() {
  1292. this.$creditCard.val('5267 9789 9451 9654');
  1293. this.bv.validate();
  1294. expect(this.bv.isValidField('cc')).toBeTruthy();
  1295. });
  1296. it('accept dashes', function() {
  1297. this.$creditCard.val('6011-2649-6840-4521');
  1298. this.bv.validate();
  1299. expect(this.bv.isValidField('cc')).toBeTruthy();
  1300. });
  1301. it('invalid format', function() {
  1302. this.$creditCard.val('4539.1870.2954.3862');
  1303. this.bv.validate();
  1304. expect(this.bv.isValidField('cc')).toEqual(false);
  1305. });
  1306. it('American Express', function() {
  1307. this.$creditCard.val('340653705597107');
  1308. this.bv.validate();
  1309. expect(this.bv.isValidField('cc')).toBeTruthy();
  1310. });
  1311. it('American Express invalid length', function() {
  1312. this.$creditCard.val('3744148309166730');
  1313. this.bv.validate();
  1314. expect(this.bv.isValidField('cc')).toEqual(false);
  1315. });
  1316. it('American Express invalid prefix', function() {
  1317. this.$creditCard.val('356120148436654');
  1318. this.bv.validate();
  1319. expect(this.bv.isValidField('cc')).toEqual(false);
  1320. });
  1321. it('Diners Club', function() {
  1322. this.$creditCard.val('30130708434187');
  1323. this.bv.validate();
  1324. expect(this.bv.isValidField('cc')).toBeTruthy();
  1325. });
  1326. it('Diners Club (US)', function() {
  1327. this.$creditCard.val('5517479515603901');
  1328. this.bv.validate();
  1329. expect(this.bv.isValidField('cc')).toBeTruthy();
  1330. });
  1331. it('Discover', function() {
  1332. this.$creditCard.val('6011734674929094');
  1333. this.bv.validate();
  1334. expect(this.bv.isValidField('cc')).toBeTruthy();
  1335. });
  1336. it('JCB', function() {
  1337. this.$creditCard.val('3566002020360505');
  1338. this.bv.validate();
  1339. expect(this.bv.isValidField('cc')).toBeTruthy();
  1340. });
  1341. it('Laser', function() {
  1342. this.$creditCard.val('6304 9000 1774 0292 441');
  1343. this.bv.validate();
  1344. expect(this.bv.isValidField('cc')).toBeTruthy();
  1345. });
  1346. it('Maestro', function() {
  1347. this.$creditCard.val('6762835098779303');
  1348. this.bv.validate();
  1349. expect(this.bv.isValidField('cc')).toBeTruthy();
  1350. });
  1351. it('Mastercard', function() {
  1352. this.$creditCard.val('5303765013600904');
  1353. this.bv.validate();
  1354. expect(this.bv.isValidField('cc')).toBeTruthy();
  1355. });
  1356. it('Solo', function() {
  1357. this.$creditCard.val('6334580500000000');
  1358. this.bv.validate();
  1359. expect(this.bv.isValidField('cc')).toBeTruthy();
  1360. });
  1361. it('Visa', function() {
  1362. this.$creditCard.val('4929248980295542');
  1363. this.bv.validate();
  1364. expect(this.bv.isValidField('cc')).toBeTruthy();
  1365. });
  1366. it('Visa invalid check digit', function() {
  1367. this.$creditCard.val('4532599916257826');
  1368. this.bv.validate();
  1369. expect(this.bv.isValidField('cc')).toEqual(false);
  1370. });
  1371. });
  1372. describe('ean', function() {
  1373. beforeEach(function() {
  1374. var html = [
  1375. '<div class="container">',
  1376. '<form class="form-horizontal" id="eanForm">',
  1377. '<div class="form-group">',
  1378. '<input type="text" name="ean" data-bv-ean />',
  1379. '</div>',
  1380. '</form>',
  1381. '</div>'
  1382. ].join('\n');
  1383. $(html).appendTo('body');
  1384. $('#eanForm').bootstrapValidator();
  1385. this.bv = $('#eanForm').data('bootstrapValidator');
  1386. this.$ean = this.bv.getFieldElements('ean');
  1387. });
  1388. afterEach(function() {
  1389. $('#eanForm').bootstrapValidator('destroy').parent().remove();
  1390. });
  1391. it('valid', function() {
  1392. var samples = ['73513537', '9780471117094', '4006381333931'];
  1393. for (var i in samples) {
  1394. this.$ean.val(samples[i]);
  1395. this.bv.validate();
  1396. expect(this.bv.isValidField('ean')).toBeTruthy();
  1397. }
  1398. });
  1399. it('contains only digits', function() {
  1400. this.$ean.val('123abcDEF!@#');
  1401. this.bv.validate();
  1402. expect(this.bv.isValidField('ean')).toEqual(false);
  1403. });
  1404. it('invalid length', function() {
  1405. this.$ean.val('1234567');
  1406. this.bv.validate();
  1407. expect(this.bv.isValidField('ean')).toEqual(false);
  1408. });
  1409. it('invalid check digit', function() {
  1410. this.$ean.val('73513536');
  1411. this.bv.validate();
  1412. expect(this.bv.isValidField('ean')).toEqual(false);
  1413. });
  1414. });
  1415. describe('iban', function() {
  1416. beforeEach(function() {
  1417. var html = [
  1418. '<div class="container">',
  1419. '<form class="form-horizontal" id="ibanForm">',
  1420. '<div class="form-group">',
  1421. '<input type="text" name="iban" data-bv-iban />',
  1422. '</div>',
  1423. '</form>',
  1424. '</div>'
  1425. ].join('\n');
  1426. $(html).appendTo('body');
  1427. $('#ibanForm').bootstrapValidator();
  1428. this.bv = $('#ibanForm').data('bootstrapValidator');
  1429. this.$iban = this.bv.getFieldElements('iban');
  1430. });
  1431. afterEach(function() {
  1432. $('#ibanForm').bootstrapValidator('destroy').parent().remove();
  1433. });
  1434. it('not supported country', function() {
  1435. this.$iban.val('US123456789');
  1436. this.bv.validate();
  1437. expect(this.bv.isValidField('iban')).toEqual(false);
  1438. });
  1439. it('Albania', function() {
  1440. this.$iban.val('AL47212110090000000235698741');
  1441. this.bv.validate();
  1442. expect(this.bv.isValidField('iban')).toBeTruthy();
  1443. });
  1444. it('Algeria', function() {
  1445. this.$iban.val('DZ4000400174401001050486');
  1446. this.bv.validate();
  1447. expect(this.bv.isValidField('iban')).toBeTruthy();
  1448. });
  1449. it('Andorra', function() {
  1450. this.$iban.val('AD1200012030200359100100');
  1451. this.bv.validate();
  1452. expect(this.bv.isValidField('iban')).toBeTruthy();
  1453. });
  1454. it('Angola', function() {
  1455. this.$iban.val('AO06000600000100037131174');
  1456. this.bv.validate();
  1457. expect(this.bv.isValidField('iban')).toBeTruthy();
  1458. });
  1459. it('Austria', function() {
  1460. this.$iban.val('AT611904300234573201');
  1461. this.bv.validate();
  1462. expect(this.bv.isValidField('iban')).toBeTruthy();
  1463. });
  1464. it('Azerbaijan', function() {
  1465. this.$iban.val('AZ21NABZ00000000137010001944');
  1466. this.bv.validate();
  1467. expect(this.bv.isValidField('iban')).toBeTruthy();
  1468. });
  1469. it('Bahrain', function() {
  1470. this.$iban.val('BH29BMAG1299123456BH00');
  1471. this.bv.validate();
  1472. expect(this.bv.isValidField('iban')).toBeTruthy();
  1473. });
  1474. it('Belgium', function() {
  1475. this.$iban.val('BE68539007547034');
  1476. this.bv.validate();
  1477. expect(this.bv.isValidField('iban')).toBeTruthy();
  1478. });
  1479. it('Benin', function() {
  1480. this.$iban.val('BJ11B00610100400271101192591');
  1481. this.bv.validate();
  1482. expect(this.bv.isValidField('iban')).toBeTruthy();
  1483. });
  1484. it('Brazil', function() {
  1485. this.$iban.val('BR9700360305000010009795493P1');
  1486. this.bv.validate();
  1487. expect(this.bv.isValidField('iban')).toBeTruthy();
  1488. });
  1489. it('Bulgaria', function() {
  1490. this.$iban.val('BG80BNBG96611020345678');
  1491. this.bv.validate();
  1492. expect(this.bv.isValidField('iban')).toBeTruthy();
  1493. });
  1494. it('Burkina Faso', function() {
  1495. this.$iban.val('BF1030134020015400945000643');
  1496. this.bv.validate();
  1497. expect(this.bv.isValidField('iban')).toBeTruthy();
  1498. });
  1499. it('Burundi', function() {
  1500. this.$iban.val('BI43201011067444');
  1501. this.bv.validate();
  1502. expect(this.bv.isValidField('iban')).toBeTruthy();
  1503. });
  1504. it('Cameroon', function() {
  1505. this.$iban.val('CM2110003001000500000605306');
  1506. this.bv.validate();
  1507. expect(this.bv.isValidField('iban')).toBeTruthy();
  1508. });
  1509. it('Cape Verde', function() {
  1510. this.$iban.val('CV64000300004547069110176');
  1511. this.bv.validate();
  1512. expect(this.bv.isValidField('iban')).toBeTruthy();
  1513. });
  1514. it('Costa Rica', function() {
  1515. this.$iban.val('CR0515202001026284066');
  1516. this.bv.validate();
  1517. expect(this.bv.isValidField('iban')).toBeTruthy();
  1518. });
  1519. it('Croatia', function() {
  1520. this.$iban.val('HR1210010051863000160');
  1521. this.bv.validate();
  1522. expect(this.bv.isValidField('iban')).toBeTruthy();
  1523. });
  1524. it('Cyprus', function() {
  1525. this.$iban.val('CY17002001280000001200527600');
  1526. this.bv.validate();
  1527. expect(this.bv.isValidField('iban')).toBeTruthy();
  1528. });
  1529. it('Czech Republic', function() {
  1530. this.$iban.val('CZ6508000000192000145399');
  1531. this.bv.validate();
  1532. expect(this.bv.isValidField('iban')).toBeTruthy();
  1533. });
  1534. it('Denmark', function() {
  1535. this.$iban.val('DK5000400440116243');
  1536. this.bv.validate();
  1537. expect(this.bv.isValidField('iban')).toBeTruthy();
  1538. });
  1539. it('Dominican Republic', function() {
  1540. this.$iban.val('DO28BAGR00000001212453611324');
  1541. this.bv.validate();
  1542. expect(this.bv.isValidField('iban')).toBeTruthy();
  1543. });
  1544. it('Estonia', function() {
  1545. this.$iban.val('EE382200221020145685');
  1546. this.bv.validate();
  1547. expect(this.bv.isValidField('iban')).toBeTruthy();
  1548. });
  1549. it('Faroe Islands', function() {
  1550. this.$iban.val('FO1464600009692713');
  1551. this.bv.validate();
  1552. expect(this.bv.isValidField('iban')).toBeTruthy();
  1553. });
  1554. it('Finland', function() {
  1555. this.$iban.val('FI2112345600000785');
  1556. this.bv.validate();
  1557. expect(this.bv.isValidField('iban')).toBeTruthy();
  1558. });
  1559. it('France', function() {
  1560. this.$iban.val('FR1420041010050500013M02606');
  1561. this.bv.validate();
  1562. expect(this.bv.isValidField('iban')).toBeTruthy();
  1563. });
  1564. it('Guatemala', function() {
  1565. this.$iban.val('GT82TRAJ01020000001210029690');
  1566. this.bv.validate();
  1567. expect(this.bv.isValidField('iban')).toBeTruthy();
  1568. });
  1569. it('Georgia', function() {
  1570. this.$iban.val('GE29NB0000000101904917');
  1571. this.bv.validate();
  1572. expect(this.bv.isValidField('iban')).toBeTruthy();
  1573. });
  1574. it('Germany', function() {
  1575. this.$iban.val('DE89370400440532013000');
  1576. this.bv.validate();
  1577. expect(this.bv.isValidField('iban')).toBeTruthy();
  1578. });
  1579. it('Gibraltar', function() {
  1580. this.$iban.val('GI75NWBK000000007099453');
  1581. this.bv.validate();
  1582. expect(this.bv.isValidField('iban')).toBeTruthy();
  1583. });
  1584. it('Greece', function() {
  1585. this.$iban.val('GR1601101250000000012300695');
  1586. this.bv.validate();
  1587. expect(this.bv.isValidField('iban')).toBeTruthy();
  1588. });
  1589. it('Greenland', function() {
  1590. this.$iban.val('GL8964710001000206');
  1591. this.bv.validate();
  1592. expect(this.bv.isValidField('iban')).toBeTruthy();
  1593. });
  1594. it('Hungary', function() {
  1595. this.$iban.val('HU42117730161111101800000000');
  1596. this.bv.validate();
  1597. expect(this.bv.isValidField('iban')).toBeTruthy();
  1598. });
  1599. it('Iceland', function() {
  1600. this.$iban.val('IS140159260076545510730339');
  1601. this.bv.validate();
  1602. expect(this.bv.isValidField('iban')).toBeTruthy();
  1603. });
  1604. it('Iran', function() {
  1605. this.$iban.val('IR580540105180021273113007');
  1606. this.bv.validate();
  1607. expect(this.bv.isValidField('iban')).toBeTruthy();
  1608. });
  1609. it('Ireland', function() {
  1610. this.$iban.val('IE29AIBK93115212345678');
  1611. this.bv.validate();
  1612. expect(this.bv.isValidField('iban')).toBeTruthy();
  1613. });
  1614. it('Israel', function() {
  1615. this.$iban.val('IL620108000000099999999');
  1616. this.bv.validate();
  1617. expect(this.bv.isValidField('iban')).toBeTruthy();
  1618. });
  1619. it('Italy', function() {
  1620. this.$iban.val('IT60X0542811101000000123456');
  1621. this.bv.validate();
  1622. expect(this.bv.isValidField('iban')).toBeTruthy();
  1623. });
  1624. it('Ivory Coast', function() {
  1625. this.$iban.val('CI05A00060174100178530011852');
  1626. this.bv.validate();
  1627. expect(this.bv.isValidField('iban')).toBeTruthy();
  1628. });
  1629. it('Jordan', function() {
  1630. this.$iban.val('JO94CBJO0010000000000131000302');
  1631. this.bv.validate();
  1632. expect(this.bv.isValidField('iban')).toBeTruthy();
  1633. });
  1634. it('Kazakhstan', function() {
  1635. this.$iban.val('KZ176010251000042993');
  1636. this.bv.validate();
  1637. expect(this.bv.isValidField('iban')).toBeTruthy();
  1638. });
  1639. it('Kuwait', function() {
  1640. this.$iban.val('KW74NBOK0000000000001000372151');
  1641. this.bv.validate();
  1642. expect(this.bv.isValidField('iban')).toBeTruthy();
  1643. });
  1644. it('Latvia', function() {
  1645. this.$iban.val('LV80BANK0000435195001');
  1646. this.bv.validate();
  1647. expect(this.bv.isValidField('iban')).toBeTruthy();
  1648. });
  1649. it('Lebanon', function() {
  1650. this.$iban.val('LB30099900000001001925579115');
  1651. this.bv.validate();
  1652. expect(this.bv.isValidField('iban')).toBeTruthy();
  1653. });
  1654. it('Liechtenstein', function() {
  1655. this.$iban.val('LI21088100002324013AA');
  1656. this.bv.validate();
  1657. expect(this.bv.isValidField('iban')).toBeTruthy();
  1658. });
  1659. it('Lithuania', function() {
  1660. this.$iban.val('LT121000011101001000');
  1661. this.bv.validate();
  1662. expect(this.bv.isValidField('iban')).toBeTruthy();
  1663. });
  1664. it('Luxembourg', function() {
  1665. this.$iban.val('LU280019400644750000');
  1666. this.bv.validate();
  1667. expect(this.bv.isValidField('iban')).toBeTruthy();
  1668. });
  1669. it('Macedonia', function() {
  1670. this.$iban.val('MK07300000000042425');
  1671. this.bv.validate();
  1672. expect(this.bv.isValidField('iban')).toBeTruthy();
  1673. });
  1674. it('Madagascar', function() {
  1675. this.$iban.val('MG4600005030010101914016056');
  1676. this.bv.validate();
  1677. expect(this.bv.isValidField('iban')).toBeTruthy();
  1678. });
  1679. it('Malta', function() {
  1680. this.$iban.val('MT84MALT011000012345MTLCAST001S');
  1681. this.bv.validate();
  1682. expect(this.bv.isValidField('iban')).toBeTruthy();
  1683. });
  1684. it('Mauritania', function() {
  1685. this.$iban.val('MR1300012000010000002037372');
  1686. this.bv.validate();
  1687. expect(this.bv.isValidField('iban')).toBeTruthy();
  1688. });
  1689. it('Mauritius', function() {
  1690. this.$iban.val('MU17BOMM0101101030300200000MUR');
  1691. this.bv.validate();
  1692. expect(this.bv.isValidField('iban')).toBeTruthy();
  1693. });
  1694. it('Mali', function() {
  1695. this.$iban.val('ML03D00890170001002120000447');
  1696. this.bv.validate();
  1697. expect(this.bv.isValidField('iban')).toBeTruthy();
  1698. });
  1699. it('Moldova', function() {
  1700. this.$iban.val('MD24AG000225100013104168');
  1701. this.bv.validate();
  1702. expect(this.bv.isValidField('iban')).toBeTruthy();
  1703. });
  1704. it('Monaco', function() {
  1705. this.$iban.val('MC5813488000010051108001292');
  1706. this.bv.validate();
  1707. expect(this.bv.isValidField('iban')).toBeTruthy();
  1708. });
  1709. it('Montenegro', function() {
  1710. this.$iban.val('ME25505000012345678951');
  1711. this.bv.validate();
  1712. expect(this.bv.isValidField('iban')).toBeTruthy();
  1713. });
  1714. it('Mozambique', function() {
  1715. this.$iban.val('MZ59000100000011834194157');
  1716. this.bv.validate();
  1717. expect(this.bv.isValidField('iban')).toBeTruthy();
  1718. });
  1719. it('Netherlands', function() {
  1720. this.$iban.val('NL91ABNA0417164300');
  1721. this.bv.validate();
  1722. expect(this.bv.isValidField('iban')).toBeTruthy();
  1723. });
  1724. it('Norway', function() {
  1725. this.$iban.val('NO9386011117947');
  1726. this.bv.validate();
  1727. expect(this.bv.isValidField('iban')).toBeTruthy();
  1728. });
  1729. it('Pakistan', function() {
  1730. this.$iban.val('PK24SCBL0000001171495101');
  1731. this.bv.validate();
  1732. expect(this.bv.isValidField('iban')).toBeTruthy();
  1733. });
  1734. it('Palestine', function() {
  1735. this.$iban.val('PS92PALS000000000400123456702');
  1736. this.bv.validate();
  1737. expect(this.bv.isValidField('iban')).toBeTruthy();
  1738. });
  1739. it('Poland', function() {
  1740. this.$iban.val('PL27114020040000300201355387');
  1741. this.bv.validate();
  1742. expect(this.bv.isValidField('iban')).toBeTruthy();
  1743. });
  1744. it('Portugal', function() {
  1745. this.$iban.val('PT50000201231234567890154');
  1746. this.bv.validate();
  1747. expect(this.bv.isValidField('iban')).toBeTruthy();
  1748. });
  1749. it('Qatar', function() {
  1750. this.$iban.val('QA58DOHB00001234567890ABCDEFG');
  1751. this.bv.validate();
  1752. expect(this.bv.isValidField('iban')).toBeTruthy();
  1753. });
  1754. it('Romania', function() {
  1755. this.$iban.val('RO49AAAA1B31007593840000');
  1756. this.bv.validate();
  1757. expect(this.bv.isValidField('iban')).toBeTruthy();
  1758. });
  1759. it('San Marino', function() {
  1760. this.$iban.val('SM86U0322509800000000270100');
  1761. this.bv.validate();
  1762. expect(this.bv.isValidField('iban')).toBeTruthy();
  1763. });
  1764. it('Saudi Arabia', function() {
  1765. this.$iban.val('SA0380000000608010167519');
  1766. this.bv.validate();
  1767. expect(this.bv.isValidField('iban')).toBeTruthy();
  1768. });
  1769. it('Senegal', function() {
  1770. this.$iban.val('SN12K00100152000025690007542');
  1771. this.bv.validate();
  1772. expect(this.bv.isValidField('iban')).toBeTruthy();
  1773. });
  1774. it('Serbia', function() {
  1775. this.$iban.val('RS35260005601001611379');
  1776. this.bv.validate();
  1777. expect(this.bv.isValidField('iban')).toBeTruthy();
  1778. });
  1779. it('Slovakia', function() {
  1780. this.$iban.val('SK3112000000198742637541');
  1781. this.bv.validate();
  1782. expect(this.bv.isValidField('iban')).toBeTruthy();
  1783. });
  1784. it('Slovenia', function() {
  1785. this.$iban.val('SI56191000000123438');
  1786. this.bv.validate();
  1787. expect(this.bv.isValidField('iban')).toBeTruthy();
  1788. });
  1789. it('Spain', function() {
  1790. this.$iban.val('ES9121000418450200051332');
  1791. this.bv.validate();
  1792. expect(this.bv.isValidField('iban')).toBeTruthy();
  1793. });
  1794. it('Sweden', function() {
  1795. this.$iban.val('SE3550000000054910000003');
  1796. this.bv.validate();
  1797. expect(this.bv.isValidField('iban')).toBeTruthy();
  1798. });
  1799. it('Switzerland', function() {
  1800. this.$iban.val('CH9300762011623852957');
  1801. this.bv.validate();
  1802. expect(this.bv.isValidField('iban')).toBeTruthy();
  1803. });
  1804. it('Tunisia', function() {
  1805. this.$iban.val('TN5914207207100707129648');
  1806. this.bv.validate();
  1807. expect(this.bv.isValidField('iban')).toBeTruthy();
  1808. });
  1809. it('Turkey', function() {
  1810. this.$iban.val('TR330006100519786457841326');
  1811. this.bv.validate();
  1812. expect(this.bv.isValidField('iban')).toBeTruthy();
  1813. });
  1814. it('United Arab Emirates', function() {
  1815. this.$iban.val('AE260211000000230064016');
  1816. this.bv.validate();
  1817. expect(this.bv.isValidField('iban')).toBeTruthy();
  1818. });
  1819. it('United Kingdom', function() {
  1820. this.$iban.val('GB29NWBK60161331926819');
  1821. this.bv.validate();
  1822. expect(this.bv.isValidField('iban')).toBeTruthy();
  1823. });
  1824. it('Virgin Islands, British', function() {
  1825. this.$iban.val('VG96VPVG0000012345678901');
  1826. this.bv.validate();
  1827. expect(this.bv.isValidField('iban')).toBeTruthy();
  1828. });
  1829. it('invalid checksum', function() {
  1830. this.$iban.val('TR330006100519786457841325');
  1831. this.bv.validate();
  1832. expect(this.bv.isValidField('iban')).toEqual(false);
  1833. });
  1834. });
  1835. describe('isbn', function() {
  1836. beforeEach(function() {
  1837. var html = [
  1838. '<div class="container">',
  1839. '<form class="form-horizontal" id="isbnForm">',
  1840. '<div class="form-group">',
  1841. '<input type="text" name="isbn" data-bv-isbn />',
  1842. '</div>',
  1843. '</form>',
  1844. '</div>'
  1845. ].join('\n');
  1846. $(html).appendTo('body');
  1847. $('#isbnForm').bootstrapValidator();
  1848. this.bv = $('#isbnForm').data('bootstrapValidator');
  1849. this.$isbn = this.bv.getFieldElements('isbn');
  1850. });
  1851. afterEach(function() {
  1852. $('#isbnForm').bootstrapValidator('destroy').parent().remove();
  1853. });
  1854. it('isbn10 hyphen', function() {
  1855. var samples = ['99921-58-10-7', '9971-5-0210-0', '960-425-059-0', '80-902734-1-6'];
  1856. for (var i in samples) {
  1857. this.$isbn.val(samples[i]);
  1858. this.bv.validate();
  1859. expect(this.bv.isValidField('isbn')).toBeTruthy();
  1860. }
  1861. });
  1862. it('isbn10 space', function() {
  1863. var samples = ['85 359 0277 5', '1 84356 028 3', '0 684 84328 5', '0 85131 041 9', '0 943396 04 2'];
  1864. for (var i in samples) {
  1865. this.$isbn.val(samples[i]);
  1866. this.bv.validate();
  1867. expect(this.bv.isValidField('isbn')).toBeTruthy();
  1868. }
  1869. });
  1870. it('isbn10 hyphen with X', function() {
  1871. var samples = ['0-8044-2957-X', '0-9752298-0-X'];
  1872. for (var i in samples) {
  1873. this.$isbn.val(samples[i]);
  1874. this.bv.validate();
  1875. expect(this.bv.isValidField('isbn')).toBeTruthy();
  1876. }
  1877. });
  1878. it('isbn10 invalid check digit', function() {
  1879. this.$isbn.val('99921-58-10-6');
  1880. this.bv.validate();
  1881. expect(this.bv.isValidField('isbn')).toEqual(false);
  1882. });
  1883. it('isbn13', function() {
  1884. this.$isbn.val('978-0-306-40615-7');
  1885. this.bv.validate();
  1886. expect(this.bv.isValidField('isbn')).toBeTruthy();
  1887. });
  1888. it('isbn13 invalid check digit', function() {
  1889. this.$isbn.val('978-0-306-40615-6');
  1890. this.bv.validate();
  1891. expect(this.bv.isValidField('isbn')).toEqual(false);
  1892. });
  1893. });
  1894. describe('isin', function() {
  1895. beforeEach(function() {
  1896. var html = [
  1897. '<div class="container">',
  1898. '<form class="form-horizontal" id="isinForm">',
  1899. '<div class="form-group">',
  1900. '<input type="text" name="isin" data-bv-isin />',
  1901. '</div>',
  1902. '</form>',
  1903. '</div>'
  1904. ].join('\n');
  1905. $(html).appendTo('body');
  1906. $('#isinForm').bootstrapValidator();
  1907. this.bv = $('#isinForm').data('bootstrapValidator');
  1908. this.$isin = this.bv.getFieldElements('isin');
  1909. });
  1910. afterEach(function() {
  1911. $('#isinForm').bootstrapValidator('destroy').parent().remove();
  1912. });
  1913. it('valid', function() {
  1914. var samples = ['US0378331005', 'AU0000XVGZA3', 'GB0002634946'];
  1915. for (var i in samples) {
  1916. this.$isin.val(samples[i]);
  1917. this.bv.validate();
  1918. expect(this.bv.isValidField('isin')).toBeTruthy();
  1919. }
  1920. });
  1921. it('invalid country code', function() {
  1922. this.$isin.val('AA0000XVGZA3');
  1923. this.bv.validate();
  1924. expect(this.bv.isValidField('isin')).toEqual(false);
  1925. });
  1926. it('contains only digits and alphabet', function() {
  1927. this.$isin.val('US12345ABC@#$');
  1928. this.bv.validate();
  1929. expect(this.bv.isValidField('isin')).toEqual(false);
  1930. });
  1931. it('invalid length', function() {
  1932. this.$isin.val('US1234567');
  1933. this.bv.validate();
  1934. expect(this.bv.isValidField('isin')).toEqual(false);
  1935. });
  1936. it('invalid check digit', function() {
  1937. this.$isin.val('US0378331004');
  1938. this.bv.validate();
  1939. expect(this.bv.isValidField('isin')).toEqual(false);
  1940. });
  1941. });
  1942. describe('ismn', function() {
  1943. beforeEach(function() {
  1944. var html = [
  1945. '<div class="container">',
  1946. '<form class="form-horizontal" id="ismnForm">',
  1947. '<div class="form-group">',
  1948. '<input type="text" name="ismn" data-bv-ismn />',
  1949. '</div>',
  1950. '</form>',
  1951. '</div>'
  1952. ].join('\n');
  1953. $(html).appendTo('body');
  1954. $('#ismnForm').bootstrapValidator();
  1955. this.bv = $('#ismnForm').data('bootstrapValidator');
  1956. this.$ismn = this.bv.getFieldElements('ismn');
  1957. });
  1958. afterEach(function() {
  1959. $('#ismnForm').bootstrapValidator('destroy').parent().remove();
  1960. });
  1961. it('valid start with M', function() {
  1962. this.$ismn.val('M230671187');
  1963. this.bv.validate();
  1964. expect(this.bv.isValidField('ismn')).toBeTruthy();
  1965. });
  1966. it('valid start with 979', function() {
  1967. this.$ismn.val('9790060115615');
  1968. this.bv.validate();
  1969. expect(this.bv.isValidField('ismn')).toBeTruthy();
  1970. });
  1971. it('valid contains spaces', function() {
  1972. this.$ismn.val('979 0 3452 4680 5');
  1973. this.bv.validate();
  1974. expect(this.bv.isValidField('ismn')).toBeTruthy();
  1975. });
  1976. it('valid contains dashes', function() {
  1977. this.$ismn.val('979-0-0601-1561-5');
  1978. this.bv.validate();
  1979. expect(this.bv.isValidField('ismn')).toBeTruthy();
  1980. });
  1981. it('invalid format', function() {
  1982. this.$ismn.val('N123456789');
  1983. this.bv.validate();
  1984. expect(this.bv.isValidField('ismn')).toEqual(false);
  1985. });
  1986. it('invalid check digit', function() {
  1987. this.$ismn.val('9790060115614');
  1988. this.bv.validate();
  1989. expect(this.bv.isValidField('ismn')).toEqual(false);
  1990. });
  1991. });
  1992. describe('issn', function() {
  1993. beforeEach(function() {
  1994. var html = [
  1995. '<div class="container">',
  1996. '<form class="form-horizontal" id="issnForm">',
  1997. '<div class="form-group">',
  1998. '<input type="text" name="issn" data-bv-issn />',
  1999. '</div>',
  2000. '</form>',
  2001. '</div>'
  2002. ].join('\n');
  2003. $(html).appendTo('body');
  2004. $('#issnForm').bootstrapValidator();
  2005. this.bv = $('#issnForm').data('bootstrapValidator');
  2006. this.$issn = this.bv.getFieldElements('issn');
  2007. });
  2008. afterEach(function() {
  2009. $('#issnForm').bootstrapValidator('destroy').parent().remove();
  2010. });
  2011. it('valid', function() {
  2012. var samples = ['0378-5955', '0024-9319', '0032-1478'];
  2013. for (var i in samples) {
  2014. this.$issn.val(samples[i]);
  2015. this.bv.validate();
  2016. expect(this.bv.isValidField('issn')).toBeTruthy();
  2017. }
  2018. });
  2019. it('not contains hyphen', function() {
  2020. this.$issn.val('03785955');
  2021. this.bv.validate();
  2022. expect(this.bv.isValidField('issn')).toEqual(false);
  2023. });
  2024. it('contains only digits, X', function() {
  2025. this.$issn.val('1234-566A');
  2026. this.bv.validate();
  2027. expect(this.bv.isValidField('issn')).toEqual(false);
  2028. });
  2029. it('invalid check sum', function() {
  2030. this.$issn.val('0032-147X');
  2031. this.bv.validate();
  2032. expect(this.bv.isValidField('issn')).toEqual(false);
  2033. });
  2034. });