CompareValidator Only Works On Manual Input

J

John Walker

Hi,
There is a textbox in my datagrid where the user can enter a date manually
or they can use a popup calendar which will automatically fill in the textbox
when they click on a date. This textbox has a compare validator on it, which
is shown below.

When the user types in a date, the validators fire correctly, but when the
pop-up calendar is used, the validators are not fired. They should be fired
when the user clicks on the submit button, which executes "ConfirmUpdate"
which I included at the bottom.

Do you have any idea why the validators work one way but not the other?

Thanks!
John

Here's my validator:

<asp:CompareValidator
ControlToValidate="txtTrkPUDate"
display =dynamic
Text="Date is too far past!"
Operator=GreaterThanEqual
Type="Date"
Runat="Server" ID="cvPickupXDaysPast"/>


Here's where I set the ValueToCompare in ItemDataBound:

cv = CType(e.Item.FindControl("cvPickupXDaysPast"), CompareValidator)
cv.ValueToCompare = DateTime.Today.AddDays(-7).ToShortDateString()


Here's a portion of the popup calendar window that fills the textbox with a
date:

window.opener.document.all['" &
Httpcontext.Current.Request.Querystring("controlname") & "'].value =
'1/1/2000';


Here's the javascript on the submit button which is supposed to check the
validators:

function ConfirmUpdate() {
if (Boolean(Page_IsValid) == 0)
{
alert('Invalid value.');
return false;
}
return true;
}
 
J

John Walker

Peter,

You are probably right in that the Attribute.Add I was doing was replacing
Microsoft's javascript. Here are two different buttons I tested with. I got
the "Input" one to validate properly, so I'm going to use that one. I was
originally using "ASP:Button" which was giving me the problems:

This one causes validation:
<input type="submit" onclick="return ConfirmUpdate2();" id="cmdConfirm"
value="Submit" runat="server" NAME="cmdConfirm" class="input-button" >

This one doesn't:
<asp:button id="btnUpdate" CausesValidation="True" text="Save Changes"
CssClass="input-button" Runat="server"></asp:button>

Thanks!
John

Peter Blum said:
Hi John,

When you added your javascript to the button, you are not likely to get what
you wanted because the button has its own onclick javascript that calls
Page_ClientValidate to set Page_IsValid. Your code probably is replacing the
Microsoft validation code so that Page_IsValid is not set.
My best guess is that you set the submit button's CausesValidation to false
to avoid generating the Microsoft validation code on the onclick event. Your
goal is to blend the two technologies together.

Now let me recommend a couple of changes.
1. Instead of using your code, use the ValidationSummary control. It can
display the alert box with the actual error message when the submit button
is clicked and works within the existing validation system so that you don't
have to write custom code. Just set its ShowMessageBox property to true. (If
you don't want the ValidationSummary control to appear on the page, set
ShowSummary to false.)

2. There are many third party DateTextBoxes with popup calendars available
that do not use a separate window for the popup. This has some useful
benefits:
- It cannot be blocked by a popup blocker
- It positions right next to the toggle button
- Its size is correct regardless of the browser (which has different sizes
for fonts and tables)
In order to do this, the third parties have replaced the ASP.NET calendar
control with their own, that uses javascript.
I am the author of one of them, Peter's Date Package
(http://www.peterblum.com/datecontrols/home.aspx). It has date, calendar,
time and validator controls for $50 (US) per production server. It filters
out illegal characters and has fuzzy logic data entry to handle slightly
imperfect date entries. You can find others at the www.asp.net Control
Gallery, www.123aspx.com, and www.411asp.net.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

John Walker said:
Hi,
There is a textbox in my datagrid where the user can enter a date manually
or they can use a popup calendar which will automatically fill in the
textbox
when they click on a date. This textbox has a compare validator on it,
which
is shown below.

When the user types in a date, the validators fire correctly, but when the
pop-up calendar is used, the validators are not fired. They should be
fired
when the user clicks on the submit button, which executes "ConfirmUpdate"
which I included at the bottom.

Do you have any idea why the validators work one way but not the other?

Thanks!
John

Here's my validator:

<asp:CompareValidator
ControlToValidate="txtTrkPUDate"
display =dynamic
Text="Date is too far past!"
Operator=GreaterThanEqual
Type="Date"
Runat="Server" ID="cvPickupXDaysPast"/>


Here's where I set the ValueToCompare in ItemDataBound:

cv = CType(e.Item.FindControl("cvPickupXDaysPast"), CompareValidator)
cv.ValueToCompare = DateTime.Today.AddDays(-7).ToShortDateString()


Here's a portion of the popup calendar window that fills the textbox with
a
date:

window.opener.document.all['" &
Httpcontext.Current.Request.Querystring("controlname") & "'].value =
'1/1/2000';


Here's the javascript on the submit button which is supposed to check the
validators:

function ConfirmUpdate() {
if (Boolean(Page_IsValid) == 0)
{
alert('Invalid value.');
return false;
}
return true;
}
 
J

John Walker

Peter,

One more question: our customer wants for us to notify the user immediately
if he enters a bad date, and not just when he clicks on "submit" (because
there can be very many dates to enter on the datagrid). Like I mentioned
earlier, this works fine when the date is entered manually(i.e.,when the user
tabs to a different field), but not when it is selected from a calendar
pop-up window. Do you know if there's anyway for javascript or any other way
the get the CompareValidator to check the value "Now"?

Thanks again,
John

Peter Blum said:
Hi John,

When you added your javascript to the button, you are not likely to get what
you wanted because the button has its own onclick javascript that calls
Page_ClientValidate to set Page_IsValid. Your code probably is replacing the
Microsoft validation code so that Page_IsValid is not set.
My best guess is that you set the submit button's CausesValidation to false
to avoid generating the Microsoft validation code on the onclick event. Your
goal is to blend the two technologies together.

Now let me recommend a couple of changes.
1. Instead of using your code, use the ValidationSummary control. It can
display the alert box with the actual error message when the submit button
is clicked and works within the existing validation system so that you don't
have to write custom code. Just set its ShowMessageBox property to true. (If
you don't want the ValidationSummary control to appear on the page, set
ShowSummary to false.)

2. There are many third party DateTextBoxes with popup calendars available
that do not use a separate window for the popup. This has some useful
benefits:
- It cannot be blocked by a popup blocker
- It positions right next to the toggle button
- Its size is correct regardless of the browser (which has different sizes
for fonts and tables)
In order to do this, the third parties have replaced the ASP.NET calendar
control with their own, that uses javascript.
I am the author of one of them, Peter's Date Package
(http://www.peterblum.com/datecontrols/home.aspx). It has date, calendar,
time and validator controls for $50 (US) per production server. It filters
out illegal characters and has fuzzy logic data entry to handle slightly
imperfect date entries. You can find others at the www.asp.net Control
Gallery, www.123aspx.com, and www.411asp.net.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

John Walker said:
Hi,
There is a textbox in my datagrid where the user can enter a date manually
or they can use a popup calendar which will automatically fill in the
textbox
when they click on a date. This textbox has a compare validator on it,
which
is shown below.

When the user types in a date, the validators fire correctly, but when the
pop-up calendar is used, the validators are not fired. They should be
fired
when the user clicks on the submit button, which executes "ConfirmUpdate"
which I included at the bottom.

Do you have any idea why the validators work one way but not the other?

Thanks!
John

Here's my validator:

<asp:CompareValidator
ControlToValidate="txtTrkPUDate"
display =dynamic
Text="Date is too far past!"
Operator=GreaterThanEqual
Type="Date"
Runat="Server" ID="cvPickupXDaysPast"/>


Here's where I set the ValueToCompare in ItemDataBound:

cv = CType(e.Item.FindControl("cvPickupXDaysPast"), CompareValidator)
cv.ValueToCompare = DateTime.Today.AddDays(-7).ToShortDateString()


Here's a portion of the popup calendar window that fills the textbox with
a
date:

window.opener.document.all['" &
Httpcontext.Current.Request.Querystring("controlname") & "'].value =
'1/1/2000';


Here's the javascript on the submit button which is supposed to check the
validators:

function ConfirmUpdate() {
if (Boolean(Page_IsValid) == 0)
{
alert('Invalid value.');
return false;
}
return true;
}
 
J

John Walker

Peter,
I'll take a look at your calendar and suggest it to our customer - I think
that's the way we'd want date selections to work.
Also, thanks for the javascript info!
Thanks,
John

Peter Blum said:
One nice thing about javascript is that its exposed to you. For example, if
you open [domain root]\aspnet_client\system_web\[.net
version]\webuivalidation.js, you can see the functions written to handle
client-side validation. All you need to do is determine which one is correct
and how to call it.
In this case, its function ValidatorValidate(val) (its case sensitive!)
where val is the object representing the validator.

ValidatorValidate(document.all['clientid of the validator']);

My question is why would the calendar supply an illegal date? In my own
DateTextBox product http://www.peterblum.com/datecontrols/home.aspx), you
can define dates which are not selectable and a min/max date range. That way
there is never a validation error. I even include a validator that will
detect a date that is unselectable and report an error.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

John Walker said:
Peter,

One more question: our customer wants for us to notify the user
immediately
if he enters a bad date, and not just when he clicks on "submit" (because
there can be very many dates to enter on the datagrid). Like I mentioned
earlier, this works fine when the date is entered manually(i.e.,when the
user
tabs to a different field), but not when it is selected from a calendar
pop-up window. Do you know if there's anyway for javascript or any other
way
the get the CompareValidator to check the value "Now"?

Thanks again,
John

Peter Blum said:
Hi John,

When you added your javascript to the button, you are not likely to get
what
you wanted because the button has its own onclick javascript that calls
Page_ClientValidate to set Page_IsValid. Your code probably is replacing
the
Microsoft validation code so that Page_IsValid is not set.
My best guess is that you set the submit button's CausesValidation to
false
to avoid generating the Microsoft validation code on the onclick event.
Your
goal is to blend the two technologies together.

Now let me recommend a couple of changes.
1. Instead of using your code, use the ValidationSummary control. It can
display the alert box with the actual error message when the submit
button
is clicked and works within the existing validation system so that you
don't
have to write custom code. Just set its ShowMessageBox property to true.
(If
you don't want the ValidationSummary control to appear on the page, set
ShowSummary to false.)

2. There are many third party DateTextBoxes with popup calendars
available
that do not use a separate window for the popup. This has some useful
benefits:
- It cannot be blocked by a popup blocker
- It positions right next to the toggle button
- Its size is correct regardless of the browser (which has different
sizes
for fonts and tables)
In order to do this, the third parties have replaced the ASP.NET calendar
control with their own, that uses javascript.
I am the author of one of them, Peter's Date Package
(http://www.peterblum.com/datecontrols/home.aspx). It has date, calendar,
time and validator controls for $50 (US) per production server. It
filters
out illegal characters and has fuzzy logic data entry to handle slightly
imperfect date entries. You can find others at the www.asp.net Control
Gallery, www.123aspx.com, and www.411asp.net.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

Hi,
There is a textbox in my datagrid where the user can enter a date
manually
or they can use a popup calendar which will automatically fill in the
textbox
when they click on a date. This textbox has a compare validator on it,
which
is shown below.

When the user types in a date, the validators fire correctly, but when
the
pop-up calendar is used, the validators are not fired. They should be
fired
when the user clicks on the submit button, which executes
"ConfirmUpdate"
which I included at the bottom.

Do you have any idea why the validators work one way but not the other?

Thanks!
John

Here's my validator:

<asp:CompareValidator
ControlToValidate="txtTrkPUDate"
display =dynamic
Text="Date is too far past!"
Operator=GreaterThanEqual
Type="Date"
Runat="Server" ID="cvPickupXDaysPast"/>


Here's where I set the ValueToCompare in ItemDataBound:

cv = CType(e.Item.FindControl("cvPickupXDaysPast"), CompareValidator)
cv.ValueToCompare = DateTime.Today.AddDays(-7).ToShortDateString()


Here's a portion of the popup calendar window that fills the textbox
with
a
date:

window.opener.document.all['" &
Httpcontext.Current.Request.Querystring("controlname") & "'].value =
'1/1/2000';


Here's the javascript on the submit button which is supposed to check
the
validators:

function ConfirmUpdate() {
if (Boolean(Page_IsValid) == 0)
{
alert('Invalid value.');
return false;
}
return true;
}
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top