p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   date validation (again) (http://p2p.wrox.com/showthread.php?t=25050)

crmpicco February 4th, 2005 06:40 AM

date validation (again)
 
How do i stop someone entering a duff value then just hitting the 'enter' key to submit the form?

there is javascript, to ensure you enter in the correct format. but only if you tab to the button or click it.

function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
      if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
         DateTemp = DateTemp + DateValue.substr(i,1);
      }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
         DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Incorrect Date: Please Enter the Date in Format 'DD/MM/YY'");
      DateField.select();
      DateField.focus();
   }
}

thats my existing javascript.

any ideas?

Picco


ChrisScott February 4th, 2005 06:43 AM

How are you calling the check_date() function?

Cheers,

Chris


crmpicco February 4th, 2005 06:45 AM

My textbox calls it:

<input type="text" size="10" name="date" value="" onblur="check_date(this)">

Should this work?



ChrisScott February 4th, 2005 06:58 AM

You can perform validation when the form is submitted using the forms onsubmit event...
Code:

<form name="myFormName" onsubmit="return check_date(this.date);">
You will also need to update your function to return true / false depending on whether or not the field passes validation to prevent the form submitting.

HTH,

Chris



crmpicco February 4th, 2005 07:02 AM

where would i add this true/false statement?

thanks


ChrisScott February 4th, 2005 07:09 AM

By the look of your function you are setting an error code & if this code is 0, everything is ok.
So at the end of your function add...
Code:

return err == 0 ? true : false;

crmpicco February 4th, 2005 07:12 AM

chris,


--------------------------------------------------------------------------------

http://www.donaldmackenzie.com/

Does anyone know of a font face (or name) like the text 'Welcome to Mackenzie Travel' at the top of this page pasted above?

Thanks.

Picco



ChrisScott February 4th, 2005 07:33 AM

Don't know.

This link may help you narrow it down: http://www.fonts.com/findfonts/ident...AME=identifont

Cheers,

Chris


crmpicco February 4th, 2005 07:56 AM

cheers. went through 20-odd questions.....

and you have to BUY the font!!



www.crmpicco.co.uk

joefawcett February 4th, 2005 08:27 AM

Quote:

quote:Originally posted by crmpicco
 cheers. went through 20-odd questions.....

and you have to BUY the font!!

www.crmpicco.co.uk
Amazing, someone wants paying for all the work they've done :)

--

Joe (Microsoft MVP - XML)


All times are GMT -4. The time now is 12:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.