简体   繁体   English

使用 yup 和 formik 验证密码

[英]password validation with yup and formik

how would one go about having password validation but at the same time having the errors be passed to different variables?如何进行密码验证但同时将错误传递给不同的变量?

ie IE

password: Yup.string().required("Please provide a valid password"),
passwordMin: Yup.string().oneOf([Yup.ref('password'), null]).min(8, 'Error'),
passwordLC: Yup.string().oneOf([Yup.ref('password'), null]).matches(/[a-z]/, "Error" )
passwordUC: Yup.string().oneOf([Yup.ref('password'), null]).matches(/[A-Z]/, "Error" )

I cant get the binding of the password variables to bind with the password object我无法获取密码变量的绑定以与密码对象绑定

Just to elaborate on efleurine's answer.只是为了详细说明 efleurine 的答案。
You do not need to store each validation on the same field - you can chain them to get a full validation.您不需要将每个验证存储在同一字段中 - 您可以将它们链接起来以获得完整的验证。

password: Yup.string()
  .required('No password provided.') 
  .min(8, 'Password is too short - should be 8 chars minimum.')
  .matches(/[a-zA-Z]/, 'Password can only contain Latin letters.')

Note how you still can specify individual messages for each failure.请注意您仍然可以如何为每个失败指定单独的消息。
Also, for the binding to work, make sure the form input you're binding to has an appropriate name attribute - in this case, password .此外,要使绑定正常工作,请确保您要绑定的表单输入具有适当的name属性 - 在本例中为password

Hope this helps:希望这可以帮助:

import * as Yup from 'yup';

validationSchema: Yup.object({
  password: Yup.string().required('Password is required'),
  passwordConfirmation: Yup.string()
     .oneOf([Yup.ref('password'), null], 'Passwords must match')
});

I use yup-password for this.我为此使用yup-password Example:例子:

import * as Yup from 'yup';
import YupPassword from 'yup-password';
YupPassword(Yup);

const requiredField = () => Yup.string().required('required');    
const passwordField = () =>
  requiredField()
    .min(
      8,
      'password must contain 8 or more characters with at least one of each: uppercase, lowercase, number and special'
    )
    .minLowercase(1, 'password must contain at least 1 lower case letter')
    .minUppercase(1, 'password must contain at least 1 upper case letter')
    .minNumbers(1, 'password must contain at least 1 number')
    .minSymbols(1, 'password must contain at least 1 special character');

In this example I'm validating for min number of characters, lower and uppercase chars, numbers and symbols, but you can add or remove specifications.在此示例中,我正在验证最小字符数、大小写字符、数字和符号,但您可以添加或删除规范。 You can set a maximum amount of chars, for instance.例如,您可以设置最大数量的字符。

The beauty of this library is that you get to specify the error message, and it also handles special characters for you, which is great because it saves you from using RegEx.这个库的美妙之处在于您可以指定错误消息,并且它还为您处理特殊字符,这非常棒,因为它使您免于使用 RegEx。 Have fun!玩得开心!

I know you can chain validations together for the same element.我知道您可以将同一元素的验证链接在一起。 If you are trying to do cross-validation then this article would help you.如果您正在尝试进行交叉验证,那么本文将对您有所帮助。 it about formik in react but I bet it would give you an idea how to solve your case.它是关于 formik 的反应,但我敢打赌它会给你一个如何解决你的案子的想法。

https://itnext.io/simple-react-form-validation-with-formik-yup-and-or-spected-206ebe9e7dcc https://itnext.io/simple-react-form-validation-with-formik-yup-and-or-spected-206ebe9e7dcc

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

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