Script to Remove or Check Special Charater from Javascript

To Remove or Check Special Charater from Javascript

Call  Javascript function on “onkeyup” event of TextBox

<asp:TextBox ID=”TextBox1″ runat=”server” onkeyup=”valid(this)” Width=”610px”></asp:TextBox>

Javascript :

<script type=”text/javascript”>

function valid(field) {
var txt = document.getElementById(‘TextBox1′).value;
var iChars = “!@$%^&*()+=-[]\\\’;,./{}|\”:<>?~_”;
for (var i = 0; i < txt.length; i++) {
if (iChars.indexOf(txt.charAt(i)) != -1) {
alert(“Your string has special characters. \nThese are not allowed.”);
document.getElementById(‘TextBox1’).value = txt.substring(0, txt.length – 1);
return false;
}
}

}
</script>

Above script check all given Special character which is in iChars and substring function will remove that entered character

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s