Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
validation at codebehind C# allowed only numeric values & after decimal allowed only 2 digits and not allowed alpha numeric value in textbox
Posted
Comments
ZurdoDev 21-Mar-12 9:53am    
Are you asking a question? Have you tried using the built in Validator controls?

One alternative to validations is a masked text box. You can find some for ASP.NET as well.

Please see:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx[^],
http://www.asp.net/community/control-gallery/Item.aspx?i=387[^].

For the pure JavaScript solution with jQuery, please see this discussion:
http://stackoverflow.com/questions/480979/jquery-masked-input-plugin-select-all-content-when-textbox-receives-focus[^].

A couple of jQuery plug-ins you can try:
https://github.com/RobinHerbots/jquery.inputmask[^],
http://www.meiocodigo.com/projects/meiomask/[^].

—SA
 
Share this answer
 
put this in to your head scipt tag.
this might help you....
JavaScript
var RegularExpression  =  new RegExp(^\d+(\.\d{1,2})?$);
if (RegularExpression.match("your_number_string"))
{
 alert("Valid Number");
} else
{
  alert("INVALID Number");
}
 
Share this answer
 
v2
Use a RegEx or RegularExpressionValidator control
 
Share this answer
 
try this

C#
public static bool IsDesimal(string strValue)
        {
            string decimalNumber = ".0123456789";
            for (int i = 0; i < strValue.Trim().Length; i++)
            {
                if (decimalNumber.IndexOf(strValue[i]) == -1)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Returns the Validate Input Numeric Value
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns>Numeric Value</returns>
        public static bool IsNumeric(string strValue)
        {
            string decimalNumber = "0123456789";
            for (int i = 0; i < strValue.Trim().Length; i++)
            {
                if (decimalNumber.IndexOf(strValue[i]) == -1)
                {
                    return false;
                }
            }

            return true;
        }
 
Share this answer
 
Comments
[no name] 21-Mar-12 13:06pm    
A coding horror nominee
shreekar 21-Mar-12 15:52pm    
This is a fail as it does not solve OP's problem. Besides, Regular expressions are the preferred way.
You can find the RegEx expression for your requirement done in the link below.

http://gskinner.com/RegExr/?2rj2e[^]

Please try it and let me know if it works for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900