i want to validate a dropdownlist and a textbox like this:
There are two types in the dropdownlist: if type 1 is chosen, then the
items typed in the textbox must contain at least a ';'.
In no case, the textbox may remain empty.
My problem is that the server validation doesn't occur and that the postback occurs. I rhought that "rertun false" prevents postback.
How can both validations: javascript and server validation work together?
<script language="javascript" type="text/javascript">
function test()
{
var vrg=document.getElementById("tw").value
var type=document.getElementById("type").value
if (type=="type 1")
{
if (vrg.indexOf(";")==-1)
{
alert("the items must be separated with a ; ")
return false
}
}
}
</script>
Please allow me to make a couple of modifications to the suggested solution:
1. When using ASP.NET 2+, Page_ClientValidate() takes one parameter, the validation group name. If you are not using any groups, its still passed an empty string.
2. Set CausesValidation=false on the button. When its true, it also calls Page_ClientValidate(). Since CausesValidation=false turns off server side validation for the button, call Page.Validate() in the button's Click post back event handler. Do this first.
Then test Page.IsValid is true (always do this step on any page using validation).
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
tartuffe2
Member
147 Points
446 Posts
problem with combined javascript validation and server validation
Jul 13, 2007 08:34 PM|LINK
Hi,
i want to validate a dropdownlist and a textbox like this:
There are two types in the dropdownlist: if type 1 is chosen, then the
items typed in the textbox must contain at least a ';'.
In no case, the textbox may remain empty.
My problem is that the server validation doesn't occur and that the postback occurs. I rhought that "rertun false" prevents postback.
How can both validations: javascript and server validation work together?
Thanks
Tartuffe
<asp:TextBox ID="vrg" runat="server" TextMode=MultiLine/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="vrg"
<asp:DropDownList ID="type" runat="server">
<asp:ListItem Text="choose" Value="choose" />
<asp:ListItem Text="type 1" Value="type 1" />
<asp:ListItem Text="type 2" Value="type 2" />
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator3"
ControlToValidate="type" EnableClientScript=true
InitialValue="choose" text="*" runat=server>
<asp:Button ID="Button1" runat="server" OnClientClick="return test() " Text="go />
<script language="javascript" type="text/javascript">
function test()
{
var vrg=document.getElementById("tw").value
var type=document.getElementById("type").value
if (type=="type 1")
{
if (vrg.indexOf(";")==-1)
{
alert("the items must be separated with a ; ")
return false
}
}
}
</script>
Kelsey
Contributor
4222 Points
797 Posts
Re: problem with combined javascript validation and server validation
Jul 13, 2007 09:27 PM|LINK
Instead of using OnClientClick do the following:
Remove that OnClientClick
In your Page_Load add the following code:
Button1.Attributes.Add("onClick", "if (Page_ClientValidate()) return test(); else return false;");
Your button click will be aborted correctly if you return false.
I have never actually noticed the OnClientClick attribute since I usually just do it via code.
tartuffe2
Member
147 Points
446 Posts
Re: problem with combined javascript validation and server validation
Jul 13, 2007 10:14 PM|LINK
thanks, i'll try
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: problem with combined javascript validation and server validation
Jul 15, 2007 05:46 PM|LINK
Please allow me to make a couple of modifications to the suggested solution:
1. When using ASP.NET 2+, Page_ClientValidate() takes one parameter, the validation group name. If you are not using any groups, its still passed an empty string.
2. Set CausesValidation=false on the button. When its true, it also calls Page_ClientValidate(). Since CausesValidation=false turns off server side validation for the button, call Page.Validate() in the button's Click post back event handler. Do this first. Then test Page.IsValid is true (always do this step on any page using validation).
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com