Resources » Code Snippets » ASP.NET WebForms

How to write validationexpression for a RegularExpressionValidator


Posted Date: 21-May-2008  Last Updated:   Category: ASP.NET WebForms    
Author: Member Level: Bronze    Points: 10



The following code sample shows how to write validationexpression for a RegularExpressionValidator

Email validation

<asp:TextBox ID="txtEmail" runat="Server" Columns="40" />
<asp:RegularExpressionValidator ID="revemail" runat="server" ControlToValidate="txtEmail" ErrorMessage="Eg:abcde@abc.ad" validationexpression="^.+\@.+\..+$" Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

Pincode validation

<asp:TextBox ID="txtPincode" runat="Server" Columns="40" />
<asp:RegularExpressionValidator ID="revPincode" runat="server" ControlToValidate="txtPincode" ErrorMessage="enter correct pincode" validationexpression="^.+\@.+\..+$" Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

validating password and confirm password

<asp:TextBox ID="TextBox3" runat="Server" TextMode="Password" Columns="40" />
<asp:TextBox ID="textbox2" runat="Server" TextMode="Password" Columns="40" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Passwords don't match!" ControlToValidate="TextBox3"
ControlToCompare="textbox2" ></asp:CompareValidator>

Validating year

<asp:TextBox ID="txtValidityDate" runat="server" />
<asp:RegularExpressionValidator ID="revvaliditydate" runat="server" ControlToValidate="txtValidityDate" ErrorMessage="Date Format(dd/mm/yyyy)"
ValidationExpression="^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$" Display="static" SetFocusOnError="true"></asp:RegularExpressionValidator>

Validation For 999-99-9999 Format

ValidationExpression="(^\d{3}\-?\d{2}\-?\d{4}$)"

Validation For(555) 555-5555 Format

ValidationExpression="(^((\(?\d{3}\))|(\d{3}))?\s?\d{3}[\-.]?\d{4}$)"


Did you like this resource? Share it with your friends and show your love!

Responses to "How to write validationexpression for a RegularExpressionValidator"

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

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Ali
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India