Tuesday 10 December 2013

How to remove special characters from textbox using javascript


Sometime you might come into a situation to remove the specials characters in the textbox while users give the input. Below JavaScript sample code will help you to remove those char from textbox. The following script delete all the special character  on textbox "onkeyup" event.

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

    </div>
    </form>
</body>
</html>

No comments:

Post a Comment