React,Formik,Yup:如何格式化电话输入

问题描述

我是React的新手,我已经完成了一个多步骤表单,用于使用React引导程序,Formik进行注册以及使用Yup进行验证。我想添加一些功能,使我可以在几个字段中格式化(或屏蔽用户输入。
用户键入时,我想将电话输入格式化为以下格式:111-111-1111。要格式化的邮政编码:N0G 1A3。我该怎么做?有什么建议?我需要添加任何库吗?我该怎么办?

这是我做的代码

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>
);
};

解决方法

您可以使用https://github.com/sanniassin/react-input-mask

https://www.npmjs.com/package/react-input-mask

示例非常详细。我将此用于Yup和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>

如果您不使用普通的<input />,则必须像我一样包装元素。您可以在NPM页面上找到有关此内容的讨论。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...