Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Regular expression

Posted on 2011-09-14
Medium Priority
146 Views
I am looking for the regular expression for Date,Email,Time and a double.

Date:
RegExp("^([0-1][0-9])[\-]([0-3][0-9])[\-]([1][9][0-1][0-9])\$","g");
expecting date as mm/dd/19xx
The issue is : how can I tell that
the first part : is valid only from 01 to 12?
I have it has [0-1][0-9]. Issue is I am able to give input as 19 also? how can I write the condition 13 to 19 in the above expression?

Time also I have the same question how I can tell in the regular expression except this value?

Email
0
Question by:vkchaitu82
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +2

LVL 35

Expert Comment

ID: 36540480
Yes, ([0-1][0-9]) allows 19

(0[1-9]|1[012]) works

It's trickier for the day though as obviously 31 is valid only sometimes. Allowing up to 31, you can use:
(0[1-9]|[12][0-9]|3[01])

That gives you:
RegExp("^(0[1-9]|1[012])[\-](0[1-9]|[12][0-9]|3[01])[\-]([1][9][0-1][0-9])\$","g");

I used patterns from: http://www.regular-expressions.info/dates.html
0

LVL 82

Expert Comment

ID: 36540483
>I have it has [0-1][0-9]. Issue is I am able to give input as 19 also? how can I write the condition 13 to 19 in the above expression?

Replace it by : (0[1-9]|1[012])
0

LVL 35

Expert Comment

ID: 36540485
What format do you want for time?

And for email: http://www.regular-expressions.info/email.html
0

LVL 35

Expert Comment

ID: 36540559
If you don't feel like reading the linked page, a simple regex for email is:
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\$
0

LVL 1

Expert Comment

ID: 36540575
^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19)\d\d\$ will accept mm/dd/19yy.
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d\$ will accept mm/dd/19yy or mm/dd/20yy and will accept dash (-), space ( ), slash (/), or dot (.) as dividers.
This does not disallow a date such at 02/31/1999 however.
If you are using JavaScript I would use this function instead:
function isDate(datestring) {
if ( Object.prototype.toString.call(datestring) !== "[object Date]" )
return false;
return !isNaN(datestring.getTime());
}
0

LVL 82

Expert Comment

ID: 36540773
To take into account leap years, refer to:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22802940.html

ID:19823352  gives you the regex for mm/dd/yyyy date format

ID:19818613 gives you the regex for dd/mm/yyyy date format
0

Author Comment

ID: 36540978
Terry,
I am using this as you suggested:
RegExp("^(0[1-9]|1[012])[\- ](0[1-9]|[12][0-9]|3[01])[\- ]([1][9][0-1][0-9])\$","g");. its not working?

0

LVL 1

Accepted Solution

jimmart earned 336 total points
ID: 36542398
(0[1-9]|[12][0-9]|3[01]) means:
a 0 followed by a 1 to 9
or
1 or 2 followed by a 0 to 9
or
3 followed by a 0 or 1
0

LVL 35

Assisted Solution

Terry Woods earned 664 total points
ID: 36546375
The pipe is like a logical OR for value contained within the round brackets, so

(0[1-9]|[12][0-9]|3[01])
matches pattern
0[1-9]
or pattern
[12][0-9]
or pattern
3[01]

Does that make sense?
0

Author Comment

ID: 36554702
Thanks Terry and Jummart.

Last question on this :
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\$

what does %,_ and - signify?
0

Author Comment

ID: 36555212
Thanks Terry and Jummart.

Last question on this :
^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\$

what does %,_  signify
0

LVL 82

Expert Comment

ID: 36555221
0

LVL 35

Assisted Solution

Terry Woods earned 664 total points
ID: 36555991
% and _ just the literal characters.

[A-Z]{2,4} means 2 to 4 occurrences of any character between A and Z, such as:
BY
PPP
ABCD
0

## Featured Post

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read bigâ€¦
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript â€¦
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQueryâ€¦
###### Suggested Courses
Course of the Month9 days, 14 hours left to enroll