INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...I signed up to your site to get help with a problem and I am so glad I did. I found the help I needed immediately. Thanks to all who contribute to your site..."
Geography
Where in the world do Tek-Tips members come from?
|
Client side date validation before submit runs
|
|
|
tyant (Visitor) |
1 Jun 01 3:33 |
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. |
|
Mighty (Programmer) |
1 Jun 01 4:20 |
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 = "Year must be greater than 1752"; isError=true return false; } } if (year.length == 3) { errMsg = "Year must two or four digits."; 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 ="Month "+month+" doesn't have 31 days!" 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 = "February " + year + " doesn't have " + day + " days!"; 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 = "(\d{1,2})(\/)(\d{1,2})\2(\d{4})" bPatrnMatch = RegExpValidator(sPatrn,dtDate) '// is the format ok? bValid = true if bPatrnMatch = true then aDate = split(dtDate,"/") 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 = "Year must be greater than 1752" else if sMonth < 1 or sMonth > 12 then '// check month range bValid = false sErrorText = "Month must be between 1 and 12." else if sDay < 1 or sDay > 31 then bValid = false sErrorText = "Day must be between 1 and 31." else if ((sMonth=4 or sMonth=6 or sMonth=9 or sMonth=11) and sDay=31) then bValid = false sErrorText ="Month " & sMonth & " does not have 31 days!" 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 = "February " & sYear & " does not have " & sDay & " days!" end if end if end if end if end if end if else bValid = false sErrorText = "Year must be 4 digits" end if else bValid = false sErrorText = "Use the mm/dd/yyyy format" end if else bValid = false sErrorText = "Use the mm/dd/yyyy format" end if ValidDate = bValid end function 'response.write ValidDate("5/15/9999", sErrorText) & "<br>" 'Response.Write sErrorText %>
|
|
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close