2

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>


);


}
3
  • did you try using .length ? Commented Oct 29, 2020 at 1:25
  • How do you use .length in this solution? Commented Oct 29, 2020 at 1:29
  • event.target.value.length < somenumber ? Commented Oct 29, 2020 at 1:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I expected. Thank you!!
If it answered your question you can mark it as accepted answer for helping stackoverflow community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.