Created
May 10, 2019 06:32
-
-
Save omosehin/4b008549f1f5672955019ebab71602d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validationRules = () => { | |
const emailRegex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; | |
const numRegex = /(0){1}(7|8|9){1}(0|1){1}[2-9]{1}\d{7}$/; | |
return { | |
firstName: (val) => val.length < 4 ? `First name must be longer than 3 letters!` : '', | |
lastName: (field) => field.length < 4 ? `Last name must be longer than 3 letters!` : '', | |
secret: (val) => val.length < 6 ? `Password length must be longer than 5!` : '', | |
userEmail: (val) => !emailRegex.test(val) ? "Valid email address is required!" : '', | |
phoneNumber: (val) => !numRegex.test(val) ? "Valid phone number is required!" : '', | |
userLevel: (val) => !val.length ? "User level is required!" : '', | |
permissionLevel: (val) => !val.length ? "Permission level is required!" : '', | |
} | |
} | |
isValid = () => { | |
const { formData, formatError } = this.state; | |
let response = true; | |
const firstnameErr = this.validationRules('Firstname').firstName(formData.firstName); | |
if (firstnameErr) { | |
formatError.firstNameError = firstnameErr; | |
response = false; | |
} | |
const lastnameErr = this.validationRules('Lastname').lastName(formData.lastName); | |
if (lastnameErr) { | |
formatError.lastNameError = lastnameErr; | |
response = false; | |
} | |
const emailErr = this.validationRules('Email').userEmail(formData.userEmail) | |
if (emailErr) { | |
formatError.userEmailError = emailErr; | |
response = false; | |
} | |
if (this.state.formAction === 'Create') { | |
const passwordErr = this.validationRules('Password').secret(formData.secret) | |
if (passwordErr) { | |
formatError.secretError = passwordErr; | |
response = false; | |
} | |
} | |
const phoneErr = this.validationRules('Phone number').phoneNumber(formData.phoneNumber) | |
if (phoneErr) { | |
formatError.phoneNumberError =phoneErr; | |
response = false; | |
} | |
const userLevelErr = this.validationRules('User level').userLevel(formData.userLevel) | |
if (userLevelErr) { | |
formatError.userLevelError = userLevelErr; | |
response = false; | |
} | |
const permissionLevelErr = this.validationRules('Permission level').permissionLevel(formData.permissions) | |
if (permissionLevelErr){ | |
formatError.permissionError = permissionLevelErr; | |
response = false; | |
} | |
this.setState({ formatError }); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment