javascript validation to allow only numbers and alphabets

前端 未结 3 1196
执念已碎
执念已碎 2021-01-21 05:29

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 fin

3条回答
  •  一生所求
    2021-01-21 05:59

    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!

提交回复
热议问题