only My site

Thursday, December 16, 2010

ASP.NET Regular expression

word with -
[\w\s\-]*

Negative and positive decimal number with 2 digits
^(-)?\d+(\.\d\d)$

Negative integer
^(-)?\d+$

Alpha numeric with space
[\W\s]*

negative integer with decimal point as optional
^(-)?\d+([.]\d+)*

C# Code
string name = Request.Form["txtName"];
if (name.Length > 0)
{
if (System.Text.RegularExpressions.Regex.IsMatch(name,
"^[a-zA-Z'.]{1,40}$"))
Response.Write("Valid name");
else
Response.Write("Invalid name");
}

No comments: