Skip to content

Instantly share code, notes, and snippets.

@minhajul-islam
Last active August 6, 2020 05:23
Show Gist options
  • Select an option

  • Save minhajul-islam/71baab075567c14694ae470980945b9e to your computer and use it in GitHub Desktop.

Select an option

Save minhajul-islam/71baab075567c14694ae470980945b9e to your computer and use it in GitHub Desktop.
form validation for react native
import validation from 'validate.js';
export default function validate(fieldName, value) {
let 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',
},
},
};
let formValues = {};
formValues[fieldName] = value;
let formFields = {};
formFields[fieldName] = constraints[fieldName];
const result = validation(formValues, formFields);
console.log('result', result);
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