Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show validation control's Error messages in Alert box?

Tags:

asp.net

I am using 4 required field validators,4 regular expression validators and 4 compare validators for 4 text boxes.Is it possible to show error messages

in an alert or message box when validation fails?

If possible please send code sample.

Regards,

NSJ

like image 260
sriramjitendra Avatar asked Feb 01 '11 12:02

sriramjitendra


3 Answers

    <form id="form1" runat="server">
    <asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
    <asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
        Display="None" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
    <asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
        <asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
        <asp:ListItem Value="0">Male</asp:ListItem>
        <asp:ListItem Value="1">Female</asp:ListItem>
    </asp:DropDownList>
    <asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
        Display="None" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
        ValueToCompare="-1"></asp:CompareValidator>
    <br />
    <asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
    <br />
    <asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
        ShowSummary="False" ValidationGroup="Validation" />
    </form>

You should use the same ValidationGroup text on all validation controls and add a ValidationSummary with the ValidationGroup and ShowMessageBox="true"

like image 53
PMC Avatar answered Oct 20 '22 15:10

PMC


Use the following code , just set the correct messeges that u want :

<asp:ValidationSummary ID="ValidationSummary1"   ShowMessageBox ="true" 
                    runat="server"  />  


<asp:textbox id="txt1" runat="server"></asp:textbox>
&nbsp;<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server"
    controltovalidate="txt1" errormessage="Please Enter Only Numbers" validationexpression="^[-+]?\d+(\.\d+)?$">
    </asp:regularexpressionvalidator>
&nbsp;
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt1"
    errormessage="please enter txt1">*</asp:requiredfieldvalidator>
<asp:textbox id="txt2" runat="server"></asp:textbox>
&nbsp;<asp:regularexpressionvalidator id="RegularExpressionValidator2" runat="server"
    controltovalidate="txt2" errormessage="Please Enter Only Charcters" validationexpression="^[a-zA-Z\s.]*$">
    </asp:regularexpressionvalidator>
&nbsp;
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt2"
    errormessage="please enter txt2">*</asp:requiredfieldvalidator>
    &nbsp;
<asp:textbox id="txt3" runat="server"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt3"
    errormessage="please enter txt3">*</asp:requiredfieldvalidator>





<asp:Button ID="btnok" runat="server" Text="ok"/>
like image 3
MUS Avatar answered Oct 20 '22 13:10

MUS


Don't do it. Users hate alert box error messages. It is a horrible UI design. Put the error messages on the form where the user can read them and they stay while the user makes changes or moves on without having to click on an alert box. Especially when there are multiple corrections to be made, they need to be able to read the errors while they make the corrections.

like image 1
Info Lynn Avatar answered Oct 20 '22 14:10

Info Lynn