Search This Blog

Saturday, August 4, 2012

Regular Expressions Validation for .Net Form


 A regular expression is a set of characters that specify a pattern. The term "regular" has nothing to do with a high-fiber diet. It comes from a term used to describe grammars and formal languages.

Regular Expression Example:

"^[a-zA-Z0-9_]*$"
This works for .NET regular expressions, and probably a lot of other languages as well.

Breaking it down:

^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string

Some Regular Expression for Registration form in .net.

1.Without charecter numaric not allow:
^[a-zA-Z][a-zA-Z0-9().@_\s]+$

2."Enrolment no” Validate for 7 digits (first 3 digits alphabets and next 4 digits numeric)
[a-zA-Z]{3}\d{4}

3.Enter First Name, It should be Greater Than 2 Characters, Numeric value is not allowed, And space is also not allowed in Personal Details Block"
[A-Za-z]{3,}

4.Remove Space From login
^[^\s]+$

5.Loginid should contain [6 to 15 character] with no special Character
^[a-zA-Z0-9\'\.\,\-\+\*\%\(\)]{6,15}$

6.Loginid should contain [6 to 15 character] at least one digit [0-9], one alphabet [A-Z] [a-z] and one special character [@#&*!].
(?=^.{6,15}$)(?=.*\d)(?=.*\W+)(?![.\n])(?=.*[a-zA-Z]).*$

7.Enter 6 Digits Password
[0-9]{6,}

8.Enter Valid official Mobile No of 10 Digits Starting with 7/8/9 In Office Address Block
^[789]\d{9}$

9.Invalid Official EMailID In Office Address Block
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

10.Enter Valid Website In Office Address Block
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

11.JPEG/GIF/PNG files only allowed
^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$

12.Enter Father Name and it should be Greater Than 2 Character And Numeric value is not allowed.
.{2}.*


Regular Expression
Matches
^A
"A" at the beginning of a line
A$
"A" at the end of a line
A^
"A^" anywhere on a line
$A
"$A" anywhere on a line
^^
"^" at the beginning of a line
$$
"$" at the end of a line

Regular Expression
Matches
[]
The characters "[]"
[0]
The character "0"
[0-9]
Any number
[^0-9]
Any character other than a number
[-0-9]
Any number or a "-"
[0-9-]
Any number or a "-"
[^-0-9]
Any character except a number or a "-"
[]0-9]
Any number or a "]"
[0-9]]
Any number followed by a "]"
[0-9-z]
Any number, or any character between "9" and "z".

[0-9\-a\]]
Any number, or
a "-", a "a", or a "]"
          
Regular Expression
Matches
*
Any line with an asterisk
\*
Any line with an asterisk
\\
Any line with a backslash
^*
Any line starting with an asterisk
^A*
Any line
^A\*
Any line starting with an "A*"
^AA*
Any line if it starts with one "A"
^AA*B
Any line with one or more "A"'s followed by a "B"
^A\{4,8\}B
Any line starting with 4, 5, 6, 7 or 8 "A"'s
followed by a "B"
^A\{4,\}B
Any line starting with 4 or more "A"'s
followed by a "B"
^A\{4\}B
Any line starting with "AAAAB"
\{4,8\}
Any line with "{4,8}"
A{4,8}
Any line with "A{4,8}"


Regular Expression
Class
Type
Meaning
.
all
Character Set
A single character (except newline)
^
all
Anchor
Beginning of line
$
all
Anchor
End of line
[...]
all
Character Set
Range of characters
*
all
Modifier
zero or more duplicates
\<
Basic
Anchor
Beginning of word
\>
Basic
Anchor
End of word
\(..\)
Basic
Backreference
Remembers pattern
\1..\9
Basic
Reference
Recalls pattern
_+
Extended
Modifier
One or more duplicates
?
Extended
Modifier
Zero or one duplicate
\{M,N\}
Extended
Modifier
M to N Duplicates
(...|...)
Extended
Anchor
Shows alteration
_
\(...\|...\)
EMACS
Anchor
Shows alteration
\w
EMACS
Character set
Matches a letter in a word
\W
EMACS
Character set
Opposite of \w

Regular Expression
Type
Meaning
\t
Character Set
tab
\n
Character Set
newline
\r
Character Set
return
\f
Character Set
form
\a
Character Set
alarm
\e
Character Set
escape
\033
Character Set
octal
\x1B
Character Set
hex
\c[
Character Set
control
\l
Character Set
lowercase
\u
Character Set
uppercase
\L
Character Set
lowercase
\U
Character Set
uppercase
\E
Character Set
end
\Q
Character Set
quote
\w
Character Set
Match a "word" character
\W
Character Set
Match a non-word character
\s
Character Set
Match a whitespace character
\S
Character Set
Match a non-whitespace character
\d
Character Set
Match a digit character
\D
Character Set
Match a non-digit character
\b
Anchor
Match a word boundary
\B
Anchor
Match a non-(word boundary)
\A
Anchor
Match only at beginning of string
\Z
Anchor
Match only at EOS, or before newline
\z
Anchor
Match only at end of string
\G
Anchor
Match only where previous m//g left off



No comments :