最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular Expression for HTML5 input type=time - Stack Overflow

programmeradmin2浏览0评论

I am trying to find a regular expression that plays nicely with the input type of 'time'. I want to use the new HTML5 input type=time, because this form will mostly be accessed on a mobile device, so it would be nice if the user was presented with a time picker.

However, some browsers don't do anything special with that input type, so I still require a regular expression.

I've tried both these examples:

Validating time-only input in asp MVC unobtrusive validation

24 hour time regex for HTML 5

They work when the input type=text, however they fail when I change it to time.

Thank you

EDIT

The regular expression should validate the time with AM/PM, however it doesn't matter if they enter in a leading 0 (03:00AM) or no minutes (3am).

Essentially it just needs to work with a time input:

<input data-val="true" data-val-regex="Invalid Time." data-val-regex-pattern="^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$" name="StartTime" type="time" value="" />

This returns invalid time when using the chrome time picker.

I am trying to find a regular expression that plays nicely with the input type of 'time'. I want to use the new HTML5 input type=time, because this form will mostly be accessed on a mobile device, so it would be nice if the user was presented with a time picker.

However, some browsers don't do anything special with that input type, so I still require a regular expression.

I've tried both these examples:

Validating time-only input in asp MVC unobtrusive validation

24 hour time regex for HTML 5

They work when the input type=text, however they fail when I change it to time.

Thank you

EDIT

The regular expression should validate the time with AM/PM, however it doesn't matter if they enter in a leading 0 (03:00AM) or no minutes (3am).

Essentially it just needs to work with a time input:

<input data-val="true" data-val-regex="Invalid Time." data-val-regex-pattern="^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$" name="StartTime" type="time" value="" />

This returns invalid time when using the chrome time picker.

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked May 13, 2015 at 15:48 Nick YoungNick Young 8951 gold badge10 silver badges22 bronze badges 3
  • 1 What should the regular expression exactly achieve? What are YOU trying to achieve? it's not quite clear. Could you add some code for what you have so far? – Nzall Commented May 13, 2015 at 15:50
  • The regex should make sure it's a valid time. It should force them to enter am/pm, however it doesn't matter if they enter 03:00AM or 3am or 3:00am or if there's a space between the time and the period. These two regex seem to work when the input type is "text", but not when it is set to "time". - ^(0?[1-9]|1[0-2]):[0-5][0-9] [aApP][mM]$ - ^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$ – Nick Young Commented May 13, 2015 at 15:59
  • 1 Post what you have so far, input strings that should match and input string that shouldn't match – Rodrigo López Commented May 13, 2015 at 16:04
Add a ment  | 

2 Answers 2

Reset to default 2

I have good and bad news...

The bad one is that you cannot use the pattern attribute in input types different from text, search, url, tel, email, and password.

While this is valid:

 <input type="text" pattern="([01]?[0-9]{1}|2[0-3]{1}):[0-5]{1}[0-9]{1}"/>

This would not be:

<input type="time" pattern="([01]?[0-9]{1}|2[0-3]{1}):[0-5]{1}[0-9]{1}"/>

The good news is that you may not need to use any pattern in type=time, since it already has a pattern of its own:

type=time (HH:MM)
- time value with no time zone in 24-hour military format.

EDIT

I noticed your edit with the following code:

<input data-val="true"
 data-val-regex="Invalid Time."
 data-val-regex-pattern="^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$"
 name="StartTime" type="time" value="" />

I also noticed that people using those data attributes, they still use type="text". I assume that is the reason why it "returns invalid time when using the chrome time picker".

I haven't find any case where type="time" works with other patterns (even with data-), and it seems this answer already explained it: html5 time inputs shows 12 hours

If you really need to have the AM and PM you can try to use type="text" with the regex pattern and trigger the datepicker through Javascript.

Okay, so based on Armfoot's response, I decided to use Modernizr to achieve cross-browser time input support. Like I mentioned in my question, the input type needs to be "time" for mobile purposes, because IMO the time picker improves user experience.

I'm not sure if this is the BEST approach, but here is what I did:

Here is the starting HTML for this particular form element:

<div class="form-group">
    <label for="StartTime">Time Started</label>
    <input class="form-control" type="text" data-type="time" data-val="true" data-val-regex="Time is invalid" data-val-regex-pattern="^(0?[1-9]|1[0-2]):[0-5][0-9]\s*[aApP][mM]\s*$" data-val-required="Start time is required" id="StartTime" name="StartTime" placeholder="Time Started"  value="" />
    <span class="field-validation-valid" data-valmsg-for="StartTime" data-valmsg-replace="true"></span>
</div>

The initial type is set to "text", and the regular expression for validating the time is:

^(0?[1-9]|1[0-2]):[0-5][0-9]\s*[aApP][mM]\s*$

I used the same expression from the link posted in my question, however I added "\s*" between the time and the AM/PM and also one afterwards, so the it doesn't matter how many spaces go between AM/PM or if the user accidentally adds a space afterwards. The 0? also makes the leading 0 optional.

For JavaScript, I added:

//detect if time input is supported
if (Modernizr.inputtypes.time) {
    $('*[data-type="time"]').attr('type', 'time');
    $('*[data-type="time"]').removeAttr('data-val-regex');
    $('*[data-type="time"]').removeAttr('data-val-regex-pattern');
}

The Modernizr conditional statement checks if the input type "time" is supported by the browser. If it is supported, it changes the type to "time", and removes the data-val-regex attributes since those are NOT supported for type="time".

This seems to work fine across all browsers and devices. I've tested it on Chrome/Chrome Mobile/IE/Firefox/iPad (Safari). The time pickers show up nicely on the iPad and the Nexus devices making this work well for mobile purposes. The regex works properly on Firefox and IE where the time input doesn't get rendered.

发布评论

评论列表(0)

  1. 暂无评论