Home > Code > Javascript > How to remove special characters in the textbox using JavaScript?

How to remove special characters in the textbox using JavaScript?

by Thamil   on Jun 09, 2011   Category: Javascript   |  Views: 8656    |  Points: 25   |  Gold 


Sometime you might come into a situation to remove the specials characters in the textbox while users feed the input. Below JavaScript sample code will help you to remove those char from textbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Remove Special Characters in the Textbox</title>
<script language="javascript" type="text/javascript">
function RemoveSpecialChar(txtVal) {
if (txtVal.value != '' && txtVal.value.match(/^[\w ]+$/) == null)
{
txtVal.value = txtVal.value.replace(/[\W]/g, '');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" onkeyup="javascript:RemoveSpecialChar(this)" runat="server">
</asp:TextBox>
</div>
</form>
</body>
</html>





Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts