I'm using React and Material-UI's TextField with type='number'
but as I saw the validation is rather simple, it just allows any character that could represent part of a number [0-9\.eE-]*
, allowing inputs like this: eee---...e---0.-1
, I decided to implement my own validation. (not sure if that trivial validation is Material-UI's or HTML native, but doesn't matter anyway)
So I wrote this regular expression for validation, but as soon as I enter a character that doesn't match my regular expression, my onChange event handler doesn't get called at all and it seems the component takes control and applies its basic validation. My event handler doesn't get called again until I correct the input and make it match with my regular expression.
Can I prevent the component from doing this somehow?
onValueChange = (ev) => {
if( /^-?(\d+)?(\.\d*)?$/.test(ev.target.value)) {
this.setState({ value: ev.target.value });
}
Complete code: https://codepen.io/anon/pen/eoZOaE?editors=1000