Solved

Regular expression

Posted on 2011-09-14
15
138 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Numbers in JQuery file 11 66
Google Analytics setup 5 50
xjs: how to get sum of value in for loop 2 19
Using toggleClass to resize Bootstrap columns 7 21
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

860 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