Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set material-ui TextField to accept only Hexidecimal characters

I want my TextField to accept only the values from 0-9 and letters A-F. Thanks.

like image 492
Zotov Avatar asked Jan 26 '23 23:01

Zotov


1 Answers

        <TextField
          id="text-field-1"
          placeholder="Enter text"
          type="text"
          value={state.alphanum}
          onChange={(event) => {
            const regex = /^([a-z0-9]){minLength,maxLength}$/i;
            if (event.target.value === '' || regex.test(event.target.value)) {
              setState({ ...state, alphanum: event.target.value });
            }
          }}
          variant="outlined" />
like image 53
Nagarjun Ev Avatar answered Jan 31 '23 21:01

Nagarjun Ev