Date validation 
Author Message
 Date validation

Can somebody share a script to validate on the client that a user entered
the date using 4-digit year?  Even when explicitly asked, my users keep
entering the dates like 01/03/00 and ODBC puts year 1900 into VFP database.

Any help, code snippet, pointers to sites greatly appreciated...

Grace



Sun, 02 Jun 2002 03:00:00 GMT  
 Date validation

Quote:

>Can somebody share a script to validate on the client that a user entered
>the date using 4-digit year?  Even when explicitly asked, my users keep
>entering the dates like 01/03/00 and ODBC puts year 1900 into VFP database.

>Any help, code snippet, pointers to sites greatly appreciated...

Microsoft's implementation of JavaScript has a Regular Expression
object that can be used for this sort of thing. Something on the order
of:

var     re = new RegExp("^\d\d/\d\d/\d\d\d\d$";

result = re.exec(DateString);

If result == null, then the match failed, and DateString is not in the
form dd/dd/dddd.

--
Tim Slattery



Sun, 02 Jun 2002 03:00:00 GMT  
 Date validation
here's a date validation script i wrote that i find pretty useful.  just
pass the function CheckDate() the form element you want validated.
typically i use it thusly

this will totally validate the text field, and once it makes sure that a
valid date has been entered, will convert any 2 year dates to 4 year.
<input type=text
onBlur="javascript:CheckDate(document.form.formname.elementname);">

function CheckDate(frm_element)
{
 value = frm_element.value;
 var status = "month";
 var c = "";
 var day = "";
 var month = "";
 var year = "";
 var day_limit = 0;
 var err_format = "Invalid Date:  Please enter a valid date in the format
MM/DD/YYYY.";
 var err_date = "Invalid Date:  Please constrain your dates to within
01/01/1900 and 12/31/2200.";
 var err_month = "Invalid Date:  Please make sure that the month you have
entered actually exists.";
 var err_day = "Invalid Date:  Please make sure that the day you have
entered exists in the month that you have entered.";

 if ( value.length == 0 )
  return;
 for ( i = 0; i < value.length ; i++ )
  {
   c = value.charAt(i)
   if ( c == "-" || c == "/" )
    {
     switch ( status )
      {
       case "month":
        status = "day";
        break;
       case "day":
        status = "year";
     }
    }
   else
    {
     switch ( status )
      {
       case "month":
        month += c;
        break;
       case "day":
        day += c;
        break;
       case "year":
        year += c;
      }
    }
  }

  if ( year.length == 0 || year.length > 4 || isNaN(year) == true )
   {
    FieldError(frm_element,alert(err_format));
    return;
   }

  switch ( year.length )
   {
    case 1:
     FieldError(frm_element,alert(err_format));
     return;
    case 2:
     if ( parseInt(year) > 80 )
      year = "19" + year;
     else
      year = "20" + year;
     break;
    case 3:
     alert(err_format);
     frm_element.focus();
     frm_element.select();
     return;
     break;
    case 4:
     if ( parseInt(year) < 1900 || parseInt(year) > 2200 )
      {
       alert(err_date);
       frm_element.focus();
       frm_element.select();
       return;
      }
   }

  switch ( parseInt(month) )
   {

    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
     day_limit = 31;
     break;
    case 4:
    case 6:
    case 9:
    case 11:
     day_limit = 30;
     break;
    case 2:
     day_limit = febDayLimit( parseInt(year) );
     break;
    default:
     alert(err_month);
     frm_element.focus();
     frm_element.select();
     return;
     break;
   }
  if ( !(parseInt(day) > 0 && parseInt(day) <= day_limit) )
   {
    alert(err_day);
    frm_element.focus();
    frm_element.select();
    return;
   }

  if ( day.length == 1 )
   day = "0" + day;
  if ( month.length == 1 )
   month = "0" + month;

  frm_element.value = month + "/" + day + "/" + year;

Quote:
}

function febDayLimit (year)
{
 if ( ((year - 1900) % 4) ==  0 )
  return 29;
 return 28;
Quote:
}

function FieldError(frm_element,msg)
{
 alert(msg);
 frm_element.focus();
 frm_element.select();

Quote:
}



Quote:
> Can somebody share a script to validate on the client that a user entered
> the date using 4-digit year?  Even when explicitly asked, my users keep
> entering the dates like 01/03/00 and ODBC puts year 1900 into VFP
database.

> Any help, code snippet, pointers to sites greatly appreciated...

> Grace



Mon, 10 Jun 2002 03:00:00 GMT  
 Date validation
Your leap year test breaks for 2100 which isn't a leap year.

(Every 4th except every 100th except every 400th)

2000 is because it's divisible by 400.

--
Dale Strickland-Clark
Out-Think Ltd, UK
Business Technology Experts



Quote:
> here's a date validation script i wrote that i find pretty useful.  just
> pass the function CheckDate() the form element you want validated.
> typically i use it thusly

> this will totally validate the text field, and once it makes sure that a
> valid date has been entered, will convert any 2 year dates to 4 year.
> <input type=text
> onBlur="javascript:CheckDate(document.form.formname.elementname);">

> function CheckDate(frm_element)
> {
>  value = frm_element.value;
>  var status = "month";
>  var c = "";
>  var day = "";
>  var month = "";
>  var year = "";
>  var day_limit = 0;
>  var err_format = "Invalid Date:  Please enter a valid date in the format
> MM/DD/YYYY.";
>  var err_date = "Invalid Date:  Please constrain your dates to within
> 01/01/1900 and 12/31/2200.";
>  var err_month = "Invalid Date:  Please make sure that the month you have
> entered actually exists.";
>  var err_day = "Invalid Date:  Please make sure that the day you have
> entered exists in the month that you have entered.";

>  if ( value.length == 0 )
>   return;
>  for ( i = 0; i < value.length ; i++ )
>   {
>    c = value.charAt(i)
>    if ( c == "-" || c == "/" )
>     {
>      switch ( status )
>       {
>        case "month":
>         status = "day";
>         break;
>        case "day":
>         status = "year";
>      }
>     }
>    else
>     {
>      switch ( status )
>       {
>        case "month":
>         month += c;
>         break;
>        case "day":
>         day += c;
>         break;
>        case "year":
>         year += c;
>       }
>     }
>   }

>   if ( year.length == 0 || year.length > 4 || isNaN(year) == true )
>    {
>     FieldError(frm_element,alert(err_format));
>     return;
>    }

>   switch ( year.length )
>    {
>     case 1:
>      FieldError(frm_element,alert(err_format));
>      return;
>     case 2:
>      if ( parseInt(year) > 80 )
>       year = "19" + year;
>      else
>       year = "20" + year;
>      break;
>     case 3:
>      alert(err_format);
>      frm_element.focus();
>      frm_element.select();
>      return;
>      break;
>     case 4:
>      if ( parseInt(year) < 1900 || parseInt(year) > 2200 )
>       {
>        alert(err_date);
>        frm_element.focus();
>        frm_element.select();
>        return;
>       }
>    }

>   switch ( parseInt(month) )
>    {

>     case 1:
>     case 3:
>     case 5:
>     case 7:
>     case 8:
>     case 10:
>     case 12:
>      day_limit = 31;
>      break;
>     case 4:
>     case 6:
>     case 9:
>     case 11:
>      day_limit = 30;
>      break;
>     case 2:
>      day_limit = febDayLimit( parseInt(year) );
>      break;
>     default:
>      alert(err_month);
>      frm_element.focus();
>      frm_element.select();
>      return;
>      break;
>    }
>   if ( !(parseInt(day) > 0 && parseInt(day) <= day_limit) )
>    {
>     alert(err_day);
>     frm_element.focus();
>     frm_element.select();
>     return;
>    }

>   if ( day.length == 1 )
>    day = "0" + day;
>   if ( month.length == 1 )
>    month = "0" + month;

>   frm_element.value = month + "/" + day + "/" + year;
> }

> function febDayLimit (year)
> {
>  if ( ((year - 1900) % 4) ==  0 )
>   return 29;
>  return 28;
> }
> function FieldError(frm_element,msg)
> {
>  alert(msg);
>  frm_element.focus();
>  frm_element.select();
> }



> > Can somebody share a script to validate on the client that a user
entered
> > the date using 4-digit year?  Even when explicitly asked, my users keep
> > entering the dates like 01/03/00 and ODBC puts year 1900 into VFP
> database.

> > Any help, code snippet, pointers to sites greatly appreciated...

> > Grace



Fri, 14 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Date validation script needed - Please help!!!

2. Date Validation

3. Date Validation

4. Date validation

5. Date validation

6. Short Date Validation

7. Date Validation in JScript

8. Javascript 1.0 Date Validation Function

9. Date validation

10. Client side date validation

11. Date Validation

12. Date Validation

 

 
Powered by phpBB® Forum Software