Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

registration page validation in react

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import validator from 'validator';
class FormValidator {
    constructor(validations) {
        this.validations = validations;
    }
    validate(state) {
        let validation = this.valid();
        // for each validation rule
        this.validations.forEach(rule => {
            if(!validation[rule.field].isInvalid) {
                const field_value = state[rule.field].toString();
                const args = rule.args || [];
                const validation_method = typeof rule.method === 'string' ? validator[rule.method] : rule.method
                if(validation_method(field_value, ...args, state) !== rule.validWhen) {
                    validation[rule.field] = {
                        isInvalid: true,
                        message: rule.message
                    }
                    validation.isValid = false;
                }
            }
        });
        return validation;
    }
    valid() {
        const validation = {}
        this.validations.map(rule => (validation[rule.field] = {
            isInvalid: false,
            message: ''
        }));
        return {
            isValid: true,
            ...validation
        };
    }
}
export default FormValidator;
Source by www.tutsmake.com #
 
PREVIOUS NEXT
Tagged: #registration #page #validation #react
ADD COMMENT
Topic
Name
8+5 =