首页 > 解决方案 > 如何验证 Yup 中的非必需文件

问题描述

如何验证不应包含以下特殊字符 #<`>和点(。)之前的空格的非必需文件我有正则表达式\`|\#|\&|\<|\ \.|\>来验证上述条件,但不知道如何使用 yup.matches() 来处理这个正则表达式。提前致谢

Regex: \`|\#|\&|\<|\ \.|\> 

我的验证模式是:

const validationSchema = function (values) {
  var regx = new RegExp(/\`|\#|\&|\<|\ \.|\>/gms);
    return Yup.object().shape({
      about: Yup.string()
      .matches(expression, 'about should not contain ` # < > \n')

    })
  }

标签: regexreactjsformikyup

解决方案


假设您的正则表达式有效,您可以使用string.matches函数。这是文档中的示例:

var v = string().matches(/(hi|bye)/);
v.isValid('hi')
  .should.eventually()
  .equal(true);
v.isValid('nope')
  .should.eventually()
  .equal(false);

推荐阅读