【发布时间】:2016-01-08 18:50:09
【问题描述】:
在我的信用卡结帐表格中,我有以下内容:
$('#checkout-info')
.form({
on : 'blur',
fields : {
id_cc_num : { identifier: 'id_cc_num',
optional: true,
rules: []},
id_cc_CVC : { identifier: 'id_cc_CVC',
optional: true,
rules: [{ type: 'regExp[/\d{3,4}/]',
prompt: 'CVC must be three or four digits'}]},
id_cc_month : { identifier: 'id_cc_month',
optional: true,
rules: [{ type: 'integer[1..12]',
prompt: 'MM must be a two-digit month designator - 01 thru 12'}]},
id_cc_year : { identifier: 'id_cc_year',
optional: true,
rules: [{ type: 'integer[2016..2036]',
prompt: 'Year must be at least 2016'}]},
inline : 'true'
});
显示的所有验证都正常工作,除了 regExp 验证。正则表达式 /\d{3,4}/ 通过 www.regexr.com 中的正则表达式测试,但它不会通过语义正则表达式测试。如果 CVC 字段上的 regExp 替换为以下内容,那么它可以工作,但我更喜欢 regex 的简洁性:
rules: [{ type: 'minLength[3]',
prompt: 'CVC must be three or four digits'},
{ type: 'maxLength[4]',
prompt: 'CVC must be three or four digits'}]},
【问题讨论】:
-
regExp 是用于验证正则表达式的语义 UI 关键字,因此在语义 UI 的上下文中,它在语义上是正确的:semantic-ui.com/behaviors/form.html#validation-rules
-
是的,我已经尝试过了,同样,前导 ^ 和尾随 $,但没有成功。看来这一定是图书馆的一个小故障。
-
我只是在 SO 上搜索并看到 this answer ... 但是,如果文档说这种方式是正确的,那么它就是。 ':)
标签: javascript regex validation semantic-ui