[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Regular expression in javascript

I want regular expression for following format of data time which check all the condition like hour not gretaer than 60 min not greater than 60 ss not greater than 60 with same like date date should not be greater than 31 whataever like that...
FORMAT

15-03-2010 14:52:49
dd-mm-yy    hh:mm:ss

Please give me solution.. which check all the condition wrt date and time


0
Adwait Chitaley
Asked:
Adwait Chitaley
  • 3
  • 3
  • 2
1 Solution
 
sompol_kiatkamolchaiCommented:
I think this is what you're looking for.

http://regexlib.com/REDetails.aspx?regexp_id=610

Hope this help,
Sompol
0
 
Adwait ChitaleyAuthor Commented:
31/12/2003 11:59:59 PM

This is for dd/mm/yy

But i want the format like dd-mm-yy  hh:mm:ss

Beacause if i m going to change in this expression then it will not make correct by me
So please if u can do then please give me expression or any other link that provide the excact result...
0
 
siddagrlCommented:
date in javascript could be validate as follows...

1. convert the date string in date object (regex parsing).
2. get the string representation of date object (using date.format.js from [http://blog.stevenlevithan.com/archives/date-time-format]).
3. compare both and if both the same then date string is valid.

Attached is the code that does that.
<html>

<head>
   <title>Date validation</title>
   <script type="text/javascript" src="date.format.js"></script>
</head>

<body>

<script>
function getDateFromStr (str_datetime) {
    // could be tweaked as per need
	var re_date = /^(\d\d)\-(\d\d)\-(\d\d)\s+(\d+)\:(\d+):(\d+)$/;

    if (re_date.exec(str_datetime))
    {
        // 1 - day, 2-month, 3-year, 4-hour, 5-min, 6-se
        // Constructor: Date(year, month[0-11], day, hours, minutes, seconds)
        return (new Date (2000+parseInt(RegExp.$3), RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
    }
    else
    {
        // invalid Datetime format, return current time
        return new Date();
    }
}

var inDate = "31-12-03 11:59:59";

var d = getDateFromStr(inDate);
var outDate = d.format('dd-mm-yy hh:MM:ss');

// one line could be saved
//var outDate = (getDateFromStr(inDate)).format('dd-mm-yy hh:MM:ss');

if (inDate == outDate)
    alert(inDate + '\nis valid');
else
    alert(inDate + '\nis invalid');

</script>

Open in new window

date.format.js
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
siddagrlCommented:
0
 
sompol_kiatkamolchaiCommented:
Here is the modified version.
Just change from [-./] to [-] which will allow only -

^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$


Hope this help,
Sompol
0
 
Adwait ChitaleyAuthor Commented:
Thanks siddagrl: your answer is nearly about the correct format in which i want .

This is your script in which i can check only format like 31-12-03 12:59:59 means dd-mm-yy hh:mm:ss  

but the just littel bit change in that i want like    dd-mm-yyyy (i.e 31-12-2009) and hh:mm:ss (i.e 14:59:59 or it check 23:59:59 both are the correct format )


In your script if i check format like 31-12-2009 12:59:59 its give the invalid date format
or if i check 31-12-09 14:59:59 its also gives the invalid format..

I want to check like 31-12-2009 23:59:59 (which is correct format in my requirment) so please give me the script which check this( 31-12-2009 23:59:59) format
0
 
siddagrlCommented:
attached is the updated code...
<html>

<head>
   <title>Date validation</title>
   <script type="text/javascript" src="date.format.js"></script>
</head>

<body>

<script>
function getDateFromStr (str_datetime) {
    // could be tweaked as per need
        var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+):(\d+)$/;

    if (re_date.exec(str_datetime))
    {
        // 1 - day, 2-month, 3-year, 4-hour, 5-min, 6-se
        // Constructor: Date(year, month[0-11], day, hours, minutes, seconds)
        return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
    }
    else
    {
        // invalid Datetime format, return current time
        return new Date();
    }
}

//var inDate = "31-12-2003 14:59:59";
var inDate = "31-12-2009 23:59:59";

var d = getDateFromStr(inDate);
var outDate = d.format('dd-mm-yyyy HH:MM:ss');

// one line could be saved
//var outDate = (getDateFromStr(inDate)).format('dd-mm-yy HH:MM:ss');

if (inDate == outDate)
    alert(inDate + '\nis valid');
else
    alert(inDate + '\nis invalid');

</script>
</body>
</html>

Open in new window

0
 
Adwait ChitaleyAuthor Commented:
Thanks very much ......
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now