Thursday, 1 April 2021

Validate required fields in asp.net using javascript

 /////////////////////html
 
<table class="mt-3">
                    <tr>
                        <td>Name
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="Required_txtName" runat="server" ValidationGroup="TestGrp"
                                ControlToValidate="txtName" Display="Dynamic" ErrorMessage="Required"
                                ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Salary
                        </td>
                        <td>
                            <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="Required_txtSalary" runat="server"
                                ControlToValidate="txtSalary" Display="Dynamic" ErrorMessage="Required" ValidationGroup="TestGrp"
                                ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Department
                        </td>
                        <td>
                            <asp:DropDownList ID="ddl_Dept" runat="server">
                                <asp:ListItem Value="0">-- Select --</asp:ListItem>
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="Required_ddl_Dept" runat="server" ValidationGroup="TestGrp"
                                ControlToValidate="ddl_Dept" Display="Dynamic" InitialValue="0"
                                ErrorMessage="Required" ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <asp:Button ID="btnSubmit" runat="server" Text="Sumbit" ValidationGroup="TestGrp" OnClientClick="return Validate_RequiredFiled_Save('TestGrp');" />
                        </td>
                    </tr>
                </table>


=========================  JS

function Validate_RequiredFiled_Save(groupName) {
                    if (typeof (Page_ClientValidate) == 'function') {
                        return Page_ClientValidate(groupName);;
                    }
                    if (Page_IsValid) {
                        alert(groupName + "successful");
                        //do something
                    }
                }
========================= Result

required field validator in asp.net using java-script if not disable

 /////////////////// JS
<script type="text/javascript">
        function validate_Input_Highlight() {
            for (var i = 0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                var ctrl = document.getElementById(val.controltovalidate);
             if ((ctrl.value == "" || ctrl.value == "0") && (ctrl.getAttribute('style') != 'display: none;') && (ctrl.getAttribute('style') != 'border: 1px solid red; display: none;')) {                    if (!val.isvalid) {

                        ctrl.style.borderColor = '#FF0000';
                        ctrl.style.backgroundColor = '#fce697';
                    }
                    else {
                        ctrl.style.borderColor = '';
                        ctrl.style.backgroundColor = '';
                    }
                }
            }
        }
    </script


====================================HTML

                    <table>
                        <tr>
                            <td>Emp Name
                            </td>
                            <td>
                                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Required_txtName" runat="server" ValidationGroup="TestGrp"
                                    ControlToValidate="txtName" Display="Dynamic" ErrorMessage="*"
                                    ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>Salary
                            </td>
                            <td>
                                <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Required_txtSalary" runat="server"
                                    ControlToValidate="txtSalary" Display="Dynamic" ErrorMessage="*" ValidationGroup="TestGrp"
                                    ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>Department
                            </td>
                            <td>
                                <asp:DropDownList ID="ddl_Dept" runat="server">
                                    <asp:ListItem Value="0">-- Select --</asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="Required_ddl_Dept" runat="server" ValidationGroup="TestGrp"
                                    ControlToValidate="ddl_Dept" Display="Dynamic" InitialValue="0"
                                    ErrorMessage="*" ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <asp:Button ID="btnSubmit" runat="server" Text="Sumbit" ValidationGroup="TestGrp" OnClientClick="return VerifyAndSave();" />
                            </td>
                        </tr>
                    </table>
           
============================ Result



required field validator in asp.net using java-script

 /////////////////// JS
<script type="text/javascript">
        function validate_Input_Highlight() {
            for (var i = 0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                var ctrl = document.getElementById(val.controltovalidate);
                if (ctrl != null && ctrl.style != null && (ctrl.value == "" || ctrl.value == "0")) {
                    if (!val.isvalid) {
                        ctrl.style.borderColor = '#FF0000';
                        ctrl.style.backgroundColor = '#fce697';
                    }
                    else {
                        ctrl.style.borderColor = '';
                        ctrl.style.backgroundColor = '';
                    }
                }
            }
        }
    </script
====================================HTML

                    <table>
                        <tr>
                            <td>Emp Name
                            </td>
                            <td>
                                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Required_txtName" runat="server" ValidationGroup="TestGrp"
                                    ControlToValidate="txtName" Display="Dynamic" ErrorMessage="*"
                                    ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>Salary
                            </td>
                            <td>
                                <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Required_txtSalary" runat="server"
                                    ControlToValidate="txtSalary" Display="Dynamic" ErrorMessage="*" ValidationGroup="TestGrp"
                                    ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>Department
                            </td>
                            <td>
                                <asp:DropDownList ID="ddl_Dept" runat="server">
                                    <asp:ListItem Value="0">-- Select --</asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="Required_ddl_Dept" runat="server" ValidationGroup="TestGrp"
                                    ControlToValidate="ddl_Dept" Display="Dynamic" InitialValue="0"
                                    ErrorMessage="*" ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <asp:Button ID="btnSubmit" runat="server" Text="Sumbit" ValidationGroup="TestGrp" OnClientClick="return VerifyAndSave();" />
                            </td>
                        </tr>
                    </table>
           
============================ Result



How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...