Search
 
SCRIPT & CODE EXAMPLE
 

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;
Comment

PREVIOUS NEXT
Code Example
Javascript :: give call suggestions while clicking on a contact number in next js 
Javascript :: JavaScript Using es6(ES2015) Destructuring assignment 
Javascript :: react-native navigation homeStack 
Javascript :: what does the symbol function do in javascript 
Javascript :: python code to javascript converter 
Javascript :: javascript hide div 
Javascript :: add text to each element in an array javascript 
Javascript :: prisma graphql n+1 problem solution 
Javascript :: javascript remove the second to last character of a string 
Javascript :: pass data between router components 
Javascript :: NextJs + Material UI, manually refreshing causes 
Javascript :: How to Loop Through an Array with a While Loop in JavaScript 
Javascript :: sinha express crud template 
Javascript :: how to get on hnage input before clicking off 
Javascript :: Proper Way To Access Model(s) Data From Collection In Backbone 
Javascript :: Backbone View Notes 
Javascript :: Backbone Collection Example 
Javascript :: how to decrypt md5 hash 
Javascript :: Closure examples 
Javascript :: text inside image react native 
Javascript :: react onsubmit get form values 
Javascript :: jwt_access_secret generator 
Javascript :: generate a link with javascript 
Javascript :: noise expression after effects 
Javascript :: insertmany 
Javascript :: useThrottle 
Javascript :: JavaScript Precision Problems 
Javascript :: xhr.upload 
Javascript :: actionscript random randomfunction 
Javascript :: convert to slug javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =