How to use regex to validate Date and Time using Match in Jquery

I am trying to validate the Time and Date entered by users with JQuery first before sending the data for another validation at the back side.

I want this because I see it as a waste to contact the back side when I can validate the data before sending them over.

I use regex with pattern similar to PHP's preg_match. It has so far worked for validating E-mails and compelling potentials users of the Application to choose a password pattern but my regex for Time and Date is not working so far.

the date format is YYYY-MM-DD and time format is HH:MM. The Time is a 24 hour time.

Here is what I have tried so far;

var mydate=$('#mydate').val();
var mytime=$('#mytime').val();

var validDate="/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
var validTime="/^(2[0-3]|[01][0-9]):[0-5][0-9]$/";

if (!mydate || mydate==null || !mydate.match(validTime)){ return Alert("Enter valid Time to continue");}
if (!mytime || mytime==null || !mytime.match(validDate)){ return Alert("Enter valid Date to continue");}

Please, any help will do.

Note: Regex is not particularly my strong point.


I think your regexp is just treated as string. So modify them like this.


var validDate="/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/";
var validTime="/^(2[0-3]|[01][0-9]):[0-5][0-9]$/";


var validDate=/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
var validTime=/^(2[0-3]|[01][0-9]):[0-5][0-9]$/;

Need Your Help

How do you use Suds to connect to a SOAP API on a private network from a public web server?

python suds

I have a website that needs to connect to a SOAP API that's behind a firewall on a private network. I've tried a couple things with various levels of success.

Spring Security: authorize on two or more web applications simultaneously

spring security authentication spring-security simultaneous

Can you please give me a hint, how to authorize user on few web applications at the same time?