Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Wednesday, June 25, 2014

How to use Asp.net regular expression

How to use regular expression in asp.net

Regular expression is a pattern matching standard (way ) for string parsing and replacement. That used on a wide range of platforms and programming (Application development) environments. Some time we can say that Regular expression is a way of Clint side validation. The short form of Regular Expression is regales. This reg form regular and exes from expressions.

Regular expression in asp.net

•Use regular expressions for constrain input, apply format rules, and check lengths of string or input data.
•Use the ASP.NET Regular Expression Validator control for constrain and validate input with given condition.
•Use the Regular Expression (regexes) class to constrain and validate client input data.
•knowledge common regular expressions that can be used to constrain input data in programming.

 Example of regular expressions can be used to parse dates, url(universal resource locator) and email addresses, file type, log files, text, String, Numeric values, configuration files, command line switches or programming scripts.

Basically there are fallowing steps To validate a server control's input using a Regular Expression Validator are:-

1 .Add a Regular Expression Validator control in asp.net page.
2 .Set Validate Control (by using control ID).
3. Set the Validation Expression property with regular expression (validation conditions).
4. Set the Error Message property, for define the Error message to display if the validation fails.

Regular expression for Enter number only in Textbox :


<asp:TextBox ID="Txtbox" runat="server"
ValidationGroup="ch"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
Control To Validate="Txtbox" ErrorMessage="You must enter a phone number in the form of (999) 999-9999."
ValidationExpression="^[\d\s\(\)\-]+$" ValidationGroup="ch"/>

Regular expression  for URL validation 

/((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)/i

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$

Regular expression  for ZIP code validation

^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$

Regular expression  for Password Validation 

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$

Regular expression  for Non- negative integer validation

^\d+$

Regular expression  for Currency (non- negative) validation

^\d+(\.\d\d)?$

Regular expression  for Currency (positive or negative) validation

^(-)?\d+(\.\d\d)?$

Regular expression  for Phone Number Validation :

^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$

Regular expression  for Email Validation : 

^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$

Regular expression  for Image format validation 


<asp:RegularExpressionValidator ID="revImage" ControlToValidate="uplImage"
ValidationExpression="^.*\\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F))$" Text=" ! Invalid image type" runat="server" />

Related Questions :

  1. building a multiple email regular expression for a regular expression validator on asp.net with c#   Programming
  2. regular expression validation in asp.net
  3. Prevent Range Validator to show error message with regular expression validator in asp.net Parogramming
  4. in Asp.net Text with RegEx Validator not working
  5. Regex Help needed with field validation in asp.net
  6. Regex for required field in Asp?
  7. Validate date field in asp.net using validator control in Prograaming

Other Impotent Post: