24 hour time regex for HTML 5 - regex

Wanted to Inquire about the possible Regex expression for 24-hour time format in HTML 5 (HH:MM) .
if possible, kindly tell the regex that can be used in the Pattern attribute of HTML 5
The time is expected to be in 24-hour format (HH not more than 23).
Kind regards,

I think this is a possible approach :
<input type="text" pattern="([01]?[0-9]|2[0-3]):[0-5][0-9]" id="24h"/>
<input type="text" pattern="([01]?[0-9]{1}|2[0-3]{1}):[0-5]{1}[0-9]{1}" id="24h"/>
http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/
([01]?[0-9]|2[0-3]):[0-5][0-9]
Check out this jsfiddle : example

Here is the code:
<input type="text" pattern="[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}" />
it does allow invalid hour values: 24,25,26,27,28,29, if you want to be extra correct you can do it that way:
<input type="text" pattern="([0-1]{1}[0-9]{1}|20|21|22|23):[0-5]{1}[0-9]{1}" />

If you want to force to have the format HH:MM (e.g. 00:00, 23:59)
Then, you could use something like this:
/^([01]\d|20|21|22|23):[0-5]\d$/

A bit shorter regex:
(?:[01]|2(?![4-9])){1}\d{1}:[0-5]{1}\d{1}
So in complete:
<input type="text" pattern="(?:[01]|2(?![4-9])){1}\d{1}:[0-5]{1}\d{1}" />
In the first non-capturing group ("(?:)") we match exactly one digit, either 0, 1 or 2 not followed by 4-9 (negative lookahead "(?!)"). Then me match one more digit, since it could be any of 0-9 we can go with \d shortcut. Then we match separator ":". Then one digit between 0-5 and one more between 0-9 (again with "\d").
If for some reason you need to match 24 hours as well (sometimes you do), then just adjust negative lookahead, e. g. "(?![5-9])".

Related

Regex match 3 characters followed by integers

I am new to regex expression and I need a regex in the following pattern:
The string must have a format of “TCK#”. TCK followed by integers.
For example, This is acceptable TCK123. This is not acceptable 123
Here is my current regex expression:
input class="form-control" required="true" type="text" name="TCKInput"
pattern="^[TCK][0-9]$">
With my current code, when the user enter TCK123, it is not acceptable, which is not what I am looking for
Change to below regex:
^(?:TCK)[0-9]+$
Demo: https://regex101.com/r/h9V7n1/1
Changes in the existing Regex you were using:
1) You were using [, ] around TCK which means regex has to match
any one of the values inside this bracket. As you have to match TCK
as it is, change it to (, )
2) You didn't mention + after [0-9] which means exactly one
occurrence will be matched. However, if you will mention +, it will
match one or more occurrence
If you want all 3 letters: TCK and then at least one or more digits after it, then try this:
^TCK\d+$
If you use [TCK] that will only accept one T, one C, or one K
Demo
This Demo sends to a live test server, so a successful submission of data will result in a response from said server
<form id='main' action='https://httpbin.org/post' method='post'>
<input class="form-control" required="true" type="text" name="TCKInput" pattern="^TCK\d+$">
<input type='submit'>
</form>

AngularJS ng-pattern with or within textbox

I need to limit an input box to either accept a number or a certain string pattern
<input type="text" ng-model="value" name="value" ng-pattern="/^([0-9])|([R])$/" required>
This does not seem to fully work, i can add 555f and this will be a valid value for my ng-model
How can i limit this to either match a digit or my string pattern?
http://jsfiddle.net/DaleS/dv7vuecz/
You have a problem with your reg exp in ng-pattern, this: /^([0-9])|([R])$/ will mach any string that starts from a number or ends with 'R'. And for what you want, you need smth like this:
/^[0-9]+$|^[R]+$/
It will match any string that is number, or starts and ends with 'R'.
^([0-9]|[R])$
Try this.See demo.
https://regex101.com/r/eZ0yP4/10

Validate <input type="number" /> with AngularJS and Pattern/RegEx

I have a input of type number, and I want to make sure it only accepts a number. I can do this fine on the Server side, but with AngularJS I can not get it to work.
Here's the code:
<input type="number" ng-pattern="/[0-9]+/" name="numOfFruits" ng-model="basket.numOfFruits" />
I suspect this has something to do with the pattern I am supplying [0-9]+ basically I only want numbers in this text box, anything that is not made up of the numbers 0 to 9, I want the input invalid.
At the moment, my input field sees this aa23423 as valid input!
You need to use anchors:
/^[0-9]+$/
^: Start-of-line anchor.
[0-9]+ One or more numbers between 0 and 9.
$: End-of-line anchor.
So this matches the start of the string, then it matches the one or more digits, after that it looks for the end-of-string, so it matches a string containing only numbers and nothing else.
Otherwise, /[0-9]+/ will match only a part of aa23423, more accurately the number 23423 and thus will give you valid.
Here is regexp to validate floating point numbers, both positive and negative:
/^-?[0-9]\d*(\.\d+)?$/
Use this regexp in 'text' input, example:
<input type="text" ng-model="score" ng-pattern="/^-?[0-9]\d*(\.\d+)?$/" required/>
Pattern don't work for input with type="number".
You can use type="text" and than convert value to number
Try defining your regex as a scope variable
In the controller, it worked for me.

How can I write a regular expression to validate my HTML input tag?

I want to specify a pattern for my <input> tag such that it matches any string having length 7, starting with ab- followed by 4 digits.
How would I write a regular expression for this?
The pattern you use in your comment is correct - just make sure to keep it within a <form> tag with a submit button.
<form>
<input type="text" pattern="[A|a][B|b][-][0-9]{4}">
<input type="submit">
</form>
A regex to match ab- followed by 4 digits would look like:
ab-\d{4}
Or for case insensitive matches:
[Aa][Bb]-\d{4}

Keeping Regex search to one line

I used Wget to scrape a site for migrating to new platform. I am trying to clean up the pages and remove all the viewstate code in them. I am using the following regex expression to do this:
<input type="hidden" name="__VIEWSTATE" value=.*/>
This works in programs like dreamweaver. I like to use another application called Wild Edit which is extremely fast for search and replace for large number of files. When I use that same expression it will match to the last /> on the page remove alot of good code. I have also tried <input type="hidden" name="__VIEWSTATE" value=.*/>$ with same results.
How would I constrain this to keep it to the first match of />
Try
<input type="hidden" name="__VIEWSTATE" value=.*?/>
The ?, if it's supported makes the search ungreedy so it will only match until the first /> rather than the last.
If that doesn't work, your best bet may be:
<input type="hidden" name="__VIEWSTATE" value=[^/]+/>
The regex is being too greedy. Try this:
<input type="hidden" name="__VIEWSTATE" value=.*?/>
By default, the regex engine tries to make as large of a match as possible. For example, the regular expression a.*z will match az (some other middle stuff) az as one big match, since, well, it does start with a and end with z.
The ? modifier tells the regular expression engine to, rather than be greedy, be lazy: instead of grabbing the largest possible match, grab the smallest. In the previous example, the regex a.*?z will just match the 2 az substrings, because it's being lazy: once it sees the z, it stops.