I am going through the trouble to validate a form. My Form is given below which is minimal reproduce able.
I have used React Material UI for the form.
import React from 'react';
import TextField from '@material-ui/core/TextField';
import { FormGroup } from '@material-ui/core';
function App(props) {
const validator = {
number: {
rules: [
{
test: /([\d]+)/,
message: 'numaric must contain only numeric characters',
},
{
test: (value) => {
return value.length > 5;
},
message: 'phone must be longer than five characters',
},
],
errors: [],
valid: false,
state: '',
},
};
const [values, setValues] = React.useState({
ssn: '',
phone: '',
email: '',
country: '',
});
const handleChange = name => event => {
setValues({ ...values, [name]: event.target.value });
};
return (
<React.Fragment>
<div>
<FormGroup autoComplete="on">
<TextField
id=""
label="Phone"
value={values.phone}
onChange={handleChange('phone')}
type="number"
validators={validator}
name='phone'
/>
</FormGroup>
</div>
</React.Fragment>
);
}
export default App;
I want if the user inserts values less than 5, they will get an error message, and if they don't insert numbers, they will get another error message.
I am going through the trouble to implement this simple validation
I need help, it will be much appropriated if anyone can help me in this case.
I have already written a validator with regex but I don't know how to implement this to show the error message.
Can anyone help me, please?