Example of Regular Expression


Example of Regular Expression

Example of Regular Expression

Regular Expression are very powerful tools for matching,searching and replacing text.
For example, regular expressions can be used to parse dates, urls and email addresses, log files, configuration files, command line switches or programming scripts.

To validate a server control's input using a Regular Expression Validator

I. Just Drag and drop Regular Expression Validator control to your page.
II. Set the "ControlToValidate" property to indicate which control you want to validate.
III. Set the "ValidationExpression" property to an appropriate regular expression.
IV. Set the ErrorMessag"e" property to define the message to display if the validation fails.



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Email ID
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ErrorMessage="Enter valid Email id"
ControlToValidate="txtEmail"
ValidationExpression="^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$">
</asp:RegularExpressionValidator>
Numbers <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ErrorMessage="Enter Only Number"
ControlToValidate="txtNumber"
ValidationExpression="^[0-9]{1,18}$">
</asp:RegularExpressionValidator>

Alphaneumeric
<asp:TextBox ID="txtAlpha" runat="server"></asp:TextBox>


<asp:RegularExpressionValidator ID="RegularExpressionValidator3"
runat="server" ErrorMessage="Enter Only Alphaneumeric"
ControlToValidate="txtAlpha" ValidationExpression="^[^;>;&;<;%?*!~'`;:,."";+=|]*$"
></asp:RegularExpressionValidator>



Decimal <asp:TextBox ID="txtDecimal" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4"
runat="server" ErrorMessage="Enter Decimal Only"
ControlToValidate="txtDecimal"
ValidationExpression="^\d{0,7}(\.\d{1,2})?$">
</asp:RegularExpressionValidator>

Date <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5"
runat="server" ErrorMessage="Enter Date Format dd/MM/yyyy"
ControlToValidate="txtDate"
ValidationExpression="dd/MM/yyyy">
</asp:RegularExpressionValidator>


</div>
</form>
</body>
</html>


Related Articles

Validators Example 1

Validation Group doesn't provide complete control of conditional enabling/disabling of validators.

Validator Example 2

Custom Required validator : It is single server validator that validates controls like radiobuttonlist,checkboxlist,dropdownlist and text box. One can use custom js function for specific validation.

More articles: Regular expression validator Validations Validators

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: