Restrict Special Characters in TextBox using JavaScript except .,

Friends,

I had used the following code for restrict special characters in Text Box. It blocks all the characters but I need .(fullstop) and ,(Comma) characters. Please help me. Please find the coding as follow;

function blockSpecialChar(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));
}

Thanks & Regards,

Guna

function blockSpecialChar(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) || k == 190 || k == 188);
}

Boss,

I don’t want to block .(fullstop) and ,(comma) characters.

Thanks & Regards,

It shouldn’t block according to your code. And i added the 2 key codes to allow a period and comma to go through which is keycode 190 and 188.

according to your code its saying allow key codes 65 - 90 and 97 - 122 so and etc…

keycodes 65-90 is [a-z]
keycodes 48 - 57 is [0-9]

I just added to ignore the keycodes you requested if you are having problems let me know and we can get them fixed.

Thanks for your reply. I got your point. The keycode which you added block period and comma also.

I am using a Text Box and users need to enter their remarks. Sometime they added ’ symbol so system didn’t allow to save the data. Then only, I had searched and found the said code. This is my need. The user need to feed period and comma and no other characters they don’t need.

Please help me to fix this issue.

Thanks & Regards,

IMHO you would be better off scrapping the whole keycode bit.

It depends on a keyboard event (won’t deal with copy-paste), is complicated, and is on the way out.

Deprecated
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Of course JavaScript will not prevent determined users from submitting characters you don’t want, that needs to be done server-side if it’s important, but a JavaScript regex might be good enough for your needs.

Thanks Friends…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.