Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: validate the date


Message #1 by JonGrman21@a... on Thu, 30 Nov 2000 11:13:56 EST
no i mean that i want them to enter into a text box the date in the format of 

"MM/DD/YY"  how do i validate it so i make sure its in that  format

Message #2 by Gregory_Griffiths@c... on Thu, 30 Nov 2000 17:08:04 +0000
try some javascript on the client side to check the form in the first 

instance and then check it on the server side, to be sure use some 

combo boxes rather than allowing the user to enter data directly.



> -----Original Message-----

> From: JonGrman21@a... [mailto:JonGrman21@a...]

> Sent: 30 November 2000 16:14

> To: asp_databases@p...

> Cc: JonGrman21@a...

> Subject: [asp_databases] validate the date

> 

> 

> no i mean that i want them to enter into a text box the date 

> in the format of 

> "MM/DD/YY"  how do i validate it so i make sure its in that  format

> 



Message #3 by "Dallas Martin" <dmartin@z...> on Thu, 30 Nov 2000 14:47:35 -0500
Here's some JavaScript I wrote



<html>

<head>

<script language="JavaScript1.2">

<!--

// this code from MS, due to bug in

// implementation of numbers with leading 0's

function parseVal(val)

{

   while (val.charAt(0) == '0')

      val = val.substring(1, val.length);

   return val;

}



// my code

function isGoodDate(aDate)

{



if (aDate == ""){alert("Bad Date");return false};



var aDateREI = /(\d{2})([/])(\d{2})([/])(\d{4})/;

var baDate = aDateREI.test(aDate);



if (!baDate){ alert("Bad Date");return false };



var mm = parseInt(parseVal(aDate.substring(0,2)));

var dd = parseInt(parseVal(aDate.substring(3,5)));

var yyyy = parseInt(parseVal(aDate.substring(6,10)));

var today = new Date();

var fullyear = today.getYear();



var days = new Array();

days[0] = 0;

days[1] = 31;

days[2] = (yyyy % 4) == 0 ? 29 : 28;

days[3] = 31;

days[4] = 30;

days[5] = 31;

days[6] = 30;

days[7] = 31;

days[8] = 31;

days[9] = 30;

days[10] = 31;

days[11] = 30;

days[12] = 31;



var maxdays = days[mm];



if(baDate == true)

{

  if ((mm<1) || (mm>12)){return false;}

  if (dd>maxdays){ return false;}

  if (yyyy<1900) { return false;}

  if (yyyy > fullyear){ alert("Bad Year"); return false;}



  // passes above tests

  alert("Good Date");

  return true;

}

else

{

  alert("Bad Date");

  return false;

}

}

// -->

</script>

</head>



<body>

<form name="frm1">

Enter Date (mm/dd/yyyy): <input type="text" name="thisdate" size=10

maxlength=10>

<input type="button"value="Check Date"

onClick="isGoodDate(document.frm1.thisdate.value);">

</form>

</body>

</html>





hth

Dallas Martin





----- Original Message -----

From: <Gregory_Griffiths@c...>

To: "ASP Databases" <asp_databases@p...>

Sent: Thursday, November 30, 2000 12:08 PM

Subject: [asp_databases] RE: validate the date





> try some javascript on the client side to check the form in the first

> instance and then check it on the server side, to be sure use some

> combo boxes rather than allowing the user to enter data directly.

>

> > -----Original Message-----

> > From: JonGrman21@a... [mailto:JonGrman21@a...]

> > Sent: 30 November 2000 16:14

> > To: asp_databases@p...

> > Cc: JonGrman21@a...

> > Subject: [asp_databases] validate the date

> >

> >

> > no i mean that i want them to enter into a text box the date

> > in the format of

> > "MM/DD/YY"  how do i validate it so i make sure its in that  format

> >

>

>





Message #4 by dont worry <aspmailbox@y...> on Thu, 30 Nov 2000 11:57:12 -0800 (PST)
Best way is to have three separte boxes.

one for 

<input name="month" maxlength="2">

<input name="day" maxlength="2">

<input name="year" maxlength="2">

then use javascrpt to make sure that each is entered

correctly.

then use server side code to double check again and

put them together in a mm/dd/yyyy format.

best way

laters





--- JonGrman21@a... wrote:

> no i mean that i want them to enter into a text box

> the date in the format of 

> "MM/DD/YY"  how do i validate it so i make sure its

> in that  format

> 

Message #5 by "Pat Waddington" <paw@s...> on Thu, 30 Nov 2000 22:03:39 -0000
Hi,



This is some Javascript code that I use for validating dates entered into

text boxes where the format must be dd/mm/yy.



It's part of the OnSubmit function from one of my pages.



HTH



file://__________START OF DATE VALIDATION__________

file://_____________________________________________________________________

_________

var DaysArray = new Array(0, 31, 28, 31,30, 31, 30, 31, 31, 30, 31, 30, 31);

var strYear = valdate.slice(6,8);

var intYear = parseInt(strYear);

var tmpYear = intYear / 4;

var tmpYear = intYear * 4;

{if (tmpYear = intYear)

 DaysArray[2] = 29;}

tmpYear = intYear / 400;

tmpYear = intYear * 400;

{if (tmpYear = intYear)

 DaysArray[2] = 28;}

var strMonthAll = valdate.slice(3,5);

var strMonthLead = valdate.slice(3,4);

var strMonthEnd = valdate.slice(4,5);

strMonth = strMonthAll;

{

if (strMonthLead == "0")

 {strMonth = strMonthEnd;}

}

var intMonth = parseInt(strMonth);



var strDayAll = valdate.slice(0,2);

var strDayLead = valdate.slice(0,1);

var strDayEnd = valdate.slice(1,2);

strDay = strDayAll;

{

if (strDayLead == "0")

 {strDay = strDayEnd;}

}

var intDay = parseInt(strDay);



var DateOK = "Y";

{if (intMonth < 1 || intMonth > 12)

 DateOK = "N";

}

{if (intDay < 1 || intDay > DaysArray[intMonth])

 DateOK = "N";

}

file://_____________________________________________________________________

____________



file://___________END OF DATE VALIDATION___________

----- Original Message -----

From: <JonGrman21@a...>

To: "ASP Databases" <asp_databases@p...>

Sent: Thursday, November 30, 2000 4:13 PM

Subject: [asp_databases] validate the date





> no i mean that i want them to enter into a text box the date in the format

of

> "MM/DD/YY"  how do i validate it so i make sure its in that  format

>

Message #6 by "Michael Goldman" <mg188@h...> on Thu, 30 Nov 2000 14:50:42 -0800
Jon,  someone else may come up with a slicker way to do this.  But if not,

you could reuse the code below.



This form serves select boxes for each part of the date/time.  Look way down

this message for what goes on the next page.



<table><tr><td>start * (month/day/year hr:min am/pm)



<select name=mm size=1>

<option> </option>    ' this line gives a 1-digit value for "mm".  on

the next page, i test if the value is >1

<option>01</option>

<option>02</option>

<option>03</option>

<option>04</option>

<option>05</option>

<option>06</option>

<option>07</option>

<option>08</option>

<option>09</option>

<option>10</option>

<option>11</option>

<option>12</option>

</select></td>



<td> <select name=dd size=1>

<option> </option>

<option>01</option>

<option>02</option>

<option>03</option>

<option>04</option>

<option>05</option>

<option>06</option>

<option>07</option>

<option>08</option>

<option>09</option>

<option>10</option>

<option>11</option>

<option>12</option>

<option>13</option>

<option>14</option>

<option>15</option>

<option>16</option>

<option>17</option>

<option>18</option>

<option>19</option>

<option>20</option>

<option>21</option>

<option>22</option>

<option>23</option>

<option>24</option>

<option>25</option>

<option>26</option>

<option>27</option>

<option>28</option>

<option>29</option>

<option>30</option>

<option>31</option></select></td>



<td> <select name=yy size=1>

<option> </option>

<option>00</option>

<option>01</option>

<option>02</option>

<option>03</option>

<option>04</option>

</select></td>



<td> <select name=hh size=1>

<option> </option>

<option>01</option>

<option>02</option>

<option>03</option>

<option>04</option>

<option>05</option>

<option>06</option>

<option>07</option>

<option>08</option>

<option>09</option>

<option>10</option>

<option>11</option>

<option>12</option>

</select></td>



<td> <select name=nn size=1>

<option> </option>

<option>00</option>

<option>15</option>

<option>30</option>

<option>45</option>

</select></td>



<td> <select name=ampm size=1>

<option> </option>

<option>am</option>

<option>pm</option>

</select></td></tr></table>





' ##### And now for the next page stuff.  This create the string "strdat"

which you can insert into the db.  Note that one hole in this code is that

user can leave off ampm.  You may want to force that with a client-side

validation on the first page.



happy trails





if len(request.form("mm"))>1 then

strdat=cstr(request.form("mm"))

end if

if len(request.form("dd"))>1 then

strdat=strdat&"/"&cstr(request.form("dd"))

end if

if len(request.form("yy"))>1 then

strdat=strdat&"/"&cstr(request.form("yy"))

end if



if len(request.form("hh"))>1 then

strdat=strdat&" "&cstr(request.form("hh"))

end if



if len(request.form("nn"))>1 then

strdat=strdat&":"&cstr(request.form("nn"))

end if



if len(request.form("ampm"))>1 then

strdat=strdat&cstr(request.form("ampm"))

end if





----- Original Message -----

From: <JonGrman21@a...>

To: "ASP Databases" <asp_databases@p...>

Sent: Thursday, November 30, 2000 8:13 AM

Subject: [asp_databases] validate the date





> no i mean that i want them to enter into a text box the date in the format

of

> "MM/DD/YY"  how do i validate it so i make sure its in that  format

>




  Return to Index