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 comment