describe('api', function() { // Override the options $.extend($.fn.bootstrapValidator.DEFAULT_OPTIONS, { feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' } }); beforeEach(function() { $([ '
', '
', '', '
', '
', '', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#apiForm').bootstrapValidator(); this.bv = $('#apiForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); this.$note = $('#apiForm').find('input[name="note"]'); }); afterEach(function() { $('#apiForm').bootstrapValidator('destroy').remove(); }); it('revalidateField()', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect(this.bv.isValidField('email')).toBeTruthy(); this.$email.val('invalid#email.address'); this.bv.revalidateField('email'); expect(this.bv.isValidField(this.$email)).toEqual(false); }); it('destroy()', function() { this.bv.destroy(); expect($('#apiForm').data('bootstrapValidator')).toBeUndefined(); expect($('#apiForm').find('i[data-bv-icon-for]').length).toEqual(0); expect($('#apiForm').find('.help-block[data-bv-for]').length).toEqual(0); expect($('#apiForm').find('.has-feedback').length).toEqual(0); expect($('#apiForm').find('.has-success').length).toEqual(0); expect($('#apiForm').find('.has-error').length).toEqual(0); expect($('#apiForm').find('[data-bv-field]').length).toEqual(0); }); it('getOptions()', function() { // Form options expect(this.bv.getOptions().feedbackIcons.valid).toEqual('glyphicon glyphicon-ok'); // Field options expect(this.bv.getOptions('username', 'stringlength')).toBeNull(); expect(this.bv.getOptions('username', 'stringlength', 'min')).toBeNull(); expect(this.bv.getOptions('username', 'stringLength')).toBeDefined(); expect(this.bv.getOptions('username', 'stringLength', 'min')).toEqual('8'); expect(this.bv.getOptions('username', 'stringlength', 'max')).toBeNull(); }); // #1014 it('isValidField()', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect(this.bv.isValidField(this.$note)).toBeTruthy(); expect(this.bv.isValidField(this.$email)).toBeTruthy(); }); // #1014 it('validateField()', function() { this.$email.val('email@domain.com'); this.bv.validateField(this.$email); this.bv.validateField(this.$note); expect(this.bv.isValidField(this.$email)).toBeTruthy(); expect(this.bv.isValidField(this.$note)).toBeTruthy(); }); }); describe('container form option', function() { beforeEach(function() { $([ '
', '
', '', '
', '', '
', '
', '', '
', '
', '
', '
' ].join('')).appendTo('body'); }); afterEach(function() { $('#containerForm').bootstrapValidator('destroy').remove(); }); it('form container declarative', function() { $('#containerForm') .attr('data-bv-container', '#errors') .bootstrapValidator(); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); expect($('#errors').find('.help-block').length).toBeGreaterThan(0); this.$firstName.val('First'); this.$lastName.val(''); this.bv.validate(); expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toEqual(0); expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toBeGreaterThan(0); }); it('form container programmatically', function() { $('#containerForm').bootstrapValidator({ container: '#errors' }); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); expect($('#errors').find('.help-block').length).toBeGreaterThan(0); this.$firstName.val(''); this.$lastName.val('Last'); this.bv.validate(); expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toBeGreaterThan(0); expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toEqual(0); this.bv.resetForm(); this.$firstName.val('First'); this.$lastName.val('Last'); this.bv.validate(); expect($('#errors').find('.help-block:visible').length).toEqual(0); }); }); describe('container field option', function() { beforeEach(function() { $([ '
', '
', '', '
', '', '', '
', '
', '', '', '
', '
', '
' ].join('')).appendTo('body'); $('#containerForm').bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { lastName: { container: '.lastNameMessage' } } }); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); }); afterEach(function() { $('#containerForm').bootstrapValidator('destroy').remove(); }); it('field container declarative', function() { expect($.trim($('#firstNameMessage').text())).toEqual('The first name is required'); expect($.trim($('.lastNameMessage').text())).toEqual('The last name is required'); }); it('field container programmatically', function() { this.$firstName.val('First'); this.$lastName.val(''); this.bv.validate(); expect($('#firstNameMessage').find('.help-block:visible').length).toEqual(0); expect($('.lastNameMessage').find('.help-block:visible').length).toBeGreaterThan(0); this.bv.resetForm(); this.$firstName.val(''); this.$lastName.val('Last'); this.bv.validate(); expect($('#firstNameMessage').find('.help-block:visible').length).toBeGreaterThan(0); expect($('.lastNameMessage').find('.help-block:visible').length).toEqual(0); }); }); describe('container tooltip/popover', function() { beforeEach(function() { $([ '
', '
', '', '
', '', '
', '
', '', '
', '
', '
', '
' ].join('')).appendTo('body'); }); afterEach(function() { $('#containerForm').bootstrapValidator('destroy').remove(); }); it('container declarative', function() { $('#containerForm') .attr('data-bv-container', 'tooltip') .find('[name="lastName"]') .attr('data-bv-container', 'popover') .end() .bootstrapValidator(); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined(); expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip'); expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined(); expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover'); this.bv.resetForm(); this.$firstName.val('First'); this.$lastName.val('Last'); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeUndefined(); expect(this.$lastName.parent().find('i').data('bs.popover')).toBeUndefined(); }); it('container programmatically', function() { $('#containerForm').bootstrapValidator({ container: 'tooltip', fields: { lastName: { container: 'popover' } } }); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined(); expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip'); expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined(); expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover'); this.bv.resetForm(); this.$firstName.val('First'); this.$lastName.val('Last'); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeUndefined(); expect(this.$lastName.parent().find('i').data('bs.popover')).toBeUndefined(); }); // #991: Validate once when setting trigger: blur, container: tooltip it('trigger: blur, container: tooltip', function() { $('#containerForm').bootstrapValidator({ container: 'tooltip', trigger: 'blur', fields: { firstName: { validators: { stringLength: { min: 5, message: 'The first name must be more than 5 characters' }, notEmpty: { message: 'The first name is required' }, regexp: { regexp: /^[a-z]+$/i, message: 'The first name must consist of a-z, A-Z characters only' } } }, lastName: { validators: { stringLength: { min: 5, message: 'The last name must be more than 5 characters' }, notEmpty: { message: 'The last name is required' }, regexp: { regexp: /^[a-z]+$/i, message: 'The last name must consist of a-z, A-Z characters only' } } } } }); this.bv = $('#containerForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); this.$firstName.val('').trigger('blur'); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined(); expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip'); expect(this.$firstName.parent().find('i').data('bs.tooltip').getTitle()).toEqual('The first name is required'); this.bv.resetForm(); this.$firstName.val('@not#valid'); this.$lastName.val('').focus(); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined(); expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip'); expect(this.$firstName.parent().find('i').data('bs.tooltip').getTitle()).toEqual('The first name must consist of a-z, A-Z characters only'); this.bv.resetForm(); this.$firstName.val('Phuo'); this.$lastName.val('').focus(); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined(); expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip'); expect(this.$firstName.parent().find('i').data('bs.tooltip').getTitle()).toEqual('The first name must be more than 5 characters'); this.bv.resetForm(); this.$firstName.val('Phuoc'); this.$lastName.val('').focus(); this.bv.validate(); expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeUndefined(); }); }); describe('dynamic fields', function() { beforeEach(function() { $([ '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#dynamicForm').bootstrapValidator({ fields: { fullName: { validators: { notEmpty: { message: 'The full name is required and cannot be empty' }, stringLength: { min: 8, max: 40, message: 'The full name must be more than %s and less than %s characters long' }, regexp: { enabled: false, regexp: /^[a-zA-Z\s]+$/, message: 'The full name can only consist of alphabetical, number, and space' } } }, // #725: Note that the email field isn't available in the form yet email: { validators: { emailAddress: { message: 'The email address is not valid' } } } } }); this.bv = $('#dynamicForm').data('bootstrapValidator'); this.$fullName = this.bv.getFieldElements('fullName'); }); afterEach(function() { $('#dynamicForm').bootstrapValidator('destroy').remove(); }); // https://github.com/nghuuphuoc/bootstrapvalidator/pull/725 it('adding field [does not exist but is already set in "fields" option]', function() { var $div = $('
').addClass('form-group').appendTo($('#dynamicForm')); $email = $('') .attr('type', 'text') .addClass('form-control') .attr('name', 'email') .appendTo($div); this.bv.addField('email'); this.$fullName.val('Phuoc Nguyen'); $email.val('not valid@email'); this.bv.validate(); expect(this.bv.isValidField('email')).toBeFalsy(); expect(this.bv.isValid()).toBeFalsy(); this.bv.resetForm(); $email.val('valid@email.com'); this.bv.validate(); expect(this.bv.isValidField('email')).toBeTruthy(); expect(this.bv.isValid()).toBeTruthy(); }); }); describe('enable validators', function() { beforeEach(function() { $([ '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#enableForm').bootstrapValidator({ fields: { fullName: { validators: { notEmpty: { message: 'The full name is required and cannot be empty' }, stringLength: { min: 8, max: 40, message: 'The full name must be more than %s and less than %s characters long' }, regexp: { enabled: false, regexp: /^[a-zA-Z\s]+$/, message: 'The full name can only consist of alphabetical, number, and space' } } } } }); this.bv = $('#enableForm').data('bootstrapValidator'); this.$fullName = this.bv.getFieldElements('fullName'); }); afterEach(function() { $('#enableForm').bootstrapValidator('destroy').remove(); }); it('enable all validators', function() { this.$fullName.val('@ $full N@m3'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); this.bv.resetForm(); this.$fullName.val('Contain#$@'); this.bv.enableFieldValidators('fullName', true); this.bv.validate(); expect(this.bv.isValidField('fullName')).toEqual(false); expect(this.bv.isValid()).toEqual(false); }); it('disable all validators', function() { this.bv.resetForm(); this.bv.enableFieldValidators('fullName', false); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('enabled option particular validator', function() { this.$fullName.val('Contain@#$'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); var messages = this.bv.getMessages('fullName'); expect(messages.length).toEqual(0); }); it('enable particular validators', function() { // Enable stringLength validator this.bv.resetForm(); this.bv.enableFieldValidators('fullName', true, 'stringLength'); this.bv.enableFieldValidators('fullName', true, 'regexp'); this.$fullName.val('Full@'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); var messages = this.bv.getMessages('fullName'); expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1); expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1); }); it('disable particular validators', function() { // Disable stringLength validator this.bv.enableFieldValidators('fullName', false, 'stringLength'); this.$fullName.val('Full'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); var messages = this.bv.getMessages('fullName'); expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toEqual(-1); // Disable regexp validator this.bv.enableFieldValidators('fullName', false, 'regexp'); this.$fullName.val('Special@#$'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); var messages = this.bv.getMessages('fullName'); expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toEqual(-1); }); }); TestSuite = $.extend({}, TestSuite, { Event: { onEmailValid: function(e, data) { $('#msg').html('TestSuite.Event.onEmailValid() called, ' + data.field + ' is valid'); }, onEmailInvalid: function(e, data) { $('#msg').html('TestSuite.Event.onEmailInvalid() called, ' + data.field + ' is invalid'); }, onEmailStatus: function(e, data) { $('#status').html('TestSuite.Event.onEmailStatus() called; status = ' + data.status); }, onFormValid: function(e) { $('#msg').html('TestSuite.Event.onFormValid() called, form ' + $(e.target).attr('id') + ' is valid'); }, onFormInvalid: function(e) { $('#msg').html('TestSuite.Event.onFormInvalid() called, form ' + $(e.target).attr('id') + ' is invalid'); } } }); // --- // Form events // --- function onFormValid(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' is valid'); }; function onFormInvalid(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' is invalid'); }; describe('event form attribute callback global', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator(); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call data-bv-onsuccess', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm is valid'); }); it('call data-bv-onerror', function() { this.$email.val('a@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm is invalid'); }); }); describe('event form attribute callback namespace', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator(); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call data-bv-onsuccess', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.Event.onFormValid() called, form eventForm is valid'); }); it('call data-bv-onerror', function() { this.$email.val('just"not"right@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.Event.onFormInvalid() called, form eventForm is invalid'); }); }); describe('event form trigger', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm') .bootstrapValidator() .on('success.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event'); }) .on('error.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event'); }); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('trigger success.form.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm triggered success.form.bv event'); }); it('trigger error.form.bv', function() { this.$email.val('this is"not\\allowed@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm triggered error.form.bv event'); }); }); describe('event form programmatically', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator({ onSuccess: function(e) { $('#msg').html('onSuccess() called'); }, onError: function(e) { $('#msg').html('onError() called'); } }); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call onSuccess()', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('onSuccess() called'); }); it('call onError()', function() { this.$email.val('Abc.example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('onError() called'); }); }); // --- // Field events // --- function onEmailValid(e, data) { $('#msg').html(data.field + ' is valid'); }; function onEmailInvalid(e, data) { $('#msg').html(data.field + ' is invalid'); }; function onEmailStatus(e, data) { $('#status').html(data.status); }; describe('event field attribute callback global', function() { beforeEach(function() { $([ '
', '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator(); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call data-bv-onsuccess', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('email is valid'); expect($('#status').html()).toEqual(this.bv.STATUS_VALID); }); it('call data-bv-onerror', function() { this.$email.val('A@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('email is invalid'); expect($('#status').html()).toEqual(this.bv.STATUS_INVALID); }); }); describe('event field attribute callback namespace', function() { beforeEach(function() { $([ '
', '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator(); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call data-bv-onsuccess', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.Event.onEmailValid() called, email is valid'); expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_VALID); }); it('call data-bv-onerror', function() { this.$email.val('a"b(c)d,e:f;gi[j\\k]l@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.Event.onEmailInvalid() called, email is invalid'); expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_INVALID); }); }); describe('event field trigger', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm') .bootstrapValidator() .on('success.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered success.field.bv on ' + data.field); }) .on('error.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered error.field.bv on ' + data.field); }); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('trigger success.field.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered success.field.bv on email'); }); it('trigger error.field.bv', function() { this.$email.val('just"not"right@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered error.field.bv on email'); }); }); describe('event field programmatically', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator({ fields: { email: { onSuccess: function(e, data) { $('#msg').html('onSuccess() called'); }, onError: function(e, data) { $('#msg').html('onError() called'); } } } }); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call onSuccess()', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('onSuccess() called'); }); it('call onError()', function() { this.$email.val('this is"not\\allowed@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('onError() called'); }); }); // --- // Modifying default events // --- describe('event form trigger with default events', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm1') .bootstrapValidator() .on('bv.form.success', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event'); }) .on('success.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event'); }) .on('bv.form.error', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event'); }) .on('error.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event'); }); this.bv = $('#eventForm1').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm1').bootstrapValidator('destroy').remove(); }); it('does not trigger bv.form.success', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.success event'); }); it('triggers success.form.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm1 triggered success.form.bv event'); }); it('does not trigger bv.form.error', function() { this.$email.val('A@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.error event'); }); it('triggers error.form.bv', function() { this.$email.val('A@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm1 triggered error.form.bv event'); }); }); describe('event field trigger with default events', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm3') .bootstrapValidator() .on('success.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered success.field.bv on ' + data.field); }) .on('error.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered error.field.bv on ' + data.field); }) .on('bv.field.success', '[name="email"]', function(e, data) { $('#msg').html('triggered bv.field.success on ' + data.field); }) .on('bv.field.error', '[name="email"]', function(e, data) { $('#msg').html('triggered bv.field.error on ' + data.field); }); this.bv = $('#eventForm3').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm3').bootstrapValidator('destroy').remove(); }); it('triggers success.field.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered success.field.bv on email'); }); it('does not trigger bv.field.success', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('triggered bv.field.success on email'); }); it('does not trigger error.field.bv', function() { this.$email.val('just"not"right@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered error.field.bv on email'); }); it('triggers bv.field.error', function() { this.$email.val('just"not"right@example.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('triggered bv.field.error on email'); }); }); describe('event form trigger with events changed', function() { var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS; beforeEach(function() { $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, { events: { formInit: 'init.form.bv', formError: 'bv.form.error', formSuccess: 'bv.form.success', fieldAdded: 'added.field.bv', fieldRemoved: 'removed.field.bv', fieldInit: 'init.field.bv', fieldError: 'bv.field.error', fieldSuccess: 'bv.field.success', fieldStatus: 'status.field.bv', validatorError: 'bv.validator.error', validatorSuccess: 'success.validator.bv' } }); $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm2') .bootstrapValidator() .on('bv.form.success', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event'); }) .on('success.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event'); }) .on('bv.form.error', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event'); }) .on('error.form.bv', function(e) { $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event'); }); this.bv = $('#eventForm2').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions; $('#eventForm2').bootstrapValidator('destroy').remove(); }); it('triggers bv.form.success', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.success event'); }); it('does not trigger success.form.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('form eventForm2 triggered success.form.bv event'); }); it('triggers bv.form.error', function() { spyOn(window, 'onerror'); this.$email.val('this is"not\\allowed@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.error event'); expect(window.onerror).not.toHaveBeenCalled(); }); }); describe('event field trigger with events changed', function() { var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS; beforeEach(function() { $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, { events: { formInit: 'init.form.bv', formError: 'bv.form.error', formSuccess: 'bv.form.success', fieldAdded: 'added.field.bv', fieldRemoved: 'removed.field.bv', fieldInit: 'init.field.bv', fieldError: 'bv.field.error', fieldSuccess: 'bv.field.success', fieldStatus: 'status.field.bv', validatorError: 'bv.validator.error', validatorSuccess: 'success.validator.bv' } }); $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm4') .bootstrapValidator() .on('success.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered success.field.bv on ' + data.field); }) .on('error.field.bv', '[name="email"]', function(e, data) { $('#msg').html('triggered error.field.bv on ' + data.field); }) .on('bv.field.success', '[name="email"]', function(e, data) { $('#msg').html('triggered bv.field.success on ' + data.field); }) .on('bv.field.error', '[name="email"]', function(e, data) { $('#msg').html('triggered bv.field.error on ' + data.field); }); this.bv = $('#eventForm4').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions; $('#eventForm4').bootstrapValidator('destroy').remove(); }); it('triggers success.field.bv', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('triggered success.field.bv on email'); }); it('does not trigger bv.field.success', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered bv.field.success on email'); }); it('does not trigger error.field.bv', function() { this.$email.val('Abc.example.com'); this.bv.validate(); expect($('#msg').html()).not.toEqual('triggered error.field.bv on email'); }); it('triggers bv.field.error', function() { spyOn(window, 'onerror'); this.$email.val('Abc.example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('triggered bv.field.error on email'); expect(window.onerror).not.toHaveBeenCalled(); }); }); // --- // Validator events // --- function onEmailAddressValidatorSuccess(e, data) { $('#msg').html(data.validator + ' validator passed'); }; function onEmailAddressValidatorError(e, data) { $('#msg').html(data.validator + ' validator did not pass'); }; describe('event validator declarative', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator(); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('trigger data-bv-emailaddress-onsuccess', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('emailAddress validator passed'); }); it('trigger data-bv-emailaddress-onerror', function() { this.$email.val('A@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('emailAddress validator did not pass'); }); }); describe('event validator programmatically', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#eventForm').bootstrapValidator({ fields: { email: { validators: { emailAddress: { onSuccess: function(e, data) { $('#msg').html('emailAddress validator: onSuccess() called'); }, onError: function(e, data) { $('#msg').html('emailAddress validator: onError() called'); }, message: 'The email address is not valid' } } } } }); this.bv = $('#eventForm').data('bootstrapValidator'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#eventForm').bootstrapValidator('destroy').remove(); }); it('call onSuccess()', function() { this.$email.val('email@domain.com'); this.bv.validate(); expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called'); }); it('call onError()', function() { this.$email.val('A@b@c@example.com'); this.bv.validate(); expect($('#msg').html()).toEqual('emailAddress validator: onError() called'); }); }); describe('excluded', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
', '', '
', '
', '
' ].join('')).appendTo('body'); $('#excludedForm').bootstrapValidator(); this.bv = $('#excludedForm').data('bootstrapValidator'); this.$username = this.bv.getFieldElements('username'); this.$email = this.bv.getFieldElements('email'); }); afterEach(function() { $('#excludedForm').bootstrapValidator('destroy').parent().remove(); }); it('excluded form declarative', function() { this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$username.val('your_user_name'); this.$email.val(''); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('excluded form programmatically', function() { this.bv.destroy(); $('#excludedForm').removeAttr('data-bv-excluded'); $('#excludedForm').bootstrapValidator({ excluded: '[name="username"]' }); this.bv = $('#excludedForm').data('bootstrapValidator'); this.$username = this.bv.getFieldElements('username'); this.$email = this.bv.getFieldElements('email'); this.$username.val(''); this.$email.val('invalid#email.com'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$email.val('valid@email.com'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('excluded field declarative', function() { this.bv.destroy(); $('#excludedForm').removeAttr('data-bv-excluded'); $('#excludedForm').find('[name="username"]').attr('data-bv-excluded', 'true'); $('#excludedForm').find('[name="email"]').attr('data-bv-excluded', 'false'); this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator'); this.$username = this.bv.getFieldElements('username'); this.$email = this.bv.getFieldElements('email'); this.$username.val(''); this.$email.val(''); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$email.val('invalid#email.com'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$email.val('valid@email.com'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('excluded field programmatically true/false', function() { this.bv.destroy(); $('#excludedForm').removeAttr('data-bv-excluded'); $('#excludedForm').bootstrapValidator({ fields: { username: { excluded: true }, email: { excluded: false } } }); this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator'); this.$username = this.bv.getFieldElements('username'); this.$email = this.bv.getFieldElements('email'); this.$username.val(''); this.$email.val(''); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$email.val('invalid#email.com'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$email.val('valid@email.com'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('excluded field programmatically "true"/"false"', function() { this.bv.destroy(); $('#excludedForm').removeAttr('data-bv-excluded'); $('#excludedForm').bootstrapValidator({ fields: { username: { excluded: 'false' }, email: { excluded: 'true' } } }); this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator'); this.$username = this.bv.getFieldElements('username'); this.$email = this.bv.getFieldElements('email'); this.$username.val(''); this.$email.val('valid@email.com'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$username.val('your_user_name'); this.$email.val('invalid#email.com'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); }); describe('group option', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '
', '', '
', '', '
', '
', '
' ].join('')).appendTo('body'); $('#groupForm').bootstrapValidator({ fields: { firstName: { group: '.firstNameGroup', validators: { notEmpty: { message: 'The first name is required and cannot be empty' } } }, lastName: { validators: { notEmpty: { message: 'The last name is required and cannot be empty' } } }, username: { validators: { notEmpty: { message: 'The username is required and cannot be empty' }, stringLength: { min: 6, max: 30, message: 'The username must be more than 6 and less than 30 characters long' }, regexp: { regexp: /^[a-zA-Z0-9_\.]+$/, message: 'The username can only consist of alphabetical, number, dot and underscore' } } } } }); this.bv = $('#groupForm').data('bootstrapValidator'); this.$firstName = this.bv.getFieldElements('firstName'); this.$lastName = this.bv.getFieldElements('lastName'); this.$username = this.bv.getFieldElements('username'); }); afterEach(function() { $('#groupForm').bootstrapValidator('destroy').remove(); }); it('group default', function() { this.$username.val('123@#$'); this.bv.validate(); expect(this.$username.parents('.form-group').hasClass('has-error')).toBeTruthy(); expect(this.$username.parents('.form-group').hasClass('has-success')).toEqual(false); this.bv.resetForm(); this.$username.val('validUser.Name'); this.bv.validate(); expect(this.$username.parents('.form-group').hasClass('has-success')).toBeTruthy(); expect(this.$username.parents('.form-group').hasClass('has-error')).toEqual(false); }); it('group programmatically', function() { this.$firstName.val(''); this.bv.validate(); expect(this.$firstName.parents('.firstNameGroup').hasClass('has-error')).toBeTruthy(); expect(this.$firstName.parents('.firstNameGroup').hasClass('has-success')).toEqual(false); expect(this.$firstName.parents('.form-group').hasClass('has-error')).toEqual(false); }); it('group declarative', function() { this.$firstName.val('First'); this.$lastName.val('Last'); this.bv.validate(); expect(this.$lastName.parents('.lastNameGroup').hasClass('has-success')).toBeTruthy(); expect(this.$lastName.parents('.lastNameGroup').hasClass('has-error')).toEqual(false); expect(this.$lastName.parents('.form-group').hasClass('has-success')).toEqual(false); expect(this.$lastName.parents('.form-group').hasClass('has-error')).toEqual(false); }); }); describe('i18n', function() { beforeEach(function() { $([ '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '
', '
' ].join('')).appendTo('body'); $('#i18nForm').bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { fullName: { validators: { notEmpty: {}, stringCase: { 'case': 'upper' } } }, username: { validators: { notEmpty: {}, stringLength: { min: 6, max: 20 }, regexp: { regexp: /^[a-zA-Z0-9_\.]+$/ }, different: { field: 'password' } } }, email: { validators: { emailAddress: {} } }, password: { validators: { notEmpty: {}, identical: { field: 'confirmPassword' }, different: { field: 'username' } } }, confirmPassword: { validators: { notEmpty: {}, identical: { field: 'password' }, different: { field: 'username' } } }, age: { validators: { notEmpty: {}, digits: {}, greaterThan: { value: 18 }, lessThan: { value: 100 } } }, website: { validators: { notEmpty: {}, uri: {} } }, phoneNumber: { validators: { notEmpty: {}, digits: {}, phone: { country: 'US' } } }, gender: { validators: { notEmpty: {} } }, 'languages[]': { validators: { notEmpty: {} } }, 'programs[]': { validators: { choice: { min: 2, max: 4 } } } } }); this.bv = $('#i18nForm').data('bootstrapValidator'); this.$fullName = this.bv.getFieldElements('fullName'); this.$email = this.bv.getFieldElements('email'); this.$userName = this.bv.getFieldElements('username'); this.$password = this.bv.getFieldElements('password'); this.$confirm = this.bv.getFieldElements('confirmPassword'); this.$age = this.bv.getFieldElements('age'); this.$website = this.bv.getFieldElements('website'); this.$phone = this.bv.getFieldElements('phoneNumber'); this.$program = this.bv.getFieldElements('programs[]'); }); afterEach(function() { $('#i18nForm').bootstrapValidator('destroy').remove(); }); it('default message', function() { var format = $.fn.bootstrapValidator.helpers.format, i18n = $.fn.bootstrapValidator.i18n; this.bv.validate(); expect(this.bv.getMessages(this.$fullName, 'notEmpty')[0]).toEqual(i18n.notEmpty['default']); this.$fullName.val('lowerName'); this.bv.revalidateField('fullName'); expect(this.bv.getMessages('fullName', 'stringCase')[0]).toEqual(i18n.stringCase.upper); this.bv.resetForm(); this.$userName.val('123'); this.bv.validate(); expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(format(i18n.stringLength.between, [6, 20])); this.bv.resetForm(); this.$userName.val('contain@#$'); this.bv.validate(); expect(this.bv.getMessages(this.$userName, 'regexp')[0]).toEqual(i18n.regexp['default']); this.bv.resetForm(); this.$userName.val('validUserName'); this.$password.val('validUserName'); this.bv.validate(); expect(this.bv.getMessages('username', 'different')[0]).toEqual(i18n.different['default']); this.bv.resetForm(); this.$email.val('A@b@c@example.com'); this.bv.validate(); expect(this.bv.getMessages(this.$email, 'emailAddress')[0]).toEqual(i18n.emailAddress['default']); this.bv.resetForm(); this.$password.val('@S3cur3P@@w0rd'); this.$confirm.val('notMatch'); this.bv.validate(); expect(this.bv.getMessages('password', 'identical')[0]).toEqual(i18n.identical['default']); this.bv.resetForm(); this.$age.val('notDigit'); this.bv.validate(); expect(this.bv.getMessages('age', 'digits')[0]).toEqual(i18n.digits['default']); this.bv.resetForm(); this.$age.val(10); this.bv.validate(); expect(this.bv.getMessages(this.$age, 'greaterThan')[0]).toEqual(format(i18n.greaterThan['default'], 18)); this.bv.resetForm(); this.$age.val(120); this.bv.validate(); expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual(format(i18n.lessThan['default'], 100)); this.bv.resetForm(); this.$website.val('http://invalidWebsite'); this.bv.validate(); expect(this.bv.getMessages('website', 'uri')[0]).toEqual(i18n.uri['default']); this.bv.resetForm(); this.$phone.val('123456'); this.bv.validate(); expect(this.bv.getMessages('phoneNumber', 'phone')[0]).toEqual(format(i18n.phone.country, i18n.phone.countries['US'])); this.bv.resetForm(); this.$program.eq(0).prop('checked', 'checked'); this.bv.validate(); expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4])); this.bv.resetForm(); this.$program.prop('checked', 'checked'); this.bv.validate(); expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4])); }); }); describe('input', function() { beforeEach(function(done) { $([ '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#stringLengthForm').bootstrapValidator(); this.bv = $('#stringLengthForm').data('bootstrapValidator'); this.$textCharMaxLength = this.bv.getFieldElements('textCharMaxLength'); this.$textareaCharMaxLength = this.bv.getFieldElements('textareaCharMaxLength'); this.$textUTF8BytesMaxLength = this.bv.getFieldElements('textUTF8BytesMaxLength'); this.$textareaUTF8BytesMaxLength = this.bv.getFieldElements('textareaUTF8BytesMaxLength'); this.$textCharMinLength = this.bv.getFieldElements('textCharMinLength'); this.$textareaCharMinLength = this.bv.getFieldElements('textareaCharMinLength'); this.$textUTF8BytesMinLength = this.bv.getFieldElements('textUTF8BytesMinLength'); this.$textareaUTF8BytesMinLength = this.bv.getFieldElements('textareaUTF8BytesMinLength'); this.$textCharMinMaxLength = this.bv.getFieldElements('textCharMinMaxLength'); this.$textareaCharMinMaxLength = this.bv.getFieldElements('textareaCharMinMaxLength'); this.$textUTF8BytesMinMaxLength = this.bv.getFieldElements('textUTF8BytesMinMaxLength'); this.$textareaUTF8BytesMinMaxLength = this.bv.getFieldElements('textareaUTF8BytesMinMaxLength'); }); afterEach(function() { $('#stringLengthForm').bootstrapValidator('destroy').remove(); }); it('Valid max lengths', function() { this.$textCharMaxLength.val('123456789♥'); this.$textareaCharMaxLength.val('123456789♥'); this.$textUTF8BytesMaxLength.val('1234567♥'); this.$textareaUTF8BytesMaxLength.val('1234567♥'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('Valid min lengths', function() { this.$textCharMinLength.val('1234♥'); this.$textareaCharMinLength.val('1234♥'); this.$textUTF8BytesMinLength.val('12♥'); this.$textareaUTF8BytesMinLength.val('12♥'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('Valid min and max lengths', function() { this.$textCharMinMaxLength.val('1234♥'); this.$textareaCharMinMaxLength.val('1234♥'); this.$textUTF8BytesMinMaxLength.val('12♥'); this.$textareaUTF8BytesMinMaxLength.val('12♥'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); this.bv.resetForm(); this.$textCharMinMaxLength.val('123456789♥'); this.$textareaCharMinMaxLength.val('123456789♥'); this.$textUTF8BytesMinMaxLength.val('1234567♥'); this.$textareaUTF8BytesMinMaxLength.val('1234567♥'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('Invalid max lengths', function() { this.$textCharMaxLength.val('1234567890♥'); // 11 chars when max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaCharMaxLength.val('1234567890♥'); // 11 chars when max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textUTF8BytesMaxLength.val('12345678♥'); // 11 UTF-8 bytes when max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaUTF8BytesMaxLength.val('12345678♥'); // 11 UTF-8 bytes when max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); it('Invalid min lengths', function() { this.$textCharMinLength.val('123♥'); // 4 chars when min is 5 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaCharMinLength.val('123♥'); // 4 chars when min is 5 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textUTF8BytesMinLength.val('1♥'); // 4 UTF-8 bytes when min is 5 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaUTF8BytesMinLength.val('1♥'); // 4 UTF-8 bytes when min is 5 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); it('Invalid min and max lengths', function() { this.$textCharMinMaxLength.val('123♥'); // 4 chars when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaCharMinMaxLength.val('123♥'); // 4 chars when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textUTF8BytesMinMaxLength.val('1♥'); // 4 UTF-8 bytes when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaUTF8BytesMinMaxLength.val('1♥'); // 4 UTF-8 bytes when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textCharMinMaxLength.val('1234567890♥'); // 11 chars when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaCharMinMaxLength.val('1234567890♥'); // 11 chars when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textUTF8BytesMinMaxLength.val('12345678♥'); // 11 UTF-8 bytes when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textareaUTF8BytesMinMaxLength.val('12345678♥'); // 11 UTF-8 bytes when min is 5 and max is 10 this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); it('trim option', function() { this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', false); this.$textCharMaxLength.val(''); this.bv.validate(); expect(this.bv.isValid()).toEqual(true); this.bv.resetForm(); this.$textCharMaxLength.val(' '); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$textCharMaxLength.val('1234567890 '); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', true); this.bv.resetForm(); this.$textCharMaxLength.val(' '); this.bv.validate(); expect(this.bv.isValid()).toEqual(true); this.bv.resetForm(); this.$textCharMaxLength.val(' '); this.bv.validate(); expect(this.bv.isValid()).toEqual(true); this.bv.resetForm(); this.$textCharMaxLength.val(' 0123456789 '); this.bv.validate(); expect(this.bv.isValid()).toEqual(true); this.bv.resetForm(); this.$textCharMaxLength.val(' 01234567890 '); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); }); describe('uri', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '
' ].join('\n')).appendTo('body'); $('#uriForm').bootstrapValidator(); this.bv = $('#uriForm').data('bootstrapValidator'); this.$uri = this.bv.getFieldElements('uri'); }); afterEach(function() { $('#uriForm').bootstrapValidator('destroy').remove(); }); var validGlobalURIs = [ 'http://foo.com/blah_blah', 'http://foo.com/blah_blah', 'http://foo.com/blah_blah/', 'http://foo.com/blah_blah_(wikipedia)', 'http://foo.com/blah_blah_(wikipedia)_(again)', 'http://www.example.com/wpstyle/?p=364', 'https://www.example.com/foo/?bar=baz&inga=42&quux', 'http://✪df.ws/123', 'http://userid:password@example.com:8080', 'http://userid:password@example.com:8080/', 'http://userid@example.com', 'http://userid@example.com/', 'http://userid@example.com:8080', 'http://userid@example.com:8080/', 'http://userid:password@example.com', 'http://userid:password@example.com/', 'http://142.42.1.1/', 'http://142.42.1.1:8080/', 'http://➡.ws/䨹', 'http://⌘.ws', 'http://⌘.ws/', 'http://foo.com/blah_(wikipedia)#cite-1', 'http://foo.com/blah_(wikipedia)_blah#cite-1', 'http://foo.com/unicode_(✪)_in_parens', 'http://foo.com/(something)?after=parens', 'http://☺.damowmow.com/', 'http://code.google.com/events/#&product=browser', 'http://j.mp', 'ftp://foo.bar/baz', 'http://foo.bar/?q=Test%20URL-encoded%20stuff', 'http://مثال.إختبار', 'http://例子.测试', 'http://उदाहरण.परीक्षा', "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com", 'http://1337.net', 'http://a.b-c.de', 'http://223.255.255.254' ]; var invalidGlobalURIs = [ 'http://', 'http://.', 'http://..', 'http://../', 'http://?', 'http://??', 'http://??/', 'http://#', 'http://##', 'http://##/', 'http://foo.bar?q=Spaces should be encoded', '//', '//a', '///a', '///', 'http:///a', 'foo.com', 'rdar://1234', 'h://test', 'http:// shouldfail.com', ':// should fail', 'http://foo.bar/foo(bar)baz quux', 'ftps://foo.bar/', 'http://-error-.invalid/', 'http://a.b--c.de/', 'http://-a.b.co', 'http://a.b-.co', 'http://.www.foo.bar/', 'http://www.foo.bar./', 'http://.www.foo.bar./' ]; var localURIs = [ 'http://intranetsite', 'http://intranetsite/test', 'http://intranetsite:80', 'http://intranetsite:80/test', 'http://user:pass@intranetsite', 'http://user:pass@intranetsite/test', 'http://user:pass@intranetsite:80', 'http://user:pass@intranetsite:80/test', 'http://10.1.1.0', 'http://10.1.1.255', 'http://10.1.1.1', 'http://10.1.1.254', 'http://127.0.0.1', 'http://192.168.0.1', 'http://0.0.0.0', 'http://224.1.1.1', 'http://1.1.1.1.1', 'http://123.123.123', 'http://3628126748' ]; it('Valid URIs (allowLocal=false)', function() { var me = this; $.each(validGlobalURIs, function(index, uri) { me.bv.resetForm(); me.$uri.val(uri); me.bv.validate(); expect(me.bv.isValid()).toBeTruthy(); }); }); it('Invalid URIs (allowLocal=false)', function() { var me = this; $.each(invalidGlobalURIs.concat(localURIs), function(index, uri) { me.bv.resetForm(); me.$uri.val(uri); me.bv.validate(); expect(me.bv.isValid()).toEqual(false); }); }); it('Valid URIs (allowLocal=true)', function() { var me = this; me.bv.updateOption('uri', 'uri', 'allowLocal', true); $.each(validGlobalURIs.concat(localURIs), function(index, uri) { me.bv.resetForm(); me.$uri.val(uri); me.bv.validate(); expect(me.bv.isValid()).toBeTruthy(); }); }); it('Invalid URIs (allowLocal=true)', function() { var me = this; me.bv.updateOption('uri', 'uri', 'allowLocal', true); $.each(invalidGlobalURIs, function(index, uri) { me.bv.resetForm(); me.$uri.val(uri); me.bv.validate(); expect(me.bv.isValid()).toEqual(false); }); }); }); describe('vat', function() { beforeEach(function() { $([ '
', '
', '', '
', '
', '', '
', '
', ].join('\n')).appendTo('body'); $('#vatForm').bootstrapValidator(); /** * @type {BootstrapValidator} */ this.bv = $('#vatForm').data('bootstrapValidator'); this.$country = this.bv.getFieldElements('country'); this.$vat = this.bv.getFieldElements('vat'); }); afterEach(function() { $('#vatForm').bootstrapValidator('destroy').remove(); }); it('dynamic country', function() { this.$vat.attr('data-bv-vat-country', 'country'); this.bv.destroy(); this.bv = $('#vatForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('AT'); this.$vat.val('ATU13585627'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); this.bv.resetForm(); this.$country.val('BG'); this.$vat.val('BE0428759497'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$country.val('BE'); this.$vat.val('BE431150351'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); it('Austrian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'AT'); // Valid samples var validSamples = ['ATU13585627', 'U13585627']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['ATU13585626', 'U13585626']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Belgian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'BE'); // Valid samples var validSamples = ['BE0428759497', '0428759497']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['BE431150351', '431150351']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Bulgarian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'BG'); // Valid samples var validSamples = ['BG175074752', 'BG7523169263', 'BG8032056031', 'BG7542011030', 'BG7111042925', '175074752', '7523169263', '8032056031']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['BG175074753', 'BG7552A10004', 'BG7111042922', '175074753', '7552A10004']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Cypriot VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'CY'); // Valid samples var validSamples = ['CY10259033P', '10259033P']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['CY10259033Z', '10259033Z']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Czech Republic VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'CZ'); // Valid samples var validSamples = ['CZ25123891', 'CZ7103192745', 'CZ991231123', 'CZ640903926', '25123891', '7103192745']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['CZ25123890', 'CZ1103492745', 'CZ590312123', '25123890', '1103492745']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('German VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'DE'); // Valid samples var validSamples = ['DE136695976', '136695976']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['DE136695978', '136695978']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Danish VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'DK'); // Valid samples var validSamples = ['DK13585628', '13585628']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['DK13585627', '13585627']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Estonian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'EE'); // Valid samples var validSamples = ['EE100931558', 'EE100594102', '100931558', '100594102']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['EE100594103', '100594103']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Spanish VAT number (NIF)', function() { this.bv.updateOption('vat', 'vat', 'country', 'ES'); // Valid samples var validSamples = ['ES54362315K', 'ESX2482300W', 'ESX5253868R', 'ESM1234567L', 'ESJ99216582', 'ESB58378431', 'ESB64717838', '54362315K', 'X2482300W', 'X5253868R', 'M1234567L', 'J99216582']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['ES54362315Z', 'ESX2482300A', 'ESJ99216583', '54362315Z', 'X2482300A']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Finnish VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'FI'); // Valid samples var validSamples = ['FI20774740', '20774740']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['FI20774741', '20774741']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('French VAT number (TVA)', function() { this.bv.updateOption('vat', 'vat', 'country', 'FR'); // Valid samples var validSamples = ['FR40303265045', 'FR23334175221', 'FRK7399859412', 'FR4Z123456782', '40303265045', '23334175221', 'K7399859412']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['FR84323140391', '84323140391']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('United Kingdom VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'GB'); // Valid samples var validSamples = ['GB980780684', '980780684']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['GB802311781', '802311781']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Greek VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'GR'); // Valid samples var validSamples = ['GR023456780', 'EL094259216', '023456780', '094259216']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['GR123456781', '123456781']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Hungarian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'HU'); // Valid samples var validSamples = ['HU12892312', '12892312']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['HU12892313', '12892313']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Croatian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'HR'); // Valid samples var validSamples = ['HR33392005961', '33392005961']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['HR33392005962', '33392005962']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Irish VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'IE'); // Valid samples var validSamples = ['IE6433435F', 'IE6433435OA', 'IE8D79739I', '6433435F', '6433435OA', '8D79739I']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['IE8D79738J', '8D79738J']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Iceland VAT (VSK) number', function() { this.bv.updateOption('vat', 'vat', 'country', 'IS'); // Valid samples var validSamples = ['IS11111', 'IS111111', '11111', '111111']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['IS1234567', 'IS123456ABC', '1234567', '123456ABC']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Italian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'IT'); // Valid samples var validSamples = ['IT00743110157', '00743110157']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['IT00743110158', '00743110158']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Lithuanian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'LT'); // Valid samples var validSamples = ['LT119511515', 'LT100001919017', 'LT100004801610', '119511515', '100001919017', '100004801610']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['LT100001919018', '100001919018']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Luxembourg VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'LU'); // Valid samples var validSamples = ['LU15027442', '15027442']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['LU15027443', '15027443']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Latvian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'LV'); // Valid samples var validSamples = ['LV40003521600', 'LV16117519997', '40003521600', '16117519997']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['LV40003521601', 'LV16137519997', '40003521601', '16137519997']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Maltese VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'MT'); // Valid samples var validSamples = ['MT11679112', '11679112']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['MT11679113', '11679113']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Dutch VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'NL'); // Valid samples var validSamples = ['NL004495445B01', '004495445B01']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['NL123456789B90', '123456789B90']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Polish VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'PL'); // Valid samples var validSamples = ['PL8567346215', '8567346215']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['PL8567346216', '8567346216']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Portuguese VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'PT'); // Valid samples var validSamples = ['PT501964843', '501964843']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['PT501964842', '501964842']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Romanian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'RO'); // Valid samples var validSamples = ['RO18547290', '18547290']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['RO18547291', '18547291']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Russian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'RU'); // Valid samples var validSamples = ['RU7805145876', 'RU781300557475', '7805145876', '781300557475']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['RU7805145877', 'RU781300557474', '7805145877', '781300557474']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Swedish VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'SE'); // Valid samples var validSamples = ['SE123456789701', '123456789701']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['SE123456789101', '123456789101']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Slovenian VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'SI'); // Valid samples var validSamples = ['SI50223054', '50223054']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['SI50223055', '50223055']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Slovak VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'SK'); // Valid samples var validSamples = ['SK2022749619', '2022749619']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['SK2022749618', '2022749618']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('South African VAT number', function() { this.bv.updateOption('vat', 'vat', 'country', 'ZA'); // Valid samples var validSamples = ['ZA4012345678', '4012345678']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['ZA40123456789', 'ZA0123456789', '40123456789', '0123456789']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Venezuelan VAT number (RIF)', function() { this.bv.updateOption('vat', 'vat', 'country', 'VE'); // Valid samples var validSamples = ['VEJ309272292', 'VEV242818101', 'VEJ000126518', 'VEJ000458324', 'J309272292', 'V242818101', 'J000126518', 'J000458324']; for (var i in validSamples) { this.bv.resetForm(); this.$vat.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['VEJ309272293', 'VEV242818100', 'J000126519', 'J000458323']; for (i in invalidSamples) { this.bv.resetForm(); this.$vat.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); }); function getCountryCode(value, validator, $field) { $('#msg').html('getCountryCode() called'); return validator.getFieldElements('country').val(); }; TestSuite = $.extend({}, TestSuite, { ZipCode: { getCountryCode: function(value, validator, $field) { $('#msg').html('TestSuite.ZipCode.getCountryCode() called'); return validator.getFieldElements('country').val(); } } }); describe('zipCode', function() { beforeEach(function() { $([ '
', '
', '
', '', '
', '', '
', '
', '
', '', '
', '', '
', '
', '
' ].join('\n')).appendTo('body'); $('#zipCodeForm').bootstrapValidator(); /** * @type {BootstrapValidator} */ this.bv = $('#zipCodeForm').data('bootstrapValidator'); this.$country = this.bv.getFieldElements('country'); this.$zipCode = this.bv.getFieldElements('zc'); }); afterEach(function() { $('#zipCodeForm').bootstrapValidator('destroy').remove(); }); it('country code updateOption()', function() { // Check IT postal code this.bv.updateOption('zc', 'zipCode', 'country', 'IT'); this.$zipCode.val('1234'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$zipCode.val('IT-12345'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); // Check United Kingdom postal code this.bv.updateOption('zc', 'zipCode', 'country', 'GB'); var validUkSamples = ['EC1A 1BB', 'W1A 1HQ', 'M1 1AA', 'B33 8TH', 'CR2 6XH', 'DN55 1PT', 'AI-2640', 'ASCN 1ZZ', 'GIR 0AA']; for (var i in validUkSamples) { this.bv.resetForm(); this.$zipCode.val(validUkSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } }); it('country code other field declarative', function() { this.$zipCode.attr('data-bv-zipcode-country', 'country'); // Need to destroy the plugin instance ... $('#zipCodeForm').bootstrapValidator('destroy'); // ... and re-create it this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('IT'); this.bv.resetForm(); this.$zipCode.val('1234'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$zipCode.val('I-12345'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); }); it('country code callback declarative function', function() { this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('NL'); this.$zipCode.val('0123'); this.bv.validate(); expect($('#msg').html()).toEqual('getCountryCode() called'); expect(this.bv.isValid()).toEqual(false); }); it('country code callback declarative function()', function() { this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode()'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('NL'); this.$zipCode.val('1234 ab'); this.bv.validate(); expect($('#msg').html()).toEqual('getCountryCode() called'); expect(this.bv.isValid()).toBeTruthy(); }); it('country code callback declarative A.B.C', function() { this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('DK'); this.$zipCode.val('DK 123'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called'); expect(this.bv.isValid()).toEqual(false); }); it('country code callback declarative A.B.C()', function() { this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode()'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.$country.val('DK'); this.$zipCode.val('DK-1234'); this.bv.validate(); expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called'); expect(this.bv.isValid()).toBeTruthy(); }); it('country code callback programmatically', function() { this.$zipCode.removeAttr('data-bv-zipcode-country'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm') .bootstrapValidator({ fields: { zc: { validators: { zipCode: { country: function(value, validator, $field) { return getCountryCode(value, validator, $field); } } } } } }) .data('bootstrapValidator'); this.$country.val('SE'); this.bv.resetForm(); this.$zipCode.val('S-567 8'); this.bv.validate(); expect($('#msg').html()).toEqual('getCountryCode() called'); expect(this.bv.isValid()).toEqual(false); this.bv.resetForm(); this.$zipCode.val('S-12345'); this.bv.validate(); expect($('#msg').html()).toEqual('getCountryCode() called'); expect(this.bv.isValid()).toBeTruthy(); }); it('not supported country code', function() { this.$zipCode.attr('data-bv-zipcode-country', 'NOT_SUPPORTED'); $('#zipCodeForm').bootstrapValidator('destroy'); this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator'); this.bv.resetForm(); this.$zipCode.val('1234'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); expect(this.bv.getMessages(this.$zipCode, 'zipCode')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, 'NOT_SUPPORTED')); }); it('US zipcode', function() { this.$zipCode.val('12345'); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); this.bv.resetForm(); this.$zipCode.val('123'); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); }); it('Czech Republic postal code', function() { this.bv.updateOption('zc', 'zipCode', 'country', 'CZ'); // Valid samples var validSamples = ['12345', '123 45']; for (var i in validSamples) { this.bv.resetForm(); this.$zipCode.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['12 345', '123456', '1 2345', '1234 5', '12 3 45']; for (i in invalidSamples) { this.bv.resetForm(); this.$zipCode.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Slovakia postal code', function() { this.bv.updateOption('zc', 'zipCode', 'country', 'SK'); // Valid samples var validSamples = ['12345', '123 45']; for (var i in validSamples) { this.bv.resetForm(); this.$zipCode.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['12 345', '123456', '1 2345', '1234 5', '12 3 45']; for (i in invalidSamples) { this.bv.resetForm(); this.$zipCode.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('France postal code', function() { this.bv.updateOption('zc', 'zipCode', 'country', 'FR'); // Valid samples var validSamples = ['12340', '01230', '75116']; for (var i in validSamples) { this.bv.resetForm(); this.$zipCode.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['123 45', '12 345', '123456', '1 2345', '1234 5', '12 3 45', '1234A']; for (i in invalidSamples) { this.bv.resetForm(); this.$zipCode.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Eircode (Ireland postal code)', function() { this.bv.updateOption('zc', 'zipCode', 'country', 'IE'); // Valid samples var validSamples = ['A65 F4E2', 'D6W FNT4', 'T37 F8HK']; for (var i in validSamples) { this.bv.resetForm(); this.$zipCode.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['a65 f4e2', 'D6W FNTO', 'T37F8HK']; for (i in invalidSamples) { this.bv.resetForm(); this.$zipCode.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); it('Portugal postal code', function() { this.bv.updateOption('zc', 'zipCode', 'country', 'PT'); // Valid samples var validSamples = ['2435-459', '1000-000', '1234-456']; for (var i in validSamples) { this.bv.resetForm(); this.$zipCode.val(validSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toBeTruthy(); } // Invalid samples var invalidSamples = ['0123-456', '1234456', '1234-ABC', '1234 456']; for (i in invalidSamples) { this.bv.resetForm(); this.$zipCode.val(invalidSamples[i]); this.bv.validate(); expect(this.bv.isValid()).toEqual(false); } }); });