Tuesday, 30 January 2018

Highlight asp.net controls using required field validate with javascript

/////////////////////html

  <asp:TextBox ID="TxtUserName" OnTextChanged="Txtuser_TextChanged" CssClass="form-control" runat="server" AutoPostBack="True"></asp:TextBox>

<asp:RequiredFieldValidator ID="RFVTxtUserName" runat="server" ControlToValidate = "TxtUserName" Display="Dynamic" ValidationGroup="ValReg"/>





//////////JS
<script type="text/javascript">
        function Validate(ValGrp) {
            var FLAG;
            debugger;
            if (Page_Validators.length > 0) {
                for (var i = 0; i < Page_Validators.length; i++) {
                    var val = Page_Validators[i];
                    var ctrl = document.getElementById(val.controltovalidate); //Validate                   
                    if (ctrl != null && ctrl.style != null) {
                        if (ctrl.value == "" && (ctrl.getAttribute('style') != 'display: none;') && (ctrl.getAttribute('style') != 'border: 1px solid red; display: none;')) {
                            if (Page_Validators[i].validationGroup == ValGrp ) {
                                ctrl.style = "border: 1px solid red";
                                FLAG = 1;
                            }
                        }
                        else {
                            ctrl.style.borderColor = '';
                        }
                    }
                }
            }
            if (FLAG == 1) {
                return false;
            }
            else {
                return true;
            }
        }

        </Script>

No comments:

Post a Comment

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...