How do you restrict textboxes to only numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zeroblade
    New Member
    • Oct 2007
    • 8

    How do you restrict textboxes to only numbers?

    For a textbox I want the user to only enter in a number from 1 to 10(including numbers like 4.5 and 6.6 and so forth). How would I do this?
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    You will need a Server-side script or Programing language for this. HTML Can limit the number of characters entered by using the maxlength attribute. But it can not distinguish the difference between a number or letter.

    --Kevin

    Comment

    • media3000anthony
      New Member
      • Mar 2008
      • 4

      #3
      Using javascript, here is a start:

      [HTML]<input type='text' name='num' onkeyup="this.v alue=this.value .replace(/[^0-9\.]/g, '');" />[/HTML]

      I tried code to stop the user entering more than 1 decimal place, or more than 10, but it made it too aggressive.

      Would you like me to write more code, to restrict to: X or 10 or X.X, and append just a warning message after the box ?

      Regards,
      Anthony

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        Moving to JavaScript Forum

        Comment

        • mrhoo
          Contributor
          • Jun 2006
          • 428

          #5
          var Rx= /^(10|([1-9](\.\d+)?))$/;

          This expression matches either '10',
          or a string whose first character(^) must be an integer greater than zero [1-9].
          This first digit may be followed by a decimal point (\.) followed by 1 or more digits between 0 and 9 (\d+).
          The $ flags the end of the string, prohibiting any invalid characters following a match.

          For example-
          [CODE=javascript]
          function validator(field ,rx){
          var val= field.value;
          if(rx.test(val) ) return val;
          field.value= '';
          throw '(Invalid input in '+field.name+') \n'+field.title ;
          }[/CODE]

          // fake an input for this test
          var inputfield= {name:'oneten', value:'',title: 'Type a number between 1 and 10'};
          var Rx= /^(10|([1-9](\.\d+)?))$/;
          inputfield.valu e='10.12';

          alert(validator (inputfield,Rx) )

          Comment

          • malav123
            New Member
            • Feb 2008
            • 217

            #6
            HI,
            If you want the user to allow for only numeric input than you can use following function.....
            [CODE=javascript]function onlyDigit(e)
            {
            var unicode=e.charC ode? e.charCode : e.keyCode
            if (unicode!=8 && unicode!=9 && unicode!=46 && unicode!=39 && unicode!=37)
            {
            if((unicode<48| |unicode>57) && (unicode<96||un icode>105))
            return false
            }
            }[/CODE]
            Last edited by gits; Mar 31 '08, 09:24 AM. Reason: remember to use CODE TAGS!!!

            Comment

            Working...