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
4 Answers
Reset to default 9You 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 ...