What I want to do is just to accept valid number so if someone put text and click continue the application should display an error saying that just numbers are allowed and only for numbers these eE+- keys will not be accepted means single keys if user put just e and hit submit I need to throw an error. I am using Formik also
The following code does not display an error message if the textfield is empty and the other validation to check if the user inputs text or numbers is pending and I'm stuck.
like this way it needs to be supported
this is valid
+1
-1
1e1
this blow is invalid need to throw error in this case
e
1-
1+
12e
const NumberField = (props) => {
const { errorMessege, ...restProps } = props;
const Number = /^-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i;
const numberConvertor = (e) => {
if (e.target.value == '' || Number.test(e.target.value)) {
return true
console.log(e.target.value);
} else {
e.preventDefault();
return false;
}
};
return (
<TextField
fullWidth
onKeyPress={numberConvertor}
helperText={errorMessege}
error={Boolean(errorMessege)}
{...restProps}
/>
);
};