最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable number input in a text input with js - Stack Overflow

programmeradmin4浏览0评论

What I have to do is not permit to introduce number characters in a text input in a form, but I've to do it with javascript. Here's my HTML code:

<form id="formu">
<input type="text" id="num" />
</form>

And here's my JS code:

function numero(theEvent){

    var evento=theEvent||window.event;

        switch(evento.type){
            case 'keypress':
                if(document.getElementById("num").evento.keyCode>=48&&document.getElementById("num").evento.keyCode<=57){
                    return false;
                }
        }
    }

What I have to do is not permit to introduce number characters in a text input in a form, but I've to do it with javascript. Here's my HTML code:

<form id="formu">
<input type="text" id="num" />
</form>

And here's my JS code:

function numero(theEvent){

    var evento=theEvent||window.event;

        switch(evento.type){
            case 'keypress':
                if(document.getElementById("num").evento.keyCode>=48&&document.getElementById("num").evento.keyCode<=57){
                    return false;
                }
        }
    }
Share Improve this question edited Nov 15, 2013 at 9:15 super 2,3282 gold badges21 silver badges23 bronze badges asked Nov 15, 2013 at 9:11 AlexAlex 7805 gold badges15 silver badges35 bronze badges 1
  • where did u call numero(theEvent) ? – Vicky Gonsalves Commented Nov 15, 2013 at 9:14
Add a ment  | 

4 Answers 4

Reset to default 9

You can use the new pattern attrib HTML ,

<input type="text" id="num" pattern="[A-Za-z]" title="Only Alphabets">
function validateKeyStrokes(event) {
    var charCode = (event.which) ? event.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}


<form id="formu">
<input type="text" id="num"  onkeypress="return validateKeyStrokes(event)" />
</form>

Try this

Try using jQuery and a plugin for input text field filtering

See here

You can also use the HTML5 new input type number :

<input type="number" name="num" id="num">

But use with care if you want to provide old browsers support ...

发布评论

评论列表(0)

  1. 暂无评论