"pavan" wrote:
Quote:
Originally Posted by
please help me as i have to validate a text box for date...
>
>
Quote:
Originally Posted by
please help me as i have to validate a text box for date...
>
>
by using a CompareValidator.
<asp:TextBox ID="txtDate" runat="server" />
<asp:CompareValidator ControlToValidate="txtDate" Operator="DataTypeCheck" Type="Date" ErrorMessage="Please enter a valid date" runat="server" ID="cmpValtxtDate" />
I wrote a function in javascript called "validateform" which checks all controls and validates the form.
Now when User clicks Submit Button I want to use this function.
The Problem is When i included that code in Button.attributes.add(...),Its giving me errormessages but also executing the server side code for button.
please help me
thxHow are you calling the Javascript function, from an OnClick event in the submit button or from an onSubmit call from the form. In either case you need to return false from the javascript function if a field does not validated. This will prevent the form from posting back and executing the server side code. If you could provide a code example, it would be easier for me to determine what is going wrong.
this is sample code
javascript :
function va()
{
if(document.all.p_title1.value=="")
{
alert("Team Name required");
return false;
}
else
return true;
}
Page Load :
ContinueButton.Attributes.Add("onclick","va();");Button Server Event :
private void ContinueButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect("SelectMaterial.aspx");}
thx
I noticed the other day that if you don't specify that you want a value returned in the onclick event as well as returning the value from the function that the form postsback to the server regardless of the Javascript function return value, with that said, all you need to do is change you attributes.add statement like so.
ContinueButton.Attributes.Add("onclick","return va();");
That should solve the postback problem.
thank you very much.
It worked
I do not think the RequiredFieldValidator control works with checkboxlist controls. Maybe it does in "asp.net" 2.0?
Your validation controls look interesting but I think I will try to do it myself.
It seems to be a little complicated though, but I will give it a go.
Thanks anyway!
Sincerely,
PeleP
The RequiredFieldValidator does work with the CheckBoxList and RadioButtonList. It does not work with ordinary CheckBox and RadioButton controls.
strange... i have tried that, and i cannot make it work. can you give me a quick example how you would do it?
this is what i have learned:
<asp:checkboxlist id="cb1" runat="server">
...items...
</asp:checkboxlist>
<asp.requiredfieldvalidator id="rfv" controltovalidate="cb1" runat="server"/>
so, are you sure? this is the last time... i will try to not disturb you further with stupid questions...
Sincerely,
PeleP
It is my understanding that's how to set it up. The RequiredFieldValidator has a property called InitialValue which has a string that is the text representing "blank". It defaults to "". When evaluating any type of list control, if there is no selection, the control has a value of "" which should match to InitialValue="".
There is one more piece. Each <asp:ListItem> has a Value property too. These must be assigned to something other than "".
<asp:ListItem value="1">text here</asp:ListItem>
Sorry. The RequiredFieldValidator does NOT word with the CheckboxList.
Here's why -- Remember that the builtin validation controls also render client script. It gets the required validation property from the value attribute of the HTML element that gets rendered on the client. Since an HTML value attribute can't be two things at once, the validated control must have a single logical value on the client.
This is why RadioButtonList can be validated, but CheckBoxList cannot
Hi Lee,
You are correct. I don't know where my head was when I was writing those posts. I suspect I confused the RadioButtonList's support with the CheckBoxList. Oh well. Thanks for clearing it up.
Hello, thank you both for following up this post. Now I understand why it is not possible, so I can and will leave it for now. Maybe the new asp.net 2.0 can give a solution to this!? I will look into that later.
Anyway, I temporarily solved it by using ordinary CheckBox's and have validated them using some client code.
Thanks for your help!
Sincerely, PeleP
Yes, there are workarounds, thank goodness. Probably the easiest is to create a customvalidator with both server and client-side code.
ASP 2.0 does not provide a solution. Understand, the inability to validate the CheckBoxList never really was a limitation of ASP in the first place. It's really caused by the limitations of HTML. (Remember, no matter what, the server still has to render HTML at the end of the whole deal.) There is no value= attribute of HTML checkboxes that let them determine the state of other checkboxes. Therefore, a built-in ASP validator couldn't automatically render "generic" client-side code for it.