There's something out there in life that will delight and amaze you.
Get up, get moving, and go find out what it is. - (Ralph Marston)

Friday, October 27, 2006

Validating a decimal number of specified integers and factional points; using a regular expression.


It’s often the case where we stick to a specified number of integer and fractional number in decimal numbers. But some times the requirement arises to validate the decimal numbers which we specify the number of integers and fractions in it.

Here is a solution to such a problem.

Following is a regular expression I wrote will validate 0 to i number of integers and 0 to f number of fractional points.

string sTest = @"^\d{0," + i + @"}\.\d{0," + f + "}$";

string i = iNumericLength.ToString();
string f = iDecimalPoints.ToString();
 
string sTest = @"^\d{0," + i + @"}\.\d{0," + f + "}$";
 
Regex ex = new Regex(sTest);
string st = txtEnteredValue.Text;
 
if (!ex.IsMatch(st))
{
MessageBox.Show("This is a decimal field which supports max. of " + i + " numbers and max. of " + f+ " decimal numbers.");
}

0 Comments:

Post a Comment

<< Home