Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

JavaScript - Remove Special Characters from Textbox

Feb 28, 2013
Introduction

Here I will explain how to use JavaScript to remove special characters from textbox entered by 
user. 

Description:
  
In previous articles I explained JavaScript restrict user to enter only numbers, Watermark textbox in JavaScript, JavaScript validations for phone, email and many articles relating to
JavaScript, jQuery, asp.net. Now I will explain how to use JavaScript to remove special characters from textbox entered by user.

To implement this we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Remove Special Characters from the Textbox using JavaScript</title>
<script language="javascript" type="text/javascript">
function RemoveSpecialChar(txtName) {
if (txtName.value != '' && txtName.value.match(/^[\w ]+$/) == null) {
txtName.value = txtName.value.replace(/[\W]/g, '');
}
}
</script>
</head>
<body>
<div>
<b>Enter Text:</b><input id="txtName"  type="text" onkeyup="javascript:RemoveSpecialChar(this)" />
</div>
</body>
</html>
Live Demo

For live demo check below example


Enter Text:

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

9 comments :

Anonymous said...

Lorem Ipsum
becomes
LoremIpsum
after putting a special character...
spaces are removed...
i'm bad at regex...so cant solve myself...plz help

Atul Jadhav said...

hey Suresh...
this is not working if you drag the special character into textbox....
check it out.
and provide proper code.

Suresh Dasari said...

@Atul Jadhav...

Code is working fine for demo purpose i written code like above and i didn't put any restrictions and it's working fine. If you want don't want to allow drag and drop options you need to disable such kind of options by writing custom code.....

Constructions in madurai said...

can u explain it

HANUMA KARTHI said...

Good Example

Constructions in madurai said...

how to do this in C# sir...
not in Javascript.

hermes said...

Thanks for helping me solve the special characters problem.

Anonymous said...

its good but i need for if we use

pradeep joshi said...

Dear sir,
Its working fine .I am getting a problem in my textbox text come from database and i want to change this text if i use special character then it removes all( backspace . ,) and combine whole text together.
please tell me how can i do this.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.