简体   繁体   English

如何使用 material-ui、formik、yup 和 react-input-mask 正确验证屏蔽输入?

[英]How to correctly validate an masked-input using material-ui, formik, yup, and react-input-mask?

I'm trying to validate my input using formik/yup/material-ui/ and react-input-mask.我正在尝试使用 formik/yup/material-ui/ 和 react-input-mask 验证我的输入。 From validationSchema only one option works correctly: .required.从validationSchema 中,只有一个选项可以正常工作:.required。 When i'm trying to validate by min/max length on the input it doesn't work properly.当我尝试通过输入的最小/最大长度进行验证时,它无法正常工作。

When I set a mask for the input equals to: {"99 9999"} it looks like yup recognizes it like 7 characters(6 digits and one for space) and it doesn't change when I am typing in input field.当我为输入设置掩码等于:{"99 9999"} 时,它看起来像 7 个字符(6 位数字和一个空格),并且在输入字段中输入时它不会改变。

For example when I set: .min(7, "Password must contain at least 7 characters")例如,当我设置: .min(7, "Password must contain at least 7 characters")

in validationSchema I will always get no errors even if I don't type anything in text field.在validationSchema 中,即使我没有在文本字段中输入任何内容,我也始终不会出错。

And when i set min(8, "Password must contain at least 8 characters") I will always get error feedback, even if I type something.当我设置min(8, "Password must contain at least 8 characters")我总是会得到错误反馈,即使我输入了一些东西。 Looks like the lenght of the input is always equals to length of the mask.看起来输入的长度总是等于掩码的长度。

There is my input field:有我的输入字段:

 <InputMask
    mask={"99 9999"}
    value={name}
    onChange={change.bind(null, "name")}
  >
    {() => (
      <TextField
        id="name"
        name="name"
        variant="outlined"
        helperText={touched.name ? errors.name : ""}
        error={touched.name && Boolean(errors.name)}
        label="Name"
        fullWidth
      />
    )}
 </InputMask> 

And there you can see my whole code:在那里你可以看到我的整个代码:

https://codesandbox.io/s/zrrxol5614 https://codesandbox.io/s/zrrxol5614

What am doing wrong?做错了什么?

Your Example你的榜样

const validationSchema = Yup.object({
  name: Yup.string("Enter a name")
  .required("Name is required")
  .min(7, "Password must contain at least 7 characters")
});

With Transform to Unmasked转换为未屏蔽

const validationSchema = Yup.object({
  name: Yup.string("Enter a name")
  .required("Name is required")
  .transform(value => value.replace(/[^\d]/g, ''))
  .min(6, "Name must contain at least 6 characters")
});

Why?为什么?

If the placeholder characters and the space are present in the input, then they are going to be sent with the value to the validator.如果输入中存在占位符字符和空格,则它们将与值一起发送到验证器。 You can unmask them along the way or run a transform as shown above.您可以沿途取消屏蔽它们或运行如上所示的转换。 This particular transform simply removes all non-digits from the string before validating, so you'd have to change it according to your needs.这个特定的转换只是在验证之前从字符串中删除所有非数字,因此您必须根据需要更改它。

You need to use trim()你需要使用trim()

const validationSchema = Yup.object({
  name: Yup.string()
  .required("Name is required")
  .test(value => value.trim().length > 6)
});

Trim help you to remove space from the output of input mask Trim 帮助您从输入掩码的输出中去除空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM