Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate date- check for non "/"

Status
Not open for further replies.

cat5ive

MIS
Joined
Dec 3, 2004
Messages
184
Location
US
Hi,

I'm trying to validate the date. First I want to strip out non digit charater. If the non digit char. is not "/", then I want to pop the alert box. It seem that the statement that I compare with "/" is not working. When I enter date as 05/01/06 (with slash), the alert box pop up.
thanks in advance
Code:
for (i = 0; i < DateValue.length; i++)
		   {
	         if (checkstr.indexOf(DateValue.substr(i,1)) >= 0)
			  {
	             DateTemp = DateTemp + DateValue.substr(i,1);
	          }
[COLOR=red]			 else if (checkstr.indexOf(DateValue.substr(i,1)) != "/") [/color] 
			  {		  
				 alert("Date is incorrect. Use mm/dd/yy format.");           
	             DateField.focus();
			     return false;
		      } 
            }
 
Here's a function I use to check to see if something is a date or not:
Code:
function isDate(onestring)
{

var checkstring = onestring;

if (checkstring.indexOf('/')==-1) {return false;}

var month=parseInt(checkstring.substr(0, checkstring.indexOf('/')), 10);
checkstring = checkstring.substr(checkstring.indexOf('/') + 1);

if (checkstring.indexOf('/')==-1) {return false;}

var day=parseInt(checkstring.substr(0, checkstring.indexOf('/')), 10);
var year=parseInt(checkstring.substr(checkstring.indexOf('/') + 1), 10);

if (isNaN(month)) {return false;}
if (isNaN(day)) {return false;}
if (isNaN(year)) {return false;}

if (day > 31 || day < 1) {return false;}
if (month > 12 || month < 1) {return false;}

switch (month)
  {
  case  1:
  case  3:
  case  5:
  case  7:
  case  8:
  case 10:
  case 12: break;
  case 4:
  case 6:
  case 9:
  case 11:
    {
    if (day > 30) {return false;}
    break;
    }
  case 2:
    {
    if (day > 29) {return false;}
    if ((year % 4) != 0)
      {
      if (day > 28) {return false;}
      }
    if ((year % 100) == 0)
      {
      if ((year % 400) != 0)
        {
        if (day > 28) {return false;}
        }
      }
    }
  }

return true;
}

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top