date validation!!

Results 1 to 3 of 3

Thread: date validation!!

  1. #1
    Join Date
    Dec 1969

    Default date validation!!

    I have page with start date and end date. I want to validate;<BR>1) both dates are entered<BR>2) start date is less than end date<BR>3) both dates are in mm/dd/yyyy format.<BR>before form is submitted<BR>I am using javascript to check all three, but its not working. if I enter startdate and leave enddate empty, I get to next page which uses these date. Hence I get error.<BR>Thanks in advance.<BR>function isDate(DateField) <BR>{<BR> var value = DateField.value<BR> var label =;<BR> // convert hyphen delimiters to slashes<BR> var delim1 = value.indexOf("/")<BR> var delim2 = value.lastIndexOf("/")<BR> if ((delim1 != -1 && delim1 == delim2) &#124&#124 (delim1 == -1 && delim2 == -1) ) {<BR> alert( getName(label) + " is not in an acceptable date format.

    You can enter dates in the following format: mm/dd/yyyy")<BR> DateField.focus()<BR><BR> return false<BR> }<BR> if (delim1 != -1) {<BR> // there are delimiters; extract component values<BR> var mm = parseInt(value.substring(0,delim1),10)<BR> var dd = parseInt(value.substring(delim1 + 1,delim2),10)<BR> var yyyy = parseInt(value.substring(delim2 + 1, value.length),10)<BR> } <BR>function DateCompare(date1, date2)<BR>{<BR> var vDate1 = new Date(Date.parse(date1));<BR> var vDate2 = new Date(Date.parse(date2));<BR> if ( vDate1 &#062; vDate2 ) { <BR> alert(" Start Date must be less than End Date, Please enter again."); <BR> return false;<BR> }<BR> else if ( vDate1 &#060; vDate2 )<BR> return true;<BR> else if ( vDate1 == vDate2 ) {<BR> alert(" Start Date must be less than End Date, Please enter again."); <BR> false;<BR> }<BR>}<BR><BR>In my asp page, I have &#060;form method="POST" action="&#060;%=rptaction%&#062;" id=form1 name=form1 onsubmit="return isDate(document.forms[0].StartDate);return isDate(document.forms[0].EndDate);return DateCompare(StartDate,EndDate)" &#062;<BR><BR>

  2. #2
    Fr0st Guest

    Default RE: date validation!!

    This might not be the answer you are looking for, but it would make for easy validation. If you make your page so a person would enter in the date as ddmmyyyy (either using three separate input boxes with a text "/" in between input box, or just using one input box with no delimiters) then you could simply subtract the values when you wanted to make sure they were valid and use the CDate() function to format properly on the following page. This also makes for easy database storage as you can simply store the date value in a text datatype field.<BR><BR>Good luck.

  3. #3
    Join Date
    Dec 1969

    Default RE: date validation!!

    Hi,<BR><BR>I have not used Javascript but this can be done very easily in VBScript. <BR><BR>1)To check if both dates are entered...use the Len() function<BR>If(Len(startDate)&#062; 0 AND Len(endDate) &#062; 0) then...<BR>The LEN function checks the length of the variable. If they are both greater than 0, it means the user has entered a value for both fields.<BR><BR>2)To check if the dates are in mm/dd/yyyy format use the IsDate() function.<BR>If(IsDate(startDate) AND IsDate(endDate)then...<BR>IsDate() checks if the variable is a valid date.<BR><BR>3)To check if the startDate is less than endDate use the DateDiff function. <BR>If(DateDiff("d", startDate, endDate) &#060; 0) then...<BR>If this condition is met, startDate is less than endDate.<BR><BR>### Even if you are a JavaScript can use VBScript in the same file for date validation...Hope this helps...<BR>

Posting Permissions

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