Introduction:
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.
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
|
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. |
|||
|
|||
9 comments :
Lorem Ipsum
becomes
LoremIpsum
after putting a special character...
spaces are removed...
i'm bad at regex...so cant solve myself...plz help
hey Suresh...
this is not working if you drag the special character into textbox....
check it out.
and provide proper code.
@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.....
can u explain it
Good Example
how to do this in C# sir...
not in Javascript.
Thanks for helping me solve the special characters problem.
its good but i need for if we use
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.
Note: Only a member of this blog may post a comment.