RSS

Tag Archives: special char not insert into textbox validation

Special character not Allowed in textbox with javascript


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;
}
// –>
</script>

html Code

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

</div>
</form>

 
1 Comment

Posted by on August 19, 2011 in Java Script

 

Tags: