Nida Amin
Nida Amin

Reputation: 755

How to allow all the characters except numbers?

I want to allow all the character except numbers. I have written below jquery.

$(".no-numbers").keypress(function(e){
  if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) 
  {
    swal({
    title: "",
    text: "Numbers are not allowed"
    });

    return false;
  }
  else
  {
    return true;
  }
}); 

The above code is not accepting anything... Please help!!!

Upvotes: 2

Views: 835

Answers (4)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

Your conditiion was wrong.

You have to test for keycode from 48 to 57. And also for the keypad numbers, which are 96 to 105.

Your original condition explained:

(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) 

If the key is not backspace (8)
AND
if the key is not... There is no keycode zero!!
AND
if the key is below keycode 48 OR above keycode 57 --> That's the opposite of what you wish!


Look at the below snippet now ;)

$(".no-numbers").keydown(function(e){
  if ( (e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105) ){
    swal({
      title: "",
      text: "Numbers are not allowed"
    });

    return false;
  }
  else
  {
    return true;
  }
}); 
<link href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.28.5/sweetalert2.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.28.5/sweetalert2.min.js"></script>

<input class="no-numbers">

Side note... I always tend to test and "prevent" a keyboard press using the keydown event, which fires first.

Upvotes: 1

Guillermo Quiros
Guillermo Quiros

Reputation: 441

Looks like your if statement is wrong

Take a look at this sandbox https://codesandbox.io/s/91wrqorl7o

if (e.which >= 48 && e.which <= 57) {
  console.log("Not accepted is a  Numbers");
  return false;
} else {
  console.log("Accepted not a number");
  return true;
}

Upvotes: 1

Rahul Dudharejiya
Rahul Dudharejiya

Reputation: 375

Try the following:

$(".no-numbers").keypress(function(e){
  var key = e.keyCode;
  if (key >= 48 && key <= 57) {
    swal({
    title: "",
    text: "Numbers are not allowed"
    });

    return false;
  }
  else
  {
    return true;
  }
}); 

Or you can use regular expression like:

var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
  // do your stuff here
}

Upvotes: 2

Jerdine Sabio
Jerdine Sabio

Reputation: 6140

Reference How to find out what character key is pressed?;

You could convert the keypress to its actual value then use e.preventDefault() to disregard the input.

$(".no-numbers").keypress(function(e) {
  var numbers = ['0','1','2','3','4','5','6','7','8','9'];
  var keynum;

  if (window.event) { // IE                    
    keynum = e.keyCode;
  } else if (e.which) { // Netscape/Firefox/Opera                   
    keynum = e.which;
  }

  if (numbers.indexOf(String.fromCharCode(keynum)) > -1) {
    alert("No numbers please.");
    e.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="no-numbers">

Upvotes: 1

Related Questions