Reputation: 13
I'm new to React and I have done a multistep form for signing up using React bootstrap, Formik, and validation with Yup. I want to add functionality that will allow me to format (or mask) user inputs in a couple of fields.
While user typing, I want to format the phone input to this format: 111-111-1111. The postal code to format: N0G 1A3. How I can do this? Any suggestions? Do I need to add any libraries? What should I do?
Here is the code I did:
export const step1Scehma = Yup.object({
firstName: Yup.string()
.required("This field is required"),
lastName: Yup.string()
.required("This field is required"),
email: Yup.string()
.email()
.required("Email is required"),
password: Yup.string()
.required("This field is required")
.matches(
"^(?=.*[A-Za-z])(?=.*d)(?=.*[@$!%*#?&])[A-Za-zd@$!%*#?&]{8,}$",
"Must Contain at least 8 Characters: One Uppercase, One Lowercase, One Number and one
special case Character"
),
phone: Yup.string()
.required("This field is required")
});
export const Step1 = ({ formik }) => {
const { handleChange, values, errors, touched } = formik;
return (
<React.Fragment>
<Container>
<Card.Title className="text-center">New User</Card.Title>
<Card.Body>
<Form.Group as={Col}>
<Form.Label>First Name</Form.Label>
<Form.Control
type="text"
name="firstName"
onChange={handleChange}
value={values.firstName}
isValid={touched.firstName && !errors.firstName}
isInvalid={!!errors.firstName}
className={touched.firstName && errors.firstName ? "error" : null}
/>
{touched.firstName && errors.firstName ? (<div className="error-message">{errors.firstName}</div>): null}
</Form.Group>
<Form.Group as={Col}>
<Form.Label>Last Name</Form.Label>
<Form.Control
type="text"
name="lastName"
onChange={handleChange}
value={values.lastName}
isValid={touched.lastName && !errors.lastName}
isInvalid={!!errors.lastName}
className={touched.lastName && errors.lastName ? "error" : null}
/>
{touched.lastName && errors.lastName ? (<div className="error-message">{errors.lastName}</div>): null}
</Form.Group>
<Form.Group as={Col} >
<Form.Label>Email</Form.Label>
<Form.Control
type="text"
name="email"
onChange={handleChange}
value={values.email}
isValid={touched.email && !errors.email}
isInvalid={!!errors.email}
className={touched.email && errors.email ? "error" : null}
/>
{touched.email && errors.email ? (<div className="error-message">{errors.email}</div>): null}
</Form.Group>
<Form.Group as={Col} >
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
name="password"
onChange={handleChange}
value={values.password}
isValid={touched.password && !errors.password}
isInvalid={!!errors.password}
className={touched.password && errors.password ? "error" : null}
/>
{touched.password && errors.password ? (<div className="error-message">{errors.password}</div>): null}
</Form.Group>
<Form.Group as={Col} >
<Form.Label>Phone Number</Form.Label>
<Form.Control
type="tel"
name="phone"
onChange={handleChange}
value={values.phone}
isValid={touched.phone && !errors.phone}
isInvalid={!!errors.phone}
className={touched.phone && errors.phone ? "error" : null}
/>
{touched.phone && errors.phone ? (<div className="error-message">{errors.phone}</div>): null}
</Form.Group>
</Card.Body>
</Container>
</React.Fragment>
);
};
Upvotes: 0
Views: 4783
Reputation: 407
You can use https://github.com/sanniassin/react-input-mask
https://www.npmjs.com/package/react-input-mask
The examples are very detailed. I used this with Yup and Material UI.
<InputMask mask="999-999-9999" onChange={ handleChange } onBlur={ handleBlur } value={values.billing_phone} error={errors.billing_phone && touched.billing_phone} helperText={(errors.billing_phone && touched.billing_phone) && errors.billing_phone}>
{(inputProps) => <TextField {...inputProps} className={classes.MuiInputBase} id="billing_phone" variant="outlined" name="billing_phone" placeholder={addressObj.phone} />}
</InputMask>
If you're not using a plain <input />
you'll have to wrap your element like I did. You can find the discussion about this on the NPM page.
Upvotes: 1