How to use RegularExpressionValidator in asp.net or RegularExpressionValidator Example in Asp.net RegularExpressionValidator for Email and RegularExpressionValidator for File upload Control.

asp.net learning

Introducation:In this Example iam going to explain how to use  RegularExpressionValidator
in Asp.net

Description:
The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern.

Regular Expression Validator for Email:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Regular Expression Validator</title>
</head>
<body>
<form id="form1" runat="server">

<div style="margin-left: 250px">
<asp:Label ID="Label1" runat="server" Text="Email ID:"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"             ErrorMessage="Please Enter Valid Email ID "
  ControlToValidate="txtEmail"
  ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
    </div>
  
 <div style="margin-left: 320px; margin-top: 50px;">
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>


Regular Expression Validator for File Upload Control: 



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Regular Expression Validator</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="margin-left: 250px">
  
 <asp:Label ID="Label1" runat="server" Text="Upload File:"></asp:Label>
  <asp:FileUpload ID="FileUpload1" runat="server" />
   
 <asp:RegularExpressionValidator ID="revPFCompanyLogo" runat="server"   
ControlToValidate="FileUpload1"                                                           
  ValidationExpression="^.+(.jpg|.bmp|.png|.jpeg|.gif)$"
  ErrorMessage="Please upload  [.jpg |.bmp |.png |.jpeg |.gif ] files Only.">
   
 </asp:RegularExpressionValidator>
    </div>
    

   <div style="margin-left: 320px; margin-top: 50px;">
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

 

No comments: