Regular Expression Validation Control in Asp.Net C#

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

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.




PropertyDescription
BackColorThe background color of the RegularExpressionValidator control
ControlToValidateThe id of the control to validate
DisplayThe display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
EnableClientScriptA Boolean value that specifies whether client-side validation is enabled or not
EnabledA Boolean value that specifies whether the validation control is enabled or not
ErrorMessageThe 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
ForeColorThe foreground color of the control
idA unique id for the control
IsValidA Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
runatSpecifies that the control is a server control. Must be set to "server"
BackColorThe background color of the RegularExpressionValidator control
TextThe message to display when validation fails
ValidationExpressionSpecifies 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 EscapesDescription
\bMatches a backspace
\tMatches a tab
\rMatches a carriage return
\vMatches a vertical tab
\fMatches a form feed
\nMatches a new line
\Escape character

Apart from single character match, a class of characters could be specified that can be matched, called the metacharacters.


MetacharactersDescription
.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
\wMatches any alphanumeric character and underscore
\WMatches any non-word character
\sMatches whitespace characters like, space, tab, new line etc.
\SMatches any non-whitespace character
\dMatches any decimal character
\DMatches any non-decimal character

Quantifiers could be added to specify number of times a character could appear


QuantifierDescription
*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