Friday, April 29, 2016

ASP .Net Validation Sample(s) with Client Side Custom Validation Sample(s)

Required Field Validation With Initial value
<asp:RequiredFieldValidator ID="rfvtxtOther" runat="server" InitialValue="Other" ValidationGroup="vgSubmit" Enabled="false" ControlToValidate="txtOther" Display="Dynamic" CssClass="visitErrormsg otherErrormsg" ErrorMessage="Please provide other details" />



String Validation
<asp:RegularExpressionValidator ID="revtxtCity" runat="server" ValidationGroup="vgSubmit" ControlToValidate="txtCity" Display="Dynamic" CssClass="visitErrormsg" ErrorMessage="Please provide valid city name" ValidationExpression="^[a-zA-Z ]{2,30}$" />


Email Validation
<asp:RegularExpressionValidator ID="regtxtEmail" runat="server" ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="Please provided valid email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="vgSubmit" CssClass="visitErrormsg"></asp:RegularExpressionValidator>


Mobile Number Validation
<asp:RegularExpressionValidator ID="revtxtMobile" runat="server" ValidationGroup="vgSubmit" ControlToValidate="txtMobile" Display="Dynamic" CssClass="visitErrormsg" ErrorMessage="Please provide valid mobile number"                                   ValidationExpression="^[1-9]{1}[0-9]{9}$" />

Address Validation
<asp:RegularExpressionValidator ID="revtxtAddress" runat="server" ValidationGroup="vgSubmit" ControlToValidate="txtAddress" Display="Dynamic" CssClass="visitErrormsg" ErrorMessage="Please provide valid address"
                                    ValidationExpression="^[a-zA-Z ,./~!@#$%^&*()_+{}\|:']{5,50}$" />

Custom Validation to Upload at least single file
<asp:CustomValidator ID="cvCommon" runat="server" ValidationGroup="vgSubmit" ClientValidationFunction="file_ValidationSingle" CssClass="visitErrormsg" ErrorMessage="Please provided at least 1 file in image format." />

function file_ValidationSingle(sender, args) {
            var f1 = document.getElementById('<%=file_one.ClientID%>').value;
            var f2 = document.getElementById('<%=file_two.ClientID%>').value;
            var f3 = document.getElementById('<%=file_three.ClientID%>').value;
            if (f1 == "" && f2 == "" && f3 == "") {
                args.IsValid = false;  // field is empty
            } else {
                args.IsValid = true;
            }
        }

File Upload Control Validation with max 1 MB and only image format
 <asp:CustomValidator ID="cvfile_one" runat="server" ValidationGroup="vgSubmit" ControlToValidate="file_one" ClientValidationFunction="file_Validation" CssClass="visitErrormsg" ErrorMessage="Please provided file in image format with max 1 MB." />

  function file_Validation(sender, args) {
            var xyz = document.getElementById(sender.controltovalidate);
           // var array = ['pdf', 'doc', 'docx', 'txt', 'xlsx', 'ppt', 'zip'];
            var array = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
            var xyz = v.value;
            var Extension = xyz.value.substring(xyz.value.lastIndexOf('.') + 1).toLowerCase();
            if (array.indexOf(Extension) <= -1) {
                //alert("Please Upload only pdf,doc,zip,txt.xlsx and ppt extension flle");
                args.IsValid = false;
            } else {
                //alert(xyz.files[0].size);
                if (xyz.files[0].size > 1048576) {  //1MB
                    args.IsValid = false;
                }
            }
        }

Enable Validators from Client Site
ValidatorEnable(document.getElementById("rfvtxtOther"), true);

Disable Validator from Client Side
ValidatorEnable(document.getElementById("rfvtxtOther"), false);

Check From Validation from client side without any Validation Group
if (Page_ClientValidate()){
 //Form Valid
}else{
 //Invalid Form
}

Check From Validation from client side with Validation Group

if (Page_ClientValidate("ValidationGroupName")){
 //Form Valid
}else{
 //Invalid Form
}

No comments:

Post a Comment