REGULAR EXPRESSION
RegularExpressionValidator control determine whether an input control's entered value matches a pattern defined by a regular expression.
this validation control is very useful to check predictable sequences of characters.
DEMO
regularexpressionvalidator is mostly use for validate email address, social security number, telephone number, postal (zip) code etc.
the commonly used syntax constructs for regular expressions:
First - Select New - WebForm - Select Textbox & Label & Button
Next - Email Id Validation Expression Default Setting Like that below
Next - Html Coding for Regular Expression
Input Given Without Proper Mobile Number & Email Id Show Error Message
Input Given to Proper Hide the Error Message
RegularExpressionValidator control determine whether an input control's entered value matches a pattern defined by a regular expression.
this validation control is very useful to check predictable sequences of characters.
DEMO
Html Coding
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table><tr><td>
<asp:Label ID="Label1" runat="server"
Text="Mobile Number"></asp:Label>
</td><td>
<asp:TextBox ID="txtMobile"
runat="server"></asp:TextBox>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator2"
runat="server"
ControlToValidate="txtMobile"
ErrorMessage="Invalid Number"
ValidationExpression="^((\\+91-?)|0)?[0-9]{10}$">
</asp:RegularExpressionValidator>
</td></tr>
<tr><td>
<asp:Label ID="Label2" runat="server" Text="Email ID"></asp:Label>
</td><td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtEmail"
ErrorMessage="Invalid Email ID"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td></tr>
<tr><td colspan ="2">
<asp:Button ID="Button1" runat="server" Text="Save" />
</td></tr>
</table>
</div>
</form>
</body>
</html>
Mobile Number Validation Expression :
^((\\+91-?)|0)?[0-9]{10}$
regularexpressionvalidator is mostly use for validate email address, social security number, telephone number, postal (zip) code etc.
Property | Description |
---|---|
BackColor | The background color of the RegularExpressionValidator control |
ControlToValidate | The id of the control to validate |
Display | The display behavior for the validation control. Legal values are:
|
EnableClientScript | A Boolean value that specifies whether client-side validation is enabled or not |
Enabled | A Boolean value that specifies whether the validation control is enabled or not |
ErrorMessage | The text to display in the ValidationSummary control when validation fails.Note: This text will also be displayed in the validation control if the Text property is not set |
ForeColor | The foreground color of the control |
id | A unique id for the control |
IsValid | A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid |
runat | Specifies that the control is a server control. Must be set to "server" |
BackColor | The background color of the RegularExpressionValidator control |
Text | The message to display when validation fails |
ValidationExpression | Specifies the expression used to validate input control. The expression validation syntax is different on the client than on the server. JScript is used on the client. On the server, the language you have specified is used |
the commonly used syntax constructs for regular expressions:
Character Escapes | Description |
---|---|
\b | Matches a backspace |
\t | Matches a tab |
\r | Matches a carriage return |
\v | Matches a vertical tab |
\f | Matches a form feed |
\n | Matches a new line |
\ | Escape character |
Apart from single character match, a class of characters could be specified that can be matched, called the metacharacters.
Metacharacters | Description |
---|---|
. | Matches any character except \n |
[abcd] | Matches any character in the set |
[^abcd] | Excludes any character in the set |
[2-7a-mA-M] | Matches any character specified in the range |
\w | Matches any alphanumeric character and underscore |
\W | Matches any non-word character |
\s | Matches whitespace characters like, space, tab, new line etc. |
\S | Matches any non-whitespace character |
\d | Matches any decimal character |
\D | Matches any non-decimal character |
Quantifiers could be added to specify number of times a character could appear
Quantifier | Description |
---|---|
* | Zero or more matches |
+ | One or more matches |
? | Zero or one matches |
{N} | N matches |
{N,} | N or more matches |
{N,M} | Between N and M matches |
First - Select New - WebForm - Select Textbox & Label & Button
Select - Regular Expression From Validation Toolbox & Change the Error Message & Control To Validate
Next -F4 -Property Window Selext -Validation Expression given to Mobile Code Manually
Mobile Number Validation Expression :
^((\\+91-?)|0)?[0-9]{10}$
Next - Email Id Validation Expression Default Setting Like that below
Next - Html Coding for Regular Expression
Next - F5 Press Run The Program
Input Given Without Proper Mobile Number & Email Id Show Error Message
Input Given to Proper Hide the Error Message
0 comments:
Post a Comment