User1985 User1985 - 2 months ago 13
AngularJS Question

Regex pattern matching for input type time

To check whether input type "time" field is completed (09:00am) I have used a regular expression.

ng-pattern="\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm]))"

But in the same regular expression I want to check whether the input field is empty. For further information, time field can be empty or completed (ex: )(09:30am)

Can anyone help me regarding this..


In an ng-pattern, you need to use


and if you need to avoid leading/trailing spaces, also add ng-trim="false".

See this regex demo.

The (?:...)? optional non-capturing group is a wrapper for the whole pattern that becomes optional, i.e. can match an empty string.

The ^ anchor will only match at the start of the string, and $ will anchor the match at the end of the string, so that an entire string should match.