4

Below is my javascript code which allows only alphabets while entering if we enter numbers and some special characters it does not accept them while entering only this works fine but this javascript code should allow numbers,space also along with alphabets how can i do this can you please help me out

<script language="Javascript" type="text/javascript">

         function onlyAlphabets(e, t) {
             try {
                 if (window.event) {
                     var charCode = window.event.keyCode;
                 }
                 else if (e) {
                     var charCode = e.which;
                 }
                 else { return true; }
                 if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode == 32)
                     return true;
                 else
                     return false;
             }
             catch (err) {
                 alert(err.Description);
             }
         }

    </script> 
<asp:TextBox ID="txtname" runat="server" onkeypress="return onlyAlphabets(event,this);"></asp:TextBox>
  • It would probably be easier to exclude what you don't want instead of trying to include everything that you do want. – wot Jun 07 '16 at 14:16

3 Answers3

5

I'd probably do something along the lines of what is described here, while adding in the desired space inclusion: Best way to alphanumeric check in Javascript

For example, check out this snippet:

function allowAlphaNumericSpace(e) {
  var code = ('charCode' in e) ? e.charCode : e.keyCode;
  if (!(code == 32) && // space
    !(code > 47 && code < 58) && // numeric (0-9)
    !(code > 64 && code < 91) && // upper alpha (A-Z)
    !(code > 96 && code < 123)) { // lower alpha (a-z)
    e.preventDefault();
  }
}
<input type="text" onkeypress="allowAlphaNumericSpace(event)" />

EDIT: Based on your comment, my quick and dirty solution is as follows, though I'm sure a better option must exist... Make note of the switch from "onkeypress" to "onkeyup" to ensure that the value is updated after the new character is inserted.

function allowAlphaNumericSpace(thisInput) {
  thisInput.value = thisInput.value.split(/[^a-zA-Z0-9 ]/).join('');
}
<input type="text" onkeyup="allowAlphaNumericSpace(this)" />
Community
  • 1
  • 1
Seth M.
  • 156
  • 5
  • but it is accepting special characters along with numbers aplhabets i need only space but not other spl characters –  Jun 08 '16 at 05:26
  • @kavithavemula I converted the code into a snippet which seems to be working as expected. Which special characters are being allowed? – Seth M. Jun 08 '16 at 12:18
  • function onlyAlphabets(e, t) { var regexp = new RegExp(/^[a-zA-Z0-9 ]*$/); if (window.event) { keynum = e.keyCode; } else if (e.which) { keynum = e.which; } var test = regexp.test(String.fromCharCode(keynum)); return test; } the above provided code works fine for me in accepting only numbers characters and spaces but if i am copying and pasting the special characters it is acceptingbut it should not accept even copy pasting the characters other than those three how can i do this –  Jun 09 '16 at 15:34
  • @kavithavemula I put a quick solution option into my edit above, feel free to see if it helps you with your issue. – Seth M. Jun 09 '16 at 18:11
  • yes while entering it is getting displayed but it is not accepting,can we do without displaying without accepting while entering instead of accepting and getting removed –  Jun 10 '16 at 04:39
  • function allowAlphaNumericSpace(thisInput) { thisInput.value = thisInput.value.split(/[^a-zA-Z0-9 ]/).join(''); } this code accepts numbers,characters and spaces similar to this way i need code for accepting only numbers and characters with spaces how can i do this –  Jun 10 '16 at 04:42
  • Well, the reason it was converted from a "do not accept these characters at all" to a "remove these characters if they are here" approach is to handle the paste action. If you don't want the characters to ever appear, you'll likely need to prevent pasting in the field. – Seth M. Jun 10 '16 at 11:59
4

You can use regex to test your input , with something like this :

var regexp = new RegExp(/^[a-zA-Z0-9 ]+$/);
var str="sqdqsd qsd125";
var str2="sqdqsdqsd12;5";
console.log(regexp.test(str));
console.log(regexp.test(str2));

https://jsfiddle.net/Tintin37/7Lj7r9zp/1/

EDIT

I added a little snippet ;)

 function onlyAlphabets(e, t) {
   var regexp = new RegExp(/^[a-zA-Z0-9 ]*$/);
   console.log(regexp.test(t.value));
   return regexp.test(t.value);
 }
<input type="text" onkeyup="return onlyAlphabets(event,this);" />

EDIT2

With the desired behavior ;)

 function onlyAlphabets(e, t) {
   var regexp = new RegExp(/^[a-zA-Z0-9 ]*$/);
   if (window.event) {
     keynum = e.keyCode;
   } else if (e.which) {
     keynum = e.which;
   }
   var test = regexp.test(String.fromCharCode(keynum));
   return test;

 }
<input type="text" onkeypress="return onlyAlphabets(event,this);" />
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
0

The reason your code wont accept numbers is because numbers have their own character code as well. The character code for 1, for example, is 48. To include numbers, you should include this in your if statement:

(charCode >= 48 && charcode <= 57)

Not sure why spaces aren't working, but see if the above helps!

sahilkmr78
  • 164
  • 9
  • i need a javascript validation that allows only numbers characters while entering into text box can you please help me out textbox should not accept other than numbers and characters how can i do this –  Jun 08 '16 at 04:42
  • Adding this to your code should allow characters (which your code already did) and numbers with the part that I wrote. – sahilkmr78 Jun 08 '16 at 13:37