Hi friends, today i am going show you how to Restrict Special Characters in TextBox or in Textarea using JavaScript in asp.net.
I have two method to do it in javascript.
//Restrict HTML Character
The script works in such a way that the TextBox will accept only alphabets, numbers i.e. alphanumeric values with some allowed special keys, thus unless a special character key has been specified to be excluded it won’t be accepted.
<div align="center" style="width: 100%;">
<script type="text/javascript">
Screen 1 |
First Method:
In this method you only want to add "RestrictHtmlCharacter-Tags.js" file in order to restrict special character in textbox.
Using this method you don't need to add anything to textbox. Only you have to do one thing only call that "RestrictHtmlCharacter-Tags.js" js file in your page and it will restrict all the textbox entering special character. For this you have to add jquery 1.7.2.min.js
Click here to download RestrictHtmlCharacter-Tags.js
If you have update panel on your page please add following initializing code to initialize the "RestrictHtmlCharacter-Tags.js".
//Restrict HTML Character
$(document).ready(function() {
try {
RestrictHtmlCharacter(); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RestrictHtmlCharacter);
}
catch (err) {
}
});
Add this following file in head tag:
<script src="Script/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="Script/RestrictHtmlCharacter-Tags.js" type="text/javascript"></script>
Using this way you don't need too add anything in textbox only add this javascript file.
Second Method:
The second method to do the same is following:The script works in such a way that the TextBox will accept only alphabets, numbers i.e. alphanumeric values with some allowed special keys, thus unless a special character key has been specified to be excluded it won’t be accepted.