www.webdeveloper.com
Results 1 to 3 of 3

Thread: Odd Date Validation Issue - not accepting 10/31/2013 as valid

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    278

    Odd Date Validation Issue - not accepting 10/31/2013 as valid

    Here is the relevant code:

    Code:
    	value = value.replace(/'\'/, "/");
    	var arrayDateParts = value.split("/");
    	var intMonth = arrayDateParts[0];
    	var intDay = arrayDateParts[1];
    	var intYear = arrayDateParts[2];
    	var arrayDaysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
    	
    	if ( (!(intYear % 4) && intYear % 100) || !(intYear % 400))
    	{
    		arrayDaysInMonth[1] = 29;
    	}
    
        var bitValid = new Date(value);
    			   if (intDay > arrayDaysInMonth[--intMonth] || isNaN(bitValid) == true || value.trim() == "") {
    			      	        strError = "The field " + name + " must be a valid date";
    				        document.getElementById("hid" + FieldName).value = 1;
    				        document.getElementById(ID).style.backgroundColor = '#FF5353';
    				     }
    when inputting 9/31/2013, it flags as invalid, as it should. When putting in 1/31/2013, it flags as valid, which it should. I put in 10/31/2013, and it says invalid.

    I change the intDay>arrayDaysInMonth[--intMonth] to parseInt() for both, still flags as invalid. intDay = 31 as does arrayDaysInMonth[--intMonth] when doing an alert to the screen.

    I'm at a loss. Any ideas? All other dates work as expected that I can tell

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    Your first line is probably useless. There is no need of quotes in the regular expression. Backslash is to escape and a g is essential to replace many characters... Then something like this would probably be better :
    Code:
    value = value.replace(/\\/g,"/");
    Use the Date object !
    Code:
    var arrDte = value.split("/");
    var year=arrDte[2],month=parseInt(arrDte[1],10),day=parseInt(arrDte[0],10);
    
    var dte=new Date(year,month-1,day);
    if (dte.getMonth()!=month-1) {// not valid date !
       alert('Sorry, this date seems not valid !');
       return}
    With not valid date (like 02/29/2013 or 09/31/2013) the month will change (03/1/2013 or 10/1/2013) !
    Last edited by 007Julien; 10-01-2013 at 06:49 AM.

  3. #3
    Join Date
    Aug 2005
    Posts
    278
    Quote Originally Posted by 007Julien View Post
    Your first line is probably useless. There is no need of quotes in the regular expression. Backslash is to escape and a g is essential to replace many characters... Then something like this would probably be better :
    Code:
    value = value.replace(/\\/g,"/");
    Use the Date object !
    Code:
    var arrDte = value.split("/");
    var year=arrDte[2],month=parseInt(arrDte[1],10),day=parseInt(arrDte[0],10);
    
    var dte=new Date(year,month-1,day);
    if (dte.getMonth()!=month-1) {// not valid date !
       alert('Sorry, this date seems not valid !');
       return}
    With not valid date (like 02/29/2013 or 09/31/2013) the month will change (03/1/2013 or 10/1/2013) !
    This worked great, thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles