Special character not Allowed in textbox with javascript

19 Aug

I want to check whether the user has entered any special characters in a text box.

First Method (Function) if you insert special char with string then automatically remove special char.

Second Function which no allow to special char .if you insert then auto remove.



javascript Function

<script type=”text/javascript”>
function textBoxOnBlur(elementRef)
var checkValue = new String(elementRef.value);
var newValue = ”;

// 1<2,3>4&56789
for (var i=0; i<checkValue.length; i++)
var currentChar = checkValue.charAt(i);

if ( (currentChar != ‘<‘) && (currentChar != ‘,’) && (currentChar != ‘>’) && (currentChar != ‘&’) )
newValue += currentChar;

elementRef.value = newValue;

function valid(f) {
!(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,”):null;
// –>

html Code

<form id=”form1″ runat=”server”>
<asp:TextBox ID=”TextBox1″ runat=”server”  onblur=”textBoxOnBlur(this);”></asp:TextBox>
<input name=”mytext” type=”text” onkeyup=”valid(this)” onblur=”valid(this)”>


1 Comment

Posted by on August 19, 2011 in Java Script



One response to “Special character not Allowed in textbox with javascript

  1. LA36 Sports

    September 17, 2011 at 11:16 am

    Thats great that you talk about learner, ASP.Net/C# developer. I was searching for a log time some informations about learner, ASP.Net/C# developer.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: