Textbox that Only Accepts Numbers (Integers and Decimals)

Last post 12-05-2008 10:29 AM by sync2. 4 replies.

Sort Posts:

  • Textbox that Only Accepts Numbers (Integers and Decimals)

    12-04-2008, 5:53 PM
    • Member
      point Member
    • sync2
    • Member since 12-04-2008, 5:48 PM
    • Posts 17

    Does anyone know the C# code to write validation on a textbox contained in a web application not windows without using ASP.NET controls (for e.g. regular validators or compare validators) just plain C# to accept only integers or decimals?

    I see many posts on window applications, but none on web.  This is probably perhaps because of the built in controls of ASP.NET has already, but I just want to code it.  I'm trying to incorporate this textbox into a SharePoint application, which is why I'm asking.  I do not have access to the ASP.NET toolbox.  Therefore, I have no choice but to code it.

     Any help would be great.

     

  • Re: Textbox that Only Accepts Numbers (Integers and Decimals)

    12-04-2008, 7:23 PM
    Answer
    • Member
      556 point Member
    • eikden
    • Member since 06-20-2006, 6:16 AM
    • Penang
    • Posts 101

    Hi,

    For my advice, you can use regular expressions to control it.

    Below are some references for you:-

    http://geekswithblogs.net/rahul/articles/49097.aspx

    http://www.thescarms.com/dotnet/IsNumeric.aspx

    http://www.codeproject.com/KB/books/0735616485.aspx

    Regards,
    Yeoh Eik Den
    Software Engineer
  • Re: Textbox that Only Accepts Numbers (Integers and Decimals)

    12-04-2008, 8:31 PM
    Answer
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    Hi,

    Check these previous threads below:

    http://forums.asp.net/p/1323787/2645052.aspx#2645052

    http://forums.asp.net/p/299157/299157.aspx#299157

    http://forums.asp.net/p/1203550/2100896.aspx#2100896

    http://forums.asp.net/p/1106036/1692258.aspx#1692258



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: Textbox that Only Accepts Numbers (Integers and Decimals)

    12-04-2008, 9:32 PM
    Answer
    • Member
      218 point Member
    • kumarpgp
    • Member since 11-25-2008, 2:05 AM
    • Posts 88

    try this code hope its usefull for u 

    <script type ="text/javascript">
    function blockNonNumbers(obj, e, allowDecimal, allowNegative)
    {
     var key;
     var isCtrl = false;
     var keychar;
     var reg;
     if(window.event)
     {
       key = e.keyCode;
       isCtrl = window.event.ctrlKey
     }
     else if(e.which)
     {
       key = e.which;
       isCtrl = e.ctrlKey;
     }
     if (isNaN(key)) return true;
     keychar = String.fromCharCode(key);
     // check for backspace or delete, or if Ctrl was pressed
     if (key == 8 || isCtrl)
     {
       return true;
     }
     reg = /\d/;
     var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
     var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
     return isFirstN || isFirstD || reg.test(keychar);

    }

    function extractNumber(obj, decimalPlaces, allowNegative)
    {
      var temp = obj.value;
      // avoid changing things if already formatted correctly
      var reg0Str = '[0-9]*';
      if (decimalPlaces > 0)
      {
        reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
      }
      else if (decimalPlaces < 0)
      {
         reg0Str += '\\.?[0-9]*';
      }
      reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
      reg0Str = reg0Str + '$';
      var reg0 = new RegExp(reg0Str);
      if (reg0.test(temp)) return true;
      // first replace all non numbers
      var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
      var reg1 = new RegExp(reg1Str, 'g');
      temp = temp.replace(reg1, '');
      if (allowNegative)
      {
       // replace extra negative
       var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
       var reg2 = /-/g;
       temp = temp.replace(reg2, '');
       if (hasNegative) temp = '-' + temp;
      }
      if (decimalPlaces != 0)
      {
        var reg3 = /\./g;
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null)
        {
          // keep only first occurrence of .
         //  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
         var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
         reg3Right = reg3Right.replace(reg3, '');
         reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
         temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
       }
     }
     obj.value = temp;

    }
    </script>

  • Re: Textbox that Only Accepts Numbers (Integers and Decimals)

    12-05-2008, 10:29 AM
    • Member
      point Member
    • sync2
    • Member since 12-04-2008, 5:48 PM
    • Posts 17

    Thanks for the links.  The second link helped me alot.  Thanks to everyone else also.  The javascript would probably have worked however, I don't know how to access the HTML page of a web part programmatically in SharePoint.  Therefore in this situation, I could not use it but, thanks for the code anyway.

     Again, thanks for everyones' input.

Page 1 of 1 (5 items)