MarsSanii
MarsSanii

Reputation: 23

validation user input in react js

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>


);


}

Upvotes: 2

Views: 105

Answers (1)

elpmid
elpmid

Reputation: 932

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.

Upvotes: 1

Related Questions