Solution: How to remove special characters from textbox using JavaScript (onchange,onkeyup) | Coding Cluster - using, c#, mvc 4, iphone, php, ios, javascript, in mvc 3 & more

Solution: How to remove special characters from textbox using JavaScript (onchange,onkeyup)


 Client side validation: Remove special characters from textbox before save:
                                                         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.

Source code:
<html xmlns="" >
<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, '');
    <form id="form1" >
        <input id="txtName"  type="text" onkeyup="javascript:deleteSpecialChar(this)" />

Demo:Remove Special Characters from the Textbox - Coding cluster
Enter some text with special characters

That's all. If this post is useful for you , please share this knowledge to your friends. Thanks!


Waleed Barakat said...

Thank you, this is very good and helpful.

Waleed Barakat said...

I have modified the script a little bit to be able to add some characters and line break if i used the script in textarea Also if you're an Arabic language user you can use it too.

1- Adding
to textarea.
2- Enabling Arabic language characters.
3 Adding some more special chars like comma and dash

Here is the modification:
if (txtName.value != '' && txtName.value.match(/^[\w ء-ي-,.\n]+$/) == null)

Anonymous said...

I need the code like i can enter accept special char only dot(.) symbol can u reply me as soon as poosible

Post a Comment

Share your thoughts here...




Blogger Widgets