-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidateForm.js
42 lines (35 loc) · 1.23 KB
/
validateForm.js
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
38
39
40
41
42
const Validator = require('validator')
const isEmpty = require('./is-empty')
module.exports = validateData = (data) => {
let errors = {};
data.firstname = !isEmpty(data.firstname) ? data.firstname : '';
data.lastname = !isEmpty(data.lastname) ? data.lastname : '';
data.email = !isEmpty(data.email) ? data.email : '';
data.address = !isEmpty(data.address) ? data.address : '';
data.phonenumber = !isEmpty(data.phonenumber) ? data.phonenumber : '';
if (Validator.isEmpty(data.firstname)) {
errors.firstname = 'First name is required'
}
if (Validator.isEmpty(data.lastname)) {
errors.lastname = 'Last name is required'
}
if (Validator.isEmpty(data.email)) {
errors.email = 'Email is required'
}
if (!Validator.isEmail(data.email)) {
errors.email = 'Email is invalid'
}
if (Validator.isEmpty(data.phonenumber)) {
errors.phonenumber = 'Phone number is required'
}
if (!Validator.isNumeric(data.phonenumber)) {
errors.phonenumber = 'Please enter phone number'
}
if (Validator.isEmpty(data.address)) {
errors.address = 'Address is required'
}
return {
errors: errors,
isValid: isEmpty(errors)
}
}