Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Tuesday, September 4, 2012

Client side validation of ASPX server controls with Best example



This is a simple validation control at the client side using JavaScript.  Here we have to perform the server side controls validation at the client-side using JavaScript. In below “RadiobuttonList” is server control and we had to perform validation at the client-side first once button click operation performed.

Why need it /Use of it:
The button click operation will not continue/Perform if you select the  “No” option in the radiobuttonlist. For this reason, we have to perform client-side validations using JavaScript for Asp.net server controls.

<td>
 <div>
  <asp:RadioButtonList ID="radiobuttonList1"  
    CssClass=" radiobuttonList1class" RepeatDirection="Horizontal"
    RepeatColumns="2" runat="Server">
    <asp:ListItem Text="Yes" Value="1"  Selected="True" />
    <asp:ListItem Text="No" Value="0" />
   </asp:RadioButtonList>
 </div>
</td>
<td>                             
 <asp:Button ID="btnSubmit" Text="Submit" OnClientClick="return
  btnsubmit_OnClientClick();" OnClick="btnSubmit_OnClick" runat="server"/>
</td>

var flag = true;
    function btnsubmit_OnClientClick()  {
         if (Page_IsValid) //server-side validation
         {
            var IsRequest = false;
            var radioObj = document.getElementById("<%=rbNeedBBAccount.ClientID %>");
            var radioList = radioObj.getElementsByTagName('input');
            for (var i = 0; i < radioList.length; i++) {
                if (radioList[i].checked) {

                    if (radioList[i].value == 1) {
                        flag = true;
                    }
                    else {
                     alert("Ur Request for xx  can’t be processed, as Need for xx  account field is set to NO.");
                        flag = false;
                    }
                }
            }
        }
        return flag;
    }

Note:
  1. If you are using more than one Validation Groups in page, so you need to explicitly specify the group.
  2. Best reference URL for Asp.Net Validation using JavaScript:
    1. http://praveenbattula.blogspot.in/2009/10/client-side-validation-of-aspx.html

No comments:

Post a Comment