dateTime.tryParse validation problem

Joined
Nov 8, 2006
Messages
5
Reaction score
0
I'm trying to validate a date control on a form. It's required, then I've checked that it's digits in the right mm/dd/yyyy format. Now I'm trying to see if it's a valid date and greater than today. Thought I had it, but it's allowing the valid to pass through and process the btnSubmit_click event? How confused I am. After reading: "http://msdn2.microsoft.com/en-us/library/yb52a4x0.aspx Validation is performed on the server even if it was already performed on the client. This enables you to determine validation status in server code and provides security against users bypassing client-side validation.", I thought I had it. But the reality seems to be saying that the validation in asp.net is client side unless you write additional checking code in the page load event (or elsewhere) for page.isValid?

I included the javascript below that I found free (and am glad to give credit) and added (controlToValidate="rstart" ClientValidationFunction="isJDate" OnServerValidate="dateValidate" SetFocusOnError="true" ErrorMessage="Must be a valid date") to the custom validator.

Now, when I enter 2/31/2007 I get the correct error above, and when I change it to 1/31/2007 I still get the error above. If I change the Javascript to something that always validates true, the server validation never fires. (I've tried two different javascript date validators - both the same thing) I put a "if page.isValid then" statement around the btnClick handler, but it doesn't do anything else at this point. If I have to put the server validation code in the btnClick event, how do I do so with the page.isValid is read only - and what's?

Anybody willing to give me some specific ideas on the best way to do this? I'm obviously wallering around in the mud here.... Thanks.

Sub dateValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Dim dt As DateTime, myControl As Control = source
If (DateTime.TryParse(args.Value, dt) = False) Or (dt <= DateTime.Today) Then
args.IsValid = False
myControl.Focus()
Else
args.IsValid = True
End If
End Sub
<asp:TextBox ID="rend" columns="10" runat="server" />
<asp:RequiredFieldValidator ID="valRend" ControlToValidate="rend" runat="server" ErrorMessage="Required." SetFocusOnError="true" />
<asp:RegularExpressionValidator ID="rvalRend" ControlToValidate="rend" runat="server" ErrorMessage=" Not valid." ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" SetFocusOnError="true" Display="Dynamic" />
<asp:CustomValidator id="cvalrend" runat="server" controlToValidate="rend" OnServerValidate="dateValidate" SetFocusOnError="true" ErrorMessage="Must be a valid date" Display="Dynamic" />

<asp:button runat="server" ID="btnSubmit" Text="submit" onClick="btnSubmit_click" />

<script type="text/javascript" language="JavaScript">
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original: Torsten Frey ([email protected]) -->
<!-- Begin
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(0,2);
if ((month < 1) || (month > 12)) {
err = 21;
}
/* Validation of day*/
day = DateValue.substr(2,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("Date is incorrect!");
DateField.select();
DateField.focus();
}
}
// End -->
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top