Home > OS >  Is there a way to restrict Formik field to only accept letters and not accept numbers?
Is there a way to restrict Formik field to only accept letters and not accept numbers?

Time:11-02

I have fields like first name and last name and i want to make sure no number is passed in them. I am using formik. Is there a way to do this?

CodePudding user response:

Why not use Yup Validation with Formik. It's a really nice way of adding in tests either on the whole form with validation schema or on individual fields.

You could make a test that matches for numbers with a Regex and the provide a validation error

CodePudding user response:

For that, you have to add a Validation method

let text ='dfdfsd 1';
let pattern = /\d/g;
let result = text.match(pattern);
 if (result &&result.length) {
     console.log('Number are not allowed');
   }

Check this example I hope it will help you to understand the flow of how you can achieve your goal.

  • Related