Created
August 14, 2017 13:35
-
-
Save harpalsinh-jadeja/d84f214b859dc83d449adf32e9e43eae to your computer and use it in GitHub Desktop.
React-native Validation using validate.js
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
import validation from 'validate.js' | |
export default function validate(fieldName, value) { | |
var constraints = { | |
email: { | |
presence: true, | |
format: { | |
pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | |
message: 'Invalid email id', | |
} | |
}, | |
password: { | |
presence: true, | |
length: { | |
minimum: 4, | |
message: 'Invalid Password', | |
} | |
}, | |
confirmPassword: { | |
presence: true, | |
equality: 'password' | |
}, | |
phoneNo: { | |
presence: true, | |
format: { | |
pattern: "^[0-9]{10}$", | |
message: 'Invalid phone number', | |
}, | |
}, | |
}; | |
var formValues = {} | |
formValues[fieldName] = value | |
var formFields = {} | |
formFields[fieldName] = constraints[fieldName] | |
const result = validation(formValues, formFields) | |
if (result) { | |
return result[fieldName][0] | |
} | |
return null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment