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

x
?
Solved

Regular expression

Posted on 2011-09-14
15
Medium Priority
?
150 Views
Last Modified: 2013-11-20
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
Comment
Question by:vkchaitu82
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 35

Expert Comment

by:Terry Woods
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 83

Expert Comment

by:leakim971
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

by:Terry Woods
ID: 36540485
What format do you want for time?

And for email: http://www.regular-expressions.info/email.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:Terry Woods
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

by:jimmart
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

by:hielo
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

by:vkchaitu82
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?

Also can you  pl tell me about this. (0[1-9]|[12][0-9]|3[01])
0
 
LVL 1

Accepted Solution

by:
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

by:Terry Woods
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

by:vkchaitu82
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

by:vkchaitu82
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 83

Expert Comment

by:leakim971
ID: 36555221
0
 
LVL 35

Assisted Solution

by:Terry Woods
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

Industry Leaders: 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!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

824 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