使用 formik 进行文件名验证 yup
我正在使用 YUP 和正则表达式应用文件名验证。每次上传文件时都会显示错误,文件名不应以特殊字符开头。请参考codesandbox链接:代码链接< /a> 并查看行号。 22
I am applying file name validations using YUP with regex. It's showing an error every time when the file is uploaded, the file name should not start with special characters. Please refer to the codesandbox link: Link to Code and see line no. 22
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的正则表达式将是
^[0-9a-zA-Z].*
,稍微解释一下原因和方式:现在要使其在您的示例中工作,您需要检查
value.name
是否与所述正则表达式匹配,这就是您实现此目的的方法:一旦您将尝试上传开头包含特殊字符的内容,它应该显示错误。
我还更新了 codesandbox ,所以请看一下
The regex you need will be
^[0-9a-zA-Z].*
, a little explanation of why and how:Now to make it work in your example you need to check if
value.name
will match said regex, this is how you can achieve this:Once you will try to upload something that will have special characters at the start it should show error.
I have updated also codesandbox, so please have a look