0

For some reason, my date validation allows fullstops to be passed, but only at the end of that part of the date, e.g. 12./10/201 gets passed but 1.2/10/201 doesn't.

Here is my code:

var iDate = $("check_date").value;
 if(iDate.length > 0) {
 var a = iDate.split("/"); 
 if(isValidDate(a[0],a[1]-1,a[2]) == false){
 alert("You have entered an invalid date.");
 return false;
 }
isValidDate = function(day,month,year) {
 var dteDate;
 dteDate=new Date(year,month,day);
 return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}

Any ideas?

asked Dec 13, 2012 at 12:17
8
  • 2
    Well, I'd say that 12. is interpreted by JavaScript as 12, so it's comparing 12 and 12, but 1.2 compared to 12 is not equal (it's comparing them as integers). I'd say do a .toString() and compare the strings. That's just my wild guess :) Or just try to use three equals (===) to make a strict compare. Commented Dec 13, 2012 at 12:22
  • And is the resulting date stored on dteDate the correct one? Or just a valid date? Commented Dec 13, 2012 at 12:23
  • -1 is right in this case, for the months, JavaScript starts at 0 Commented Dec 13, 2012 at 12:24
  • @jValdron thanks buddy i was just asking! Commented Dec 13, 2012 at 12:25
  • The weird thing is 12/10/201. also gets passed, and if you do similar with the month. @Deleteman The resulting date is whatever it accepts, so it'll accept 12./10/201 for example. Commented Dec 13, 2012 at 12:26

5 Answers 5

2

You can use a function to check if the date is valid. Take a look at this code:

function isDate(dateStr) {
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
 var matchArray = dateStr.match(datePat); // is the format ok?
 if (matchArray == null) {
 alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
 return false;
 }
 month = matchArray[1]; // p@rse date into variables
 day = matchArray[3];
 year = matchArray[5];
 if (month < 1 || month > 12) { // check month range
 alert("Month must be between 1 and 12.");
 return false;
 }
 if (day < 1 || day > 31) {
 alert("Day must be between 1 and 31.");
 return false;
 }
 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
 alert("Month "+month+" doesn`t have 31 days!")
 return false;
 }
 if (month == 2) { // check for february 29th
 var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
 if (day > 29 || (day==29 && !isleap)) {
 alert("February " + year + " doesn`t have " + day + " days!");
 return false;
 }
 }
 return true; // date is valid
}
answered Dec 13, 2012 at 12:24
Sign up to request clarification or add additional context in comments.

1 Comment

The answer is good. However, it's much better to help others with their current solutions so they can learn. Anyone can Google for a function to validate dates :)
1

As discussed in the comments, here is the solution:

isValidDate = function(day,month,year) {
 var dteDate;
 dteDate=new Date(year,month,day);
 return ((day.toString()===dteDate.getDate().toString()) && (month.toString()===dteDate.getMonth().toString()) && (year.toString()===dteDate.getFullYear().toString()));
}

You also need the toString() methods, as if you compare "12" and 12, it won't be equal. Now you'll compare "12" with "12" which will be equal for valid dates.

answered Dec 13, 2012 at 12:33

Comments

0

This is because "12." is interpreted as 12, when parsing the string to a number (This happens internally, in the date object).
Try this:

if(isValidDate(a[0],a[1]-1,a[2]) == false && iDate.indexOf('.') == -1){

Basically, just check for the dot.

answered Dec 13, 2012 at 12:25

Comments

0
  1. is valid number. After it is passed to the Date constructor it translated to Number as 12.0 and it obviously equals to 12. 1.2 is not. I'd recommend you to match the regexp first and then check the correctness of the parts as you did.

I would suggest you to implement something like this:

function isValidDate(dateString)
{
 if(! dateString.match(/((1[0-2])|(0?\d))\/(([0-2]?\d)|(3[0-1]))/(\d\d)?\d\d/) )
 return false;
 var parts = dateString.split("/");
 var dateToCheck = new Date(parts[0],parts[1]-1,parts[2]);
 return ((parts[0]==dateToCheck.getDate()) && (parts[1]==dateToCheck.getMonth()+1) && (parts[2]==dateToCheck.getFullYear()));
}
answered Dec 13, 2012 at 12:35

1 Comment

Thank you. I'm glad to be here
0

This is my version of date validation. It will not allow to enter wrong date even some one intend to type. This will also auto populate / after month and day.

$("#date").keyup(function (e) {
 // validation for length max 10
 var temp = $(this).val();
 if (temp.length > 10) $(this).val(temp.substring(0, temp.length - 1));
 var key = String.fromCharCode(e.keyCode);
 console.log(key + " " + e.keyCode);
 var regex = /[0-9]|[a-i]|\/|\`|\o|\¿|\%|\'|\$/; 
 // checking teh allowed character
 if (!regex.test(key)) {
 // checking backspace and shift key ( need to allow)
 if (e.keyCode != 8 && e.keyCode != 16) {
 var tam = $(this).val().length;
 $(this).val(temp.substring(0, tam - 1));
 }
 }
 else {
 //if its not backslash
 if (e.keyCode != 191 && e.keyCode != 111) {
 if ($(this).val().length == 2) {
 $(this).val($(this).val() + "/");
 } else if ($(this).val().length == 5) {
 $(this).val($(this).val() + "/");
 }
 }
 else {
 // if backslash
 if ($(this).val().length != 3 && $(this).val().length != 6) {
 var tam = $(this).val().length;
 $(this).val(temp.substring(0, tam - 1));
 }
 }
 }
 });
answered Jan 29, 2015 at 20:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.