user3138522
user3138522

Reputation: 111

keypress event for some characters in javascript

number:<input type="text" name="number" onkeypress='return numbersonly(event)' />

here is my textbox...i have apllied a function o it on keypress..here is the function...

<script type="text/javascript">
   function numbersonly(e){
     var unicode=e.charCode? e.charCode : e.keyCode
     if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
        if (unicode<48||unicode>57&&unicode!=65) //if not a number
           return false //disable key press
        }
     }
</script>

with this keypress function the user can only enter number that is from 0-9 now what i want is that it should acceept only one alphabet that is a whose ascii number is 65...but when i am apllying condition it is not taking a alphabet so can anyone please help me with the condition??

Upvotes: 0

Views: 166

Answers (1)

laaposto
laaposto

Reputation: 12213

+ sign unicode is 43 not 107

Try:

function numbersonly(e){
 var unicode=e.charCode? e.charCode : e.keyCode;
 if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
    if ((unicode<48||unicode>57)&&(unicode!=65)&&(unicode!=43)) //if not a number
    return false //disable key press
    }
      }

DEMO

Upvotes: 1

Related Questions