Reputation: 1
I want to avoid entering hypen to be entered in text box. The text box field is for a phone number which is in US format, therefore i am adding -(dash) after 3rd and 6th digit through JQuery which is automatically adding as user enters, but i didn't want user to enter -(dash) manually through keyboard.
$("#someID").keydown(function (e){
if(e.keyCode==189){
e.preventDefault();
}else{
var myLength = $(this).val().length;
var text1=$(this).val();
if((myLength==3) ){
$(this).val($(this).val()+'-');
}else if(myLength==7){
$(this).val($(this).val()+'-');
}
}
});
189 is keycode for -(dash/hyphen)
Upvotes: 0
Views: 158
Reputation: 144729
jQuery normalizes which
property and you can use this property instead of keyCode
:
if (e.which === 189) {
You can also consider using Masked Input plugin.
Upvotes: 2
Reputation: 74738
just change to this:
if(e.keyCode==109){
//---------^^^------109 is the keycode for hyphen
you can check here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Upvotes: 0