[英]How to validate a field that is not required with a regex with JQuery
I have a form in my web and I'm using the JQuery validation plugin. 我在我的网页上有一个表单,我正在使用JQuery验证插件。 I have a 'comment' that I want to be NOT required (it can be empty), but has to match a specific regular expression if filled. 我有一个'评论',我不想要它(它可以是空的),但必须匹配特定的正则表达式,如果填写。
Using this, if the field is empty, the validation returns false (not valid), and the error is shown: 使用此选项,如果该字段为空,则验证返回false(无效),并显示错误:
'comment':{
required: false,
regexp: /^([-a-z0-9_ ()])+$/i,
},
How can I achieve the intended behaviour? 我怎样才能实现预期的行为?
Try: 尝试:
/^[-a-z0-9_ ()]+$|^$/i
I just added another match for emptiness 我刚刚为空虚添加了另一场比赛
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.