Click here to Skip to main content
15,741,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have one text box which is alone Alphanumeric and special character. But i should not allow Special character alone. How Can i restrict this in JavaScript?

What I have tried:

i should not allow Special character alone. How Can i restrict this in JavaScript
Posted
Updated 12-Sep-18 2:42am
v2
Comments
Mohibur Rashid 3-Sep-18 5:22am    
You are talking about validation. And your validation requirement is not clear. Try regular expression. A simple example, when you want to accept lowercase character only, the following javascript code will work
/^[a-z]+$/.test("abc");
-> true
/^[a-z]+$/.test("abC");
-> false

To understand the brief of regular expression, search google

1 solution

function blockSpecialKey(e)
{
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
 
Share this answer
 
Comments
Richard Deeming 13-Sep-18 15:33pm    
Read the question again. The OP is not asking how to block "special" characters!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900