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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Client side date validation before submit runs 5

Status
Not open for further replies.

tyant

Programmer
Jun 1, 2001
68
AU
I can figure out how to validate text boxes before the form is submitted to my database.

I need to know how to validate a date before it is submitted to my database because there will be a "type mismatch" as the field in the database is a date field.
 
Use the isDate function.

i.e.
if not isDate(form.field.value) then
display error message
end if Mise Le Meas,

Mighty :)
 
Hi,

I'm using isDate successfully in VB Script (on Client-Side error checking on .asp), except when the user types in something like 9/31/01 (30 days has September), in which case I get a string error just for calling isDate, or any of the other date functions.

Does anyone know of a good way to avoid/trap this error and prevent this invalid date entry?

Thanks,
Ray
 
This should do the trick. I've included javascript and vbscript.
Jon
Javascript:
function validateDate(fieldVal)
{
fieldVal = trim(fieldVal);
// Begin
// Checks for the following valid date formats:
// mm/dd/yyyy mm-dd-yyyy
// Also separates date into month, day, and year variables
var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;

/*
^^^^ Information about this string ^^^^
Ignore first and last '/' it is code for RegExp
Anything between () will be matched and remembered for later use

^ matches first input
$ matches last input
\ means that the next char after the '\' has a special meaning
\2 means same thing as second operation in this case its : (\/)
d means digit, it matches a number from 0 to 9
{n,m] = matches at least N and at most M occurences. N & M are assumed to be positive
*/

var matchArray = fieldVal.match(datePat); // is the format ok?
if (matchArray == null)
{
errMsg ='Date is not in a valid format.\nUse the mm/dd/yyyy format'
isError=true
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
//alert(year);
//alert(year.length);
if (year.length == 4)
{
if (year < 1753)//dates less than January 1 1753 cause an exception in sql server
{
errMsg = &quot;Year must be greater than 1752&quot;;
isError=true
return false;
}
}
if (year.length == 3)
{
errMsg = &quot;Year must two or four digits.&quot;;
isError=true
return false;
}
if (month < 1 || month > 12) // check month range
{
errMsg ='Month must be between 1 and 12.'
isError=true
return false;
}
if (day < 1 || day > 31)
{
errMsg ='Day must be between 1 and 31.'
isError=true
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
errMsg =&quot;Month &quot;+month+&quot; doesn't have 31 days!&quot;
isError=true
return false
}
if (month == 2) // check for february 29th
{
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
errMsg = &quot;February &quot; + year + &quot; doesn't have &quot; + day + &quot; days!&quot;;
isError=true
return false;
}

}
return true; // date is valid
}

vbscript:

<%
Function RegExpValidator(patrn, strng)
Dim regEx, Match, Matches, RetStr
Set regEx = new RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True' Set case insensitivity.
regEx.Global = false' Set global applicability.
Set Matches = regEx.Execute(strng)' Execute search.
if Matches.count > 0 then
RetStr = true
else
RetStr = false
end if
set Matches = nothing
set regEx = nothing
RegExpValidator = RetStr
End Function



function ValidDate(dtDate, sErrorText)
dim sPatrn, sMonth, sDay, sYear, aDate, bValid, bIsLeap, bPatrnMatch
dtDate = trim(dtDate)
sPatrn = &quot;(\d{1,2})(\/)(\d{1,2})\2(\d{4})&quot;
bPatrnMatch = RegExpValidator(sPatrn,dtDate) '// is the format ok?
bValid = true
if bPatrnMatch = true then
aDate = split(dtDate,&quot;/&quot;)
if ubound(aDate) = 2 then
sMonth = aDate(0)
sDay = aDate(1)
sYear = aDate(2)

if len(sYear) = 4 then
if sYear < 1753 then ' //dates less than January 1 1753 cause an exception in sql server
bValid = false
sErrorText = &quot;Year must be greater than 1752&quot;
else
if sMonth < 1 or sMonth > 12 then '// check month range
bValid = false
sErrorText = &quot;Month must be between 1 and 12.&quot;
else
if sDay < 1 or sDay > 31 then
bValid = false
sErrorText = &quot;Day must be between 1 and 31.&quot;
else
if ((sMonth=4 or sMonth=6 or sMonth=9 or sMonth=11) and sDay=31) then
bValid = false
sErrorText =&quot;Month &quot; & sMonth & &quot; does not have 31 days!&quot;
else
if sMonth = 2 then '// check for february 29th
If (sYear Mod 4 = 0 And sYear Mod 100 <> 0) Or sYear Mod 400 = 0 Then
bIsLeap = True
Else
bIsLeap = False
End If
'Response.Write bIsLeap
'Response.Write sDay

if sDay > 29 or (sDay = 29 and not bIsLeap) then
bValid = false
sErrorText = &quot;February &quot; & sYear & &quot; does not have &quot; & sDay & &quot; days!&quot;
end if
end if
end if
end if
end if
end if
else
bValid = false
sErrorText = &quot;Year must be 4 digits&quot;
end if
else
bValid = false
sErrorText = &quot;Use the mm/dd/yyyy format&quot;
end if
else
bValid = false
sErrorText = &quot;Use the mm/dd/yyyy format&quot;
end if

ValidDate = bValid
end function
'response.write ValidDate(&quot;5/15/9999&quot;, sErrorText) & &quot;<br>&quot;
'Response.Write sErrorText
%>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top