12

I have this JavaScript function that is used to force user only type number in the textbox. Right now and I want to modify this function so it will allow the user to enter plus (+) symbol. How to achieve this?

//To only enable digit in the user input

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
Kara
  • 6,115
  • 16
  • 50
  • 57
cyberfly
  • 5,568
  • 8
  • 50
  • 67
  • 2
    Greetings cyberfly All answers are fine but be warned QC will return an issue to you that if you implement this function they will tell you i cant copy and paste in this textbox or even cut & paste or even select all so also you have to enable the user to use the following keys Ctrl + A , Ctrl + C , Ctrl + V , Ctrl + A – Marwan Apr 04 '11 at 07:27
  • Don't use charcode, use a regular expression and the value. Preventing input really annoys users, let them enter whatever and validate on submit. What about pasting from the context or Edit menus? – RobG Apr 04 '11 at 08:11

8 Answers8

15

Since the '+' symbol's decimal ASCII code is 43, you can add it to your condition.

for example :

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode != 43 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

This way, the Plus symbol is allowed.

dominicbri7
  • 2,479
  • 4
  • 23
  • 33
  • thanks it simple and works. i have tried something like your solution before but the char code i am referring is 107. http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx – cyberfly Apr 04 '11 at 07:29
7

This code might work. I added support for SHIFT + (equal sign) and the numpad +.

function isNumberKey(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode;
  var shiftPressed = (window.Event) ? e.modifiers & Event.SHIFT_MASK : e.shiftKey;

  if ((shiftPressed && charCode == 187) || (charCode == 107))
  {
    return true;
  } else if ((charCode > 95) && (charCode < 106)) {
    return true;
  } else if (charCode > 31 && (charCode < 48 || charCode > 57))) {
    return false;
  } else {
    return true;
  }
}
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 1
    `(shiftPressed && charCode == 187)`. `187` is the keyCode of the `=` key, since `shift + =` = `+`. I'm a bit confused about what you're asking... – Blender Apr 04 '11 at 07:15
  • I tried this but only Plus key is working and `Shift + =` key is returning false.. also `e` is not defined in `e.modifiers` and `e.shiftKey` – sohaiby Dec 22 '15 at 10:21
3

It's Work. Javascript keycode allow number and plus symbol only

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation</title>
</head>
<body>

<form name="form1" action="#">

Mobile Number:&nbsp;&nbsp;<input type='text'  id='PhoneNumber' maxlength="10" onKeyPress="return IsNumeric3(event);" ondrop="return false;" onpaste="return false;"/>
<span id="error3" style="color: Red; display: none">* Input digits (0 - 9)</span>

</form>
 <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8); 
  specialKeys.push(43); 
  specialKeys.push(37); 
  specialKeys.push(39); 
  //Backspace
        function IsNumeric3(e) {
            var keyCode = e.which ? e.which : e.keyCode
            var ret = (keyCode != 37 && keyCode != 8 && keyCode != 46 && (keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            document.getElementById("error3").style.display = ret ? "none" : "inline";
            return ret;
        }
    </script>

 <script>
function stringlength(inputtxt, minlength, maxlength)
{ 
var field = inputtxt.value; 
var mnlen = minlength;
var mxlen = maxlength;

if(field.length<mnlen || field.length> mxlen)
{ 
alert("Please input the 10 digit mobile number");
return false;
}
else
{ 

return true;
}
}
</script>
</body>
</html>

Thank you friends

Artem Kulikov
  • 2,250
  • 19
  • 32
manoj
  • 109
  • 1
  • 7
3

this is stupid ... not really an answer at all. I would suggest you to do following.

function isNumberKey(evt)
{
    console.log(evt.keyCode);
    return false;
}

And find out the ranges of all keys, and implement it.

S L
  • 14,262
  • 17
  • 77
  • 116
1

Here is the modified code:

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if ( (charCode >= 48  && charCode <= 57) || charCode == 43) 
        return true;
    return false;
}
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
0

Here is the code . working fine with numbers and plus + sign in phone fields. you will have to implement the code on keydown function . target the id/class of the particular phone field and use keydown function.

    //allows only these keys
    // backspace, delete, tab, escape, and enter
    if ( event.keyCode == 107 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
         // Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) || 
         // home, end, left, right
        (event.keyCode >= 35 && event.keyCode <= 39)) {
             return;
    }
    else {
        // Ensure that it is a number and stop the keypress
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
            event.preventDefault(); 
        }   
    }
0

Using experience of my colleagues above, write one function, that fits me well. It filters all except numbers, arrows and backspace. Maybe it would be useful for somebody.

function isKeyCorrect(keyEvent) {
    var charCode = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
    var isNotNumber = charCode < 48 || charCode > 57;
    var isNotArrow = charCode < 37 || charCode > 40;
    var isNotBackspace = charCode !== 8;

    return isNotNumber && isNotArrow && isNotBackspace;
}
chakzefir
  • 138
  • 1
  • 9
-3
<script type="text/javascript">
 $(document).ready(function() {
    `enter code here`  $('#form-1').submit(function(msg) {  
        $.post("action.php?act=login",$(this).serialize(),function(data){      
            if (data == 'ERR:A3001004') { alert("Güvenlik hatası, sayfayı yenileyin."); }
            else if (data == 'TIMEEND') { alert("Anahtarınızın süresi dolmuş.");    }
            else if (data == 'INVALID') { alert("Geçersiz anahtar şifresi girdiniz.");  }
            else if (data == 'OK') { alert("Başarıyla giriş yaptınız. Yetişkinlere göre içerik barındıran sitelere erişim sağlayabilirsiniz."); }
        });

        return false; 
    });
});
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
</script>