Is there a way to restrict the phone number input to only allow 10 numbers and exclude any other characters? Currently, setting the maxLength attribute limits the characters to 10 but allows additional characters as well.
If I change the type
attribute to type="number"
, maxLength no longer functions properly and certain characters like [minus(-) or plus(+)] are still accepted.
What steps can be taken to ensure that the input field only accepts a maximum of 10 numbers and disallows any other characters?
<input
placeholder="Telephone Number"
className="input_fields telephone_no"
type="tel"
maxLength={"10"}
name="phoneNumber"
id="teacher_telephone_no"
value={this.state.user.phoneNumber}
onChange={e =>
this.setState({
user: {
...this.state.user,
[e.target.name]: e.target.value
}
})
}
/>