29 January 2015

How to validate double value in c#.net


 Add namespace: using System.Text.RegularExpressions;



if (Regex.IsMatch(txt_number.Text, @"\d+(\.\d{1,2})?"))
{
    //valid Number
    // Do something with value
}
else
{
    //invalid Number
    lbl_error.Text = "Please enter a valid number";
    return;
}

Int value validation in c#.net

            int parsedValue;
            if (int.TryParse(txt_number.Text, out parsedValue))
            {

                //valid Number
                // Do something with value
            }
            else
            {
                //invalid Number
                lbl_error.Text = "Please enter a valid number";
                return;
            }

Decimal number validation in c#.net

           decimal d;
            if (decimal.TryParse(txt_number.Text, out d))
            {
                //valid Number
                // Do something with value
            }
            else
            {
                //invalid Number
                                lbl_error.Text = "Please enter a valid number";
                return;
            }