Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yup.string().matches() alternates beween valid and invalid as you are typing

Trying to validate a phone number field in Formik using Yup validation schema, but when i use Yup.string().matches(/^[0-9]*$/g, 'This is not a number') it consistently alternates between valid and invalid on each change event.

    phone: Yup.string()
      .trim()     
      .matches(/^[0-9]*$/g, 'This is not a number')    
  }),

input field is of type tel. <Field type="tel" id="phone" name="phone" />

like image 516
Frederick Rogers Avatar asked Dec 28 '25 14:12

Frederick Rogers


1 Answers

Ok, appears i could have been my RegEx after all that was causing this strange behavior.

  • Original regEx: /^[0-9]*$/g
  • Modified regEx: /^[\d]*$/g
  • FINAL regEx: /^\d+$/ <-- This solved the problem.

My regEx knowledge is limited so i would be happy if somebody could tell me why the two first regEx codes caused the alternating state of validation.

like image 192
Frederick Rogers Avatar answered Dec 31 '25 19:12

Frederick Rogers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!