简体   繁体   中英

I want to accept only 1-24 in the input field and disable all other buttons. how is it possible

This is my input field and I want to accept only 1-24 and disable all other keys. How can this be achieved in react.

                    <TextFieldCustom
                    placeholder="Enter checkin hours"
                    value={e.target.value}
                    onChange={onChangeHandler}
                    maxLength={3}
                    type=number
                    onKeyDown={onKeyDown}
          
                  />

here is the change handler of your case without specifying type="number" :

const [text, setText] = useState('');
  const changeHandler = (e: any) => {
    const val = e.target.value;
    if ((val >=1 && val <= 24) || val === '') {
      setText(val);
    }
  };

return (
 <TextFieldCustom
   placeholder="Enter checkin hours"
   value={text}
   onChange={changeHandler}
   onKeyDown={onKeyDown}
  />
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM