tags:

views:

58

answers:

1

I have found a Regex that test if the text passed to a TextBox is an email.

If Regex.IsMatch(email.Text, "^(?("")("".+?""@)|(([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}))$") _
Then
// True
End If

I want to change this, so it will test if the text typed is just Numbers ?

How can I do that ?

+1  A: 

If you want to make sure the text contains only digits use a simple ^\d+$ or ^\s*\d+\s*$ to allow some spaces at the beginning and end.

To allow negative numbers: ^-?\d+$ or ^[+-]?\d+$ to allow numbers like +12

For decimal numbers: ^[+-]?\d+(\.\d+)?$ (this will allow 0.54 but not .54)

This one will allow things like .54

^[+-]?(\d+(\.\d+)?|\.\d+)$
Amarghosh