CustomValidator

S

Stan SR

Hi,

I have a web user control that contains 3 dropdownlists
it's a date selector, so one contains the days, one the months and the last
the years.
Each of them starts with a blank value.


I want to control if the date is valid or blank (that means all the
dropdownlist have a blank value).

I need to add a customvalidator but I don't know on which control I have to
set it.

I create a javascript function to check the date.
here's the script
var client="<%=ClientID%>";
function checkDate(sender,args){
cbDay=document.getElementById(client+'_day');
cbMonth=document.getElementById(client+'_month');
cbYear=document.getElementById(client+'_year');
document.getElementById(client+'_err_msg').innerHTML="";
if
(cbDay.selectedIndex>0&&cbMonth.selectedIndex>0&&cbYear.selectedIndex>0)
args.IsValid=true;
else
{
if
(cbDay.selectedIndex==0&&cbMonth.selectedIndex==0&&cbYear.selectedIndex==0)
args.IsValid=true;
else
{
args.IsValid=false;
document.getElementById(client+'_err_msg').innerHTML="Invalid
date";
}
}
}

And more code to check if the date is correct if the user selects a month
that contains 30 or 31 days and adjust the number of days if the month is
february.
This function is called on an event that I add on the codebehind the month
and year dropdownlists like

month.attribute["onchange"]=setDate('"+day.ClientID+"','"+month.ClientID+"','"+year.ClientID+"')";
year.attribute["onchange"]=setDate('"+day.ClientID+"','"+month.ClientID+"','"+year.ClientID+"')";



On the aspx page, I have

<asp:dropDownlist runat="server" id="day" />
<asp:dropDownlist runat="server" id="month" />
<asp:dropDownlist runat="server" id="year" />



The codebehind binds the initial values for the dropdownlists.

My first idea was to set a customvalidator onto one dropdownlist
<asp:CustomValidator ruen="server" id="cvDate" ControlToValidate="day"
ClientValidationFunction="checkDate"
Display="non" ValidationGroupe="date" />
But it doesn't work, I get the "invalid date" if I change the day (of course
because the 2 other dropdownlists are blank).

How can I create the good customvalidator ?

Thanks for your help
Stan
 
H

Hans Kesting

Hi,
I have a web user control that contains 3 dropdownlists
it's a date selector, so one contains the days, one the months and the
last
the years.
Each of them starts with a blank value.
I want to control if the date is valid or blank (that means all the
dropdownlist have a blank value).

I need to add a customvalidator but I don't know on which control I
have to set it.

For a CustomValidator, you are allowed to leave "ControlToValidate" blank.

Hans Kesting
 
S

Stan SR

Many thanks Hans, it works.
But I have another question about the same user control.
I ve put 2 of the same user controls onto a webform.

On this webform, I put a button with a validationgroup attribute.

On the customvalidator (of the user control) I set ValidationGroup with the
same value of the webform validationgroup.

When I submit my webform,
it seems the customvalidator of the 2nd usercontrol is checked.

Do I miss something on the user control ?
I m not sure that I have to set a validationgroup on the webform.

Stan
 
S

Stan SR

Ok, I ve found the issue...
Many thanks Hans, it works.
But I have another question about the same user control.
I ve put 2 of the same user controls onto a webform.

On this webform, I put a button with a validationgroup attribute.

On the customvalidator (of the user control) I set ValidationGroup with
the same value of the webform validationgroup.

When I submit my webform,
it seems the customvalidator of the 2nd usercontrol is checked.

Do I miss something on the user control ?
I m not sure that I have to set a validationgroup on the webform.

Stan
 

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,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top