Don't allow typing alphabetic characters in a <input type=number />

前端 未结 9 738
情话喂你
情话喂你 2020-12-30 01:13

I need to have a textbox, where, whenever typed inside should ONLY allow numbers [0-9]. I\'ve used type=\"number\" which definitely holds the client side valida

相关标签:
9条回答
  • 2020-12-30 02:01

    You will have to have your input box inside a form and with a submit button. The form validation happens at the submission of the form. See a demo here: http://jsfiddle.net/ds345/Q4muA/1/

    <form>Number:
        <input type="number" name="Number" value="10">
        <br>
        <input type="submit" value="Submit" />
    </form>
    
    0 讨论(0)
  • 2020-12-30 02:05

    Try this:-

     $("#txtAge").keydown(function (e) {
           if(e.key=='e' || e.key=='.' || e.key=='-')
            return false;
        });
    
    0 讨论(0)
  • 2020-12-30 02:09

    Browsers behave differently. In Chrome the following code:

    <input type="number" id="txt_number" maxlength="70" name="txt_name" placeholder="Number">
      <input type="submit">
    

    will work for you only if it is in a form and only after the user clicks the submit button.

    0 讨论(0)
提交回复
热议问题