Solved

Regular expression

Posted on 2011-09-14
15
135 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
15 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 82

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 84 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 166 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 82

Expert Comment

by:leakim971
ID: 36555221
0
 
LVL 35

Assisted Solution

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now