Skip to content

Instantly share code, notes, and snippets.

@harpalsinh-jadeja
Created August 14, 2017 13:35
Show Gist options
  • Save harpalsinh-jadeja/d84f214b859dc83d449adf32e9e43eae to your computer and use it in GitHub Desktop.
Save harpalsinh-jadeja/d84f214b859dc83d449adf32e9e43eae to your computer and use it in GitHub Desktop.
React-native Validation using validate.js
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