简体   繁体   中英

javascript validation to allow only numbers and alphabets

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>

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);" /> 

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 (AZ) !(code > 96 && code < 123)) { // lower alpha (az) 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)" /> 

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM