Home > Software engineering >  i want to add both type and maxLength to mui textfield
i want to add both type and maxLength to mui textfield

Time:06-13

<TextField
                      value={ele.mobile}
                      helperText={ferrors[id]?.mobile}
                      name="mobile"
                      classes={{ root: classes.textField }}
                      InputProps={{ className: classes.textField }}
                      inputProps={{ maxLength: 10, type:'number'}}
                      label="Mobile Number"
                      variant="outlined"
                      onChange={(e) => handleChange(id, e)}
                    />

I have given the values form inputProps of maxLenght and type but only type is working

CodePudding user response:

if you want to make number of lenght max 10, just use max with largest number:

inputProps={{ max: 9999999999, type:'number'}}

CodePudding user response:

maybe this will help
 <TextField
 required
 id="required"
 label="Required"
 min={0}
 inputProps={{
    maxLength: 5,
    type: 'number',
}}
onInput={(e)=>{
    e.target.value = Math.max(0, parseInt(e.target.value) ).toString().slice(0,e.target.maxLength)
}}
/>

CodePudding user response:

About your question, I think you can try to use maxlength setting in inputProps.

<TextField
                      value={ele.mobile}
                      helperText={ferrors[id]?.mobile}
                      name="mobile"
                      classes={{ root: classes.textField }}
                      InputProps={{ className: classes.textField }}
                      inputProps ={{ maxlength: 10, type:'number'}}
                      label="Mobile Number"
                      variant="outlined"
                      onChange={(e) => handleChange(id, e)}
                    />

  • Related