【问题标题】:Date RegularExpression does NOT work on FireFox日期正则表达式在 FireFox 上不起作用
【发布时间】:2014-10-31 11:52:33
【问题描述】:
^(0[1-9]|[12][0-9]|3[01])[-\/](0[1-9]|1[012])[-\/](19|20)\d\d$

在 Firefox 33.02 中,10/10/2014 有效,但 10-10-2014 无效。

但是在 chrome 中 10/10/201410-10-2014 都可以工作。

为什么?

编辑:

我发现不是表达式无效,而可能是 MVC 中的某些东西

型号:

[Required(ErrorMessage = "Field is required")]
[RegularExpression(@"^(0[1-9]|[12][0-9]|3[01])[-\/](0[1-9]|1[012])[-\/](19|20)\d\d$", ErrorMessage = "Date is incorrect")]
[DataType(DataType.Date)]
public DateTime BevBday { get; set; }

cshtml:

@Html.TextBoxFor(model => model.BevBday)

使用这个,在 Firefox 中它会显示为

BevBday 字段必须是日期。

在 chrome 中它不会给出同样的错误。

【问题讨论】:

  • 好吧……我现在看到了“c#”标签。所以这是某种服务器代码(asp.net?),它动态生成html/js?这从最初的问题中并不明显。

标签: c# regex asp.net-mvc


【解决方案1】:

为我工作(在 Firefox 的网络控制台中测试)

var re = /^(0[1-9]|[12][0-9]|3[01])[-\/](0[1-9]|1[012])[-\/](19|20)\d\d$/;
// /^(0[1-9]|[12][0-9]|3[01])[-\/](0[1-9]|1[012])[-\/](19|20)\d\d$/

re.test('10/10/2014');
// true

re.test('10-10-2014');
// true

【讨论】:

  • 谢谢,我更新了这个问题,这仍然对你有用吗?
【解决方案2】:

改成这个,现在可以了。

    [Required(ErrorMessage = "Field is required")]
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd.MM.yy}", ApplyFormatInEditMode = true)]
    public DateTime BevBday { get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2018-10-05
    • 2020-07-08
    • 2015-08-25
    相关资源
    最近更新 更多