Merge pull request #205 from christianalfoni/isAlphanumeric
Added new validation rules: `isAlphanumeric`, `isInt`, `isFloat`
This commit is contained in:
commit
0c5e927f8b
30
API.md
30
API.md
|
|
@ -659,18 +659,36 @@ Returns true if the value is the boolean true
|
|||
```
|
||||
Returns true if the value is the boolean false
|
||||
|
||||
**isNumeric**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isNumeric"/>
|
||||
```
|
||||
Returns true if string only contains numbers
|
||||
|
||||
**isAlpha**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isAlpha"/>
|
||||
```
|
||||
Returns true if string is only letters
|
||||
|
||||
**isNumeric**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isNumeric"/>
|
||||
```
|
||||
Returns true if string only contains numbers. Examples: 42; -3.14
|
||||
|
||||
**isAlphanumeric**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isAlphanumeric"/>
|
||||
```
|
||||
Returns true if string only contains letters or numbers
|
||||
|
||||
**isInt**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isInt"/>
|
||||
```
|
||||
Returns true if string represents integer value. Examples: 42; -12; 0
|
||||
|
||||
**isFloat**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isFloat"/>
|
||||
```
|
||||
Returns true if string represents float value. Examples: 42; -3.14; 1e3
|
||||
|
||||
**isWords**
|
||||
```html
|
||||
<MyInputComponent name="foo" validations="isWords"/>
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ describe('Rules: isAlpha', function () {
|
|||
|
||||
it('should pass with a string is only latin letters', pass('myValue'));
|
||||
|
||||
it('should fail with a string with numbers', fail('myValue 42'));
|
||||
it('should fail with a string with numbers', fail('myValue42'));
|
||||
|
||||
it('should pass with an undefined', pass(undefined));
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
import React from 'react';
|
||||
import TestUtils from 'react-addons-test-utils';
|
||||
|
||||
import Formsy from './..';
|
||||
import { customizeInput } from './utils/TestInput';
|
||||
|
||||
describe('Rules: isAlphanumeric', function () {
|
||||
let Input, isValid, form, input;
|
||||
|
||||
function pass(value) {
|
||||
return pass.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(true);
|
||||
} : () => expect(isValid).toBe(true);
|
||||
}
|
||||
|
||||
function fail(value) {
|
||||
return fail.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(false);
|
||||
} : () => expect(isValid).toBe(false);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
Input = customizeInput({
|
||||
render() {
|
||||
isValid = this.isValid();
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>;
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<Input name="foo" validations="isAlphanumeric"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Input = isValid = form = null;
|
||||
});
|
||||
|
||||
it('should pass with a default value', pass());
|
||||
|
||||
it('should pass with a string is only latin letters', pass('myValue'));
|
||||
|
||||
it('should pass with a string with numbers', pass('myValue42'));
|
||||
|
||||
it('should pass with an undefined', pass(undefined));
|
||||
|
||||
it('should pass with a null', pass(null));
|
||||
|
||||
it('should pass with an empty string', pass(''));
|
||||
|
||||
it('should pass with a number', pass(42));
|
||||
|
||||
it('should fail with a non alpha and number symbols', fail('!@#$%^&*()'));
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
import React from 'react';
|
||||
import TestUtils from 'react-addons-test-utils';
|
||||
|
||||
import Formsy from './..';
|
||||
import { customizeInput } from './utils/TestInput';
|
||||
|
||||
describe('Rules: isFloat', function () {
|
||||
let Input, isValid, form, input;
|
||||
|
||||
function pass(value) {
|
||||
return pass.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(true);
|
||||
} : () => expect(isValid).toBe(true);
|
||||
}
|
||||
|
||||
function fail(value) {
|
||||
return fail.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(false);
|
||||
} : () => expect(isValid).toBe(false);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
Input = customizeInput({
|
||||
render() {
|
||||
isValid = this.isValid();
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>;
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<Input name="foo" validations="isFloat"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Input = isValid = form = null;
|
||||
});
|
||||
|
||||
it('should pass with a default value', pass());
|
||||
|
||||
it('should pass with an empty string', pass(''));
|
||||
|
||||
it('should fail with an unempty string', fail('myValue'));
|
||||
|
||||
it('should pass with a number as string', pass('+42'));
|
||||
|
||||
it('should fail with a number as string with not digits', fail('42 as an answer'));
|
||||
|
||||
it('should pass with an int', pass(42));
|
||||
|
||||
it('should pass with a float', pass(Math.PI));
|
||||
|
||||
it('should pass with a float in science notation', pass('-1e3'));
|
||||
|
||||
it('should pass with an undefined', pass(undefined));
|
||||
|
||||
it('should pass with a null', pass(null));
|
||||
|
||||
it('should pass with a zero', pass(0));
|
||||
|
||||
});
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
import React from 'react';
|
||||
import TestUtils from 'react-addons-test-utils';
|
||||
|
||||
import Formsy from './..';
|
||||
import { customizeInput } from './utils/TestInput';
|
||||
|
||||
describe('Rules: isInt', function () {
|
||||
let Input, isValid, form, input;
|
||||
|
||||
function pass(value) {
|
||||
return pass.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(true);
|
||||
} : () => expect(isValid).toBe(true);
|
||||
}
|
||||
|
||||
function fail(value) {
|
||||
return fail.length ? () => {
|
||||
TestUtils.Simulate.change(input, {target: {value}});
|
||||
expect(isValid).toBe(false);
|
||||
} : () => expect(isValid).toBe(false);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
Input = customizeInput({
|
||||
render() {
|
||||
isValid = this.isValid();
|
||||
return <input value={this.getValue()} onChange={this.updateValue}/>;
|
||||
}
|
||||
});
|
||||
|
||||
form = TestUtils.renderIntoDocument(
|
||||
<Formsy.Form>
|
||||
<Input name="foo" validations="isInt"/>
|
||||
</Formsy.Form>
|
||||
);
|
||||
|
||||
input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Input = isValid = form = null;
|
||||
});
|
||||
|
||||
it('should pass with a default value', pass());
|
||||
|
||||
it('should pass with an empty string', pass(''));
|
||||
|
||||
it('should fail with an unempty string', fail('myValue'));
|
||||
|
||||
it('should pass with a number as string', pass('+42'));
|
||||
|
||||
it('should fail with a number as string with not digits', fail('42 as an answer'));
|
||||
|
||||
it('should pass with an int', pass(42));
|
||||
|
||||
it('should fail with a float', fail(Math.PI));
|
||||
|
||||
it('should fail with a float in science notation', fail('-1e3'));
|
||||
|
||||
it('should pass with an undefined', pass(undefined));
|
||||
|
||||
it('should pass with a null', pass(null));
|
||||
|
||||
it('should pass with a zero', pass(0));
|
||||
|
||||
});
|
||||
|
|
@ -57,6 +57,8 @@ describe('Rules: isNumeric', function () {
|
|||
|
||||
it('should pass with a float', pass(Math.PI));
|
||||
|
||||
it('should fail with a float in science notation', fail('-1e3'));
|
||||
|
||||
it('should pass with an undefined', pass(undefined));
|
||||
|
||||
it('should pass with a null', pass(null));
|
||||
|
|
|
|||
|
|
@ -38,16 +38,25 @@ var validations = {
|
|||
if (typeof value === 'number') {
|
||||
return true;
|
||||
}
|
||||
return validations.matchRegexp(values, value, /^[-+]?(\d*[.])?\d+$/);
|
||||
return validations.matchRegexp(values, value, /^[-+]?(?:\d*[.])?\d+$/);
|
||||
},
|
||||
isAlpha: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^[a-zA-Z]+$/);
|
||||
return validations.matchRegexp(values, value, /^[A-Z]+$/i);
|
||||
},
|
||||
isAlphanumeric: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^[0-9A-Z]+$/i);
|
||||
},
|
||||
isInt: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^(?:[-+]?(?:0|[1-9]\d*))$/);
|
||||
},
|
||||
isFloat: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^(?:[-+]?(?:\d+))?(?:\.\d*)?(?:[eE][\+\-]?(?:\d+))?$/);
|
||||
},
|
||||
isWords: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^[a-zA-Z\s]+$/);
|
||||
return validations.matchRegexp(values, value, /^[A-Z\s]+$/i);
|
||||
},
|
||||
isSpecialWords: function (values, value) {
|
||||
return validations.matchRegexp(values, value, /^[a-zA-Z\s\u00C0-\u017F]+$/);
|
||||
return validations.matchRegexp(values, value, /^[A-Z\s\u00C0-\u017F]+$/i);
|
||||
},
|
||||
isLength: function (values, value, length) {
|
||||
return !isExisty(value) || isEmpty(value) || value.length === length;
|
||||
|
|
|
|||
Loading…
Reference in New Issue