Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regex for Date onkeypress

Posted on 2011-02-18
8
Medium Priority
?
1,605 Views
Last Modified: 2012-05-11
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
Comment
Question by:informaniac
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34925058
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
 
LVL 20

Author Comment

by:informaniac
ID: 34925101
Exactly. But is it possible to prevent the user frm entering any other characters other than valid ones.
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34925177
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:informaniac
ID: 34925331
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
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34925446
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
 
LVL 20

Author Comment

by:informaniac
ID: 34926692
The regular expression is the thing that I'm looking for. Can somebody help me with writing this Regular Expression.
0
 
LVL 19

Accepted Solution

by:
Bardobrave earned 1500 total points
ID: 34932476
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
 
LVL 20

Author Closing Comment

by:informaniac
ID: 34978515
Accepting the answer for the regex. However, I assume that it is not possible to do it on keypress.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question