Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable number input in a text input with js

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;
                }
        }
    }
like image 477
Alex Avatar asked Oct 20 '25 04:10

Alex


2 Answers

You can use the new pattern attrib HTML ,

<input type="text" id="num" pattern="[A-Za-z]" title="Only Alphabets">
like image 177
Jayanth Ramachandran Avatar answered Oct 22 '25 19:10

Jayanth Ramachandran


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

like image 40
Vicky Gonsalves Avatar answered Oct 22 '25 18:10

Vicky Gonsalves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!