Click here to Skip to main content
11,437,713 members (51,324 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 ASP.NET
validation at codebehind C# allowed only numeric values & after decimal allowed only 2 digits and not allowed alpha numeric value in textbox
Posted 21-Mar-12 4:50am
Comments
ryanb31 at 21-Mar-12 9:53am
   
Are you asking a question? Have you tried using the built in Validator controls?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

put this in to your head scipt tag.
this might help you....
var RegularExpression  =  new RegExp(^\d+(\.\d{1,2})?$);
if (RegularExpression.match("your_number_string"))
{
 alert("Valid Number");
} else
{
  alert("INVALID Number");
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use a RegEx or RegularExpressionValidator control
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this

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;
        }
  Permalink  
Comments
Mark Nischalke at 21-Mar-12 13:06pm
   
A coding horror nominee
shreekar at 21-Mar-12 15:52pm
   
This is a fail as it does not solve OP's problem. Besides, Regular expressions are the preferred way.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150506.1 | Last Updated 21 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100