Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minLength doesn't work in typescript, react

I have an input in Next, typescript. Maxlength does work but minlength doesn't work now. Does anyone how to validate this both?

Here's my code.

<input type="text" placeholder="text" required={true} minLength={2} maxLength={10} />
<button type="submit">Submit</button>
like image 261
pizzajuice Avatar asked Sep 07 '25 21:09

pizzajuice


2 Answers

Your code is fine. However minlength works different than maxlength.

  • HTML maxlength prevents the user from typing keys more than the maxlength.
  • HTML minlength will let the user enter less keys compared to the requirement. After all, they need to be able to start from less chars to come up to more chars. That said the field will show an error if minlenght is not met.

Complete example

Use this component and press the enter key to play around with the input field:

export default function App() {
  return (
  <form>
    <input type="text" placeholder="text" required={true} minLength={2} maxLength={10} />
    <button type="submit">Submit</button>
  </form>
  );
}

Screenshots

Because its required: enter image description here

Because minlenght is not met: enter image description here

like image 81
basarat Avatar answered Sep 09 '25 23:09

basarat


use can install Material ui Libraries npm install @material-ui/core Material ui Libraries

There have lots of components. use Textfield components .
Using inputProps we set maxlength in textfield

<TextField inputProps={{ maxLength: 10 }} type="text" />

Material Ui Textfied Compoents

like image 39
soma iyappan Avatar answered Sep 10 '25 00:09

soma iyappan