I want to validate a WebCombo on the client side using a custom validator. Validating that a dropdown selection has been made after the "Go" button is selected. Which means that I want to load/activate the javascript function at the end of the last page event and before selecting the "Go" button.
JavaScript function:
function ComboCheck(sender, args){ var combo = igcmbo_getComboById('<%=wcPortName.ClientID %>').selectedIndex; if(selectedIndex == -1){ args.IsValid = false; } return args.IsValid; }
Validator:
<asp:CustomValidator id="validCustCombo" runat="server" CssClass="Text" ErrorMessage="Please make a selection in the dropdown!" ClientValidationFunction="ComboCheck"></asp:CustomValidator></TD>
If it were me, I would just put the validation when you hit the "Go" button.
<asp:Button ID="btnGo" runat="server" Text="Go" OnClientClick="Javacript: return Validate()" /> function Validate(){ var combo = igcmbo_getComboById('<%=wcPortName.ClientID %>').selectedIndex; if(selectedIndex == -1){ alert("Please Select something"); return false; } return true; }
<asp:Button ID="btnGo" runat="server" Text="Go" OnClientClick="Javacript: return Validate()" />
function Validate(){ var combo = igcmbo_getComboById('<%=wcPortName.ClientID %>').selectedIndex; if(selectedIndex == -1){
alert("Please Select something"); return false; } return true; }
Returning false will prevent the page from posting back and you can then either show the error message or do any other kinda of validation. ClientSide code will process before the server side. Of course, this option doesn't use your CustomValidator
This is what I have:
function CheckCombo(){
var SelectedRow = igcmbo_getComboById(comboGD).selectedIndex;
if(SelectedRow == -1) {
alert("Please Select Gaming Device") ;
return false;
}
return True
BUT I am running .NET 1.1 Thus the OnClick="CheckCombo" will not run?
Hmmm, I have't used 1.1 in a long time.
Are you using an asp button or HTML Input button?
Try this
<input type="button" runat=server onclick="CheckCombo()" OnServerClick="RunYourServerSideFunction" />
Not using an html button, but asp:Button
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script type="text/javascript"> function wddClientValidation(sender, e) { var wdd = $find(sender.controltovalidate); if (wdd.get_selectedItemIndex() == -1) e.IsValid = false; return e.IsValid; } </script></asp:Content><asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="wddClientValidation" ControlToValidate="WebDropDown1" ErrorMessage="Require" ></asp:CustomValidator> <input id="Text1" type="text" /></h2> <ig:WebDropDown ID="WebDropDown1" runat="server" Width="200px" EnableAutoFiltering="Client" AutoSelectOnMatch="false" > <Items> <ig:DropDownItem Text="AA 1" Value="1"> </ig:DropDownItem> <ig:DropDownItem Text="BB 2" Value="2"> </ig:DropDownItem> <ig:DropDownItem Text="CC 3" Value="3"> </ig:DropDownItem> <ig:DropDownItem Text="DD 4" Value="4"> </ig:DropDownItem> <ig:DropDownItem Text="EE 5" Value="5"> </ig:DropDownItem> <ig:DropDownItem Text="FF 6" Value="6"> </ig:DropDownItem> </Items> <ClientEvents Blur="Blur" /> </ig:WebDropDown> </asp:Content>
Hi mamia77,
the above posts are about WebCombo, which is a different control, but i see you are referring to WebDropDown. What does get_selectedItemIndex return in your case ? do you select anything before submitting the form, or it stays with the default control values?
Thanks,
Angel