• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1696
  • Last Modified:

Regex for Date onkeypress

Is it possible to write a regular expression to Validate a date in the onkeypress event of a textbox?

The user should be able to enter dates in the following formats.

1. MM/dd/yyyy - 12/25/2011
2. MMMM dd, yyyy - December 25, 2011
3. dd MMM,yyyy - 25 Dec,2011

I have a dropdown in which the user will select the format of the date to be entered, so I would require 3 different Regex.

I can write a regex on blur event. But is it possible to write it in keypress event of textbox.
0
Obadiah Christopher
Asked:
Obadiah Christopher
  • 4
  • 4
1 Solution
 
BardobraveCommented:
The problem of using keypress event is that your regex comparison will be fired with each key pressing, so it will never pass until the last character is pressed (and thus it will return warning, error, or whatever are you using to alert your user of it each time).

Maybe you can launch it on keypress but only check for conformance with the regular expression when the correct number of characters are inserted.
0
 
Obadiah ChristopherAuthor Commented:
Exactly. But is it possible to prevent the user frm entering any other characters other than valid ones.
0
 
BardobraveCommented:
Of course you can, but not matching against a full regular expression (as in each keypress you'll only have a bit of the date builded).

You can use different reg exps for the different steps in the creation or use code to validate each keypress depending on what format you expect.

The problem in this is... what if your user copy/paste the date into your textbox? You should maintain the onBlur validation.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Obadiah ChristopherAuthor Commented:
Onblur validation is already present. But they want keypress also. I'm doing all these validations in onBlur event. The problem is for keypress event.
0
 
BardobraveCommented:
You can do a function for keypress event.

When if fired, this function check if the current value of the textField matches with the expected format for a string of it's length.

If the pressed key don't match, you can erase the last char from the textbox. This maybe don't work very well when users types fast, as maybe your function finishes it's execution after the next key is pressed, so the erasing of the last char will have a delay.

Probably will be better to simply show a message alerting user that introduced date doesn't match with the expected format instead of trying to block the pressing of erroneus keys.
0
 
Obadiah ChristopherAuthor Commented:
The regular expression is the thing that I'm looking for. Can somebody help me with writing this Regular Expression.
0
 
BardobraveCommented:
I think that probably you won't be able to build a single regular expression that matches every possible situation with different character numbers, but let's try a bit...

I'm not familiar with Perl regular expression flavour, so you'll need to adjust this to it

^\d{2}[\b|\,]?\d{2}[\b|\,]?\d{1}[\,]?\d{1}[\,\b]?\d{2}\d{1}?\d{1}?$

This is:

Two digits.
Followed by a blank space or a comma, that could exist or not.
Followed by another two digits.
Followed by a blank space or a comma, that could exist or not.
Followed by another digit
Followed by a comma that could exist or not
Followed by another digit
Followed by a comma AND a blank space, that could exist or not
Followed by two digits
Followed by one digit that could exist or not
Followed by one digit that could exist or not

I think that this could work for the three different syntax expected, but It probably won't work with every keypress, as while the string is not completely formed the regexp won't be completely matched.

Maybe you can combine different parts of this expression with OR clauses to obtain something that works, or better build several expressions with different parts to match in each keypress the regexp that corresponds to the number of chars you have inserted in that moment.

However this probably will fail again if a user types very fast.

For fine tuning the regexp and translate to perl flavour you can refer to the best place for regular expressions I've found ever:

http://www.regular-expressions.com

I hope this helps



0
 
Obadiah ChristopherAuthor Commented:
Accepting the answer for the regex. However, I assume that it is not possible to do it on keypress.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now