Reputation: 2775
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.
Upvotes: 1
Views: 5140
Reputation: 4501
I think your regexp is just treated as string
. So modify them like this.
before:
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]$/";
after:
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]$/;
Upvotes: 2