Pages

Wednesday, April 29, 2009

Validate Numbers Using Regular Expression in ASP.NET

In this article i would like to show you a simple code to validate User input to accept only Numbers in Text Box control.

Most traditional approach is to OnLostFocus event of Text Box we call a Java Script to read all the characters on by one to validate the entered the input is Number or not. This is time consuming process. Instead if you are familiar with Regular Expression the it becomes your life easier.

So here i am going to give you simple example to validate numbers in text box. Lets take simple case where in i want to validate Salary.



What you need to do is add a RegularExpressionValidator by setting Control to validate to the text box which we want to validate and ValidationExpression is ^[0-9] and ErrorMessage is "Enter Valid Salary".
Here "^" this symbol is called "Not" So the experession anything which is not in the range of 0-9 display the error message.
So No more For Loop or While loop to do this simple task.....
Similar Articles:

No comments: