UNPKG

4.69 kBJavaScriptView Raw
1if (typeof require !== 'undefined') {
2 var Validator = require('../src/validator.js');
3 var expect = require('chai').expect;
4} else {
5 var Validator = window.Validator;
6 var expect = window.chai.expect;
7}
8
9describe('regex validation rule for most common regular expressions', function() {
10 it('should pass with the currency pattern: 12,500.00', function() {
11 var validator = new Validator({
12 currency: '12,500.00'
13 }, {
14 currency: 'regex:/^(?!0\\.00)\\d{1,3}(,\\d{3})*(\\.\\d\\d)?$/'
15 });
16 expect(validator.passes()).to.be.true;
17 });
18
19 it('should fail with the currency pattern: 200.0', function() {
20 var validator = new Validator({
21 currency: '200.0'
22 }, {
23 currency: 'regex:/^(?!0\\.00)\\d{1,3}(,\\d{3})*(\\.\\d\\d)?$/'
24 });
25 expect(validator.fails()).to.be.true;
26 });
27
28 it('should pass with the date pattern: 03/11/2015', function() {
29 var validator = new Validator({
30 pattern: '03/11/2015'
31 }, {
32 pattern: ['regex:/^([1-9]|0[1-9]|[12][0-9]|3[01])\\D([1-9]|0[1-9]|1[012])\\D(19[0-9][0-9]|20[0-9][0-9])$/']
33 });
34 expect(validator.passes()).to.be.true;
35 });
36 it('should fail with the date pattern: 0311/2015', function() {
37 var validator = new Validator({
38 pattern: '0311/2015'
39 }, {
40 pattern: ['regex:/^([1-9]|0[1-9]|[12][0-9]|3[01])\\D([1-9]|0[1-9]|1[012])\\D(19[0-9][0-9]|20[0-9][0-9])$/']
41 });
42 expect(validator.fails()).to.be.true;
43 });
44
45 it('should pass with the year pattern: 2015', function() {
46 var validator = new Validator({
47 pattern: '2015'
48 }, {
49 pattern: ['regex:/^(19|20)[\\d]{2,2}$/']
50 });
51 expect(validator.passes()).to.be.true;
52 });
53 it('should fail with the year pattern:: 20151', function() {
54 var validator = new Validator({
55 pattern: '20151'
56 }, {
57 pattern: ['regex:/^(19|20)[\\d]{2,2}$/']
58 });
59 expect(validator.fails()).to.be.true;
60 });
61
62
63 it('should pass with the email pattern: johndoe@gmail.com', function() {
64 var validator = new Validator({
65 pattern: 'johndoe@gmail.com'
66 }, {
67 pattern: ['regex:/^(([^<>()[\\]\\\.,;:\\s@\\"]+(\\.[^<>()[\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/']
68 });
69 expect(validator.passes()).to.be.true;
70 });
71 it('should fail with the email pattern: johndoe.gmail.com', function() {
72 var validator = new Validator({
73 pattern: 'johndoe.gmail.com'
74 }, {
75 pattern: ['regex:/^(([^<>()[\\]\\\.,;:\\s@\\"]+(\\.[^<>()[\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/']
76 });
77 expect(validator.fails()).to.be.true;
78 });
79
80 it('should pass with the url pattern: http://github.com', function() {
81 var validator = new Validator({
82 pattern: 'http://github.com'
83 }, {
84 pattern: ['regex:/^https?:\\/\\/\\S+/']
85 });
86 expect(validator.passes()).to.be.true;
87 });
88 it('should fail with the url pattern: http://githubcom', function() {
89 var validator = new Validator({
90 pattern: 'http:/github.com'
91 }, {
92 pattern: ['regex:/^https?:\\/\\/\\S+/']
93 });
94 expect(validator.fails()).to.be.true;
95 });
96
97 it('should pass with the hex pattern: #ff0033', function() {
98 var validator = new Validator({
99 pattern: '#ff0033'
100 }, {
101 pattern: ['regex:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/']
102 });
103 expect(validator.passes()).to.be.true;
104 });
105 it('should fail with the hex pattern: #xx9911', function() {
106 var validator = new Validator({
107 pattern: '#xx9911'
108 }, {
109 pattern: ['regex:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/']
110 });
111 expect(validator.fails()).to.be.true;
112 });
113
114 it('should pass with the slug pattern: matching-a-slug-here', function() {
115 var validator = new Validator({
116 pattern: 'matching-a-slug-here'
117 }, {
118 pattern: ['regex:/^[a-z0-9-]+$/']
119 });
120 expect(validator.passes()).to.be.true;
121 });
122 it('should fail with the slug pattern: not_matching_here', function() {
123 var validator = new Validator({
124 pattern: 'not_matching_here'
125 }, {
126 pattern: ['regex:/^[a-z0-9-]+$/']
127 });
128 expect(validator.fails()).to.be.true;
129 });
130
131 it('should support the case insensitive flag', function () {
132 var validator = new Validator({
133 pattern: 'A'
134 }, {
135 pattern: ['regex:/[a-f]/i']
136 });
137
138 expect(validator.passes()).to.be.true;
139 })
140
141 it('should not be case insensitive unless specified', function() {
142 var validator = new Validator({
143 pattern: 'A'
144 }, {
145 pattern: ['regex:/[a-f]/']
146 });
147
148 expect(validator.fails()).to.be.true;
149 });
150});