I have a TextField with some validation i need:
minValue = 1
maxValue = 10
These validations work if I use arrows from TextField, but if I type directly into it, I could type any number. How would i fix/validate this
<TextField
label="Selectati nota"
type="number"
id={subject._id}
value={subject.gradeData.grade}
onChange={(ev) => updateGrade(ev)}
disabled={loading}
size="small"
InputLabelProps={{style: {fontSize: 18, color:"#3F51B5"}}}
InputProps={{
style: {
fontSize:18, fontWeight:"bold"
},
inputProps: {
min: 1,
max: 10,
maxLength: 2,
// pattern: "^[1-9]d*$",
},
}}
style={{ width: 150, marginLeft: 10 }}
variant="filled"/>
Does this work? added step
<TextField type="number" inputProps={{ min: "1", max: "10", step: "1" }} />
And if that's not working try to check it in the onChange function
Try changing the onChange
function:
<TextField
// other props
onChange={handleChange}
/>
onChange
function:
const handleChange = e => {
setState(prev => {
if (e.target.value < min || e.target.value > max) {
return prev;
}
return e.target.value;
})
}
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.