Solved

Regex for Date onkeypress

Posted on 2011-02-18
8
1,491 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 44
Where can I ask about my ASP.NET MVC project? 6 35
Gridview alignment 1 19
Question about JQuery and asp.net 3 24
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

805 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