validation user input in react js

Viewed 63

I am trying to figure out how to validate the user's input by checking the length of their input string. Right now the code will validate if the user's input whatever is string or integer. But I need help with validate the length of their input string and give them an error if the length of the input string is less than 2.

const handleText = (event) => {
    let letters = /^[A-Za-z]+$/;

    if (event.target.value.match(letters)) {
        setText(event.target.value);
    }else
        setText("error");
}

return(


<div>
<TextField onChange = {handleText}  label={"Name"}/>
            {text}
</div>


);


}
1 Answers

May be this code will help. You can play with it on this link sandbox i created. You can validate length through onSubmit event by using the length property.

Related