javascript validation for input time in format hh:mm a - javascript

I want to make user enter time in format "hh:mma" or "hh:mm a". I want to leave it to user the choice to enter space between mm & a.
What can be the regular expression for this ?

Use ? to make the preceding token optional, for example /colou?r/ matches color and colour. If you are struggling with the hh:mm part of the regex, this may help.

Try this:
(([0-1]?[0-9])|2[0-3]):[0-5][0-9] ?(am|pm|AM|PM)

Something like the following should work
([01]?[0-9]|2[0-3]):([0-5]?[0-9])\s?(am|pm)

I just put space in the pattern where I required space and if worked perfectly

Related

javascript textbox not to allow special characters

I have a problem in validating Textbox in which I have to enter time.
in that only the symbol
":" is allowed,
text also should not allow.
how to do this..
and
I am following the pattern /^\d{1,2}:\d{2}$/
if anyone help me would be appreciated.
thanx in advance.
try and use following regular expression for validating HH:MM time format
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
try below pattern:
/^\d{2,}:\d{2}:\d{2}$/

How to modify time regexp?

I am newbie in Regular Expressions.
I need to check this time 18:00(00:00, 01:00, 05:12, any time of this format) by regexp, is it correct or no.
Here is my variant:
/[0-9][0-9]:[0-9][0-9]/
Can I change [0-9][0-9] to something like 2*[0-9] ???
If you specifically want to match 18:00 you should use /^\d{2}:\d{2}$/. The answers provided so far will match aaa12:99ddd. The ^ and $ are anchors and will instruct the regex to match specifically the given string.
If you are after a 24 hour validation regular expression you could use this: /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/ (taken from here).
You can also write it like this: /^\d{2}:\d{2}$/
You can modify it by adding a repetition clause:
/^[0-9]{2}:[0-9]{2}/
But you should add the starting and ending clauses :
/^[0-9]{2}:[0-9]{2}$/

Regular expression in javascript to check time with date

I need to check whether the text contains a date part with it.
for ex: mytext_12/26/2011_11:51_AM or someText_12/26/2011_13:51_PM have a date part it returns true.
I am not too good with expressions so looking for one. the format of the date - time part is fixed.
i got a way out for it but failing for time .
var containsDate = ~str.search(/\d{1,2}\/\d{1,2}\/\d{4}/);
it checks the date part perfectly but when i am trying for the time part i am messing it up some where .
_\d{1,2}:\d{2}_(?:AM|PM) this is the part for time but i am not able to generate the final regex by combining this two.
Try,
.search(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}_[0-9]{2}:[0-9]{2}_(AM|PM)/)
A good resource for regex,
MDN:Regular Expressions
Try,
.search(/\d+\/\d+\/\d{4}_\d+:\d+_(AM|PM)/)

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation

regex - date format OR not specified/empty

I have a regex problem that I need some advice on. I am using a jquery plugin that validates input fields on a JSP. I am using the date validation. It all works fine but this field is not required and is not marked as required. When the user hits submit it shows a warning on the date input field since the empty string does not match the validation of a date. I need some way to say that nothing or a date is valid in a regex.
Here is the regex for the date; is there any way to put "or empty" into this also:
"date":{
"regex":"/^[0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4}$/",
"alertText":"* Invalid date, must be in MM-DD-YYYY format"},
Thanks in advance
Try:
/^([0-9]{1,2}\-\[0-9]{1,2}\-\[0-9]{4})?$/
It will then match when a valid date is supplied, or the field is left blank.
(^[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}$|^$)

Categories