skip to Main Content
onChange={(e) => {
    if (e.target.value.match("^[a-zA-Z]*$") != null) {
        setName(e.target.value);
        // console.log(setName);
    } else {
        toast.error("Please match the required format");
    }
}}

this required validation is not working. database is updated with empty string

2

Answers


  1. onChange = {(e) => {
      if(e.target.value.match("^[a-zA-Z]*$") === true){
        setName(e.target.value) 
      } else {
        toast.error("Please match the required format")
      }
    }}
    
    Login or Signup to reply.
  2. You can also use test like:

    onChange={(e) => {
     if (/^[a-zA-Z]*$/.test(e.target.value)) {
         setName(e.target.value);
         // console.log(setName);
     } else {
         toast.error("Please match the required format");
     }
    }}
    

    Hope it maybe helpful.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search