Wednesday, February 20, 2013

Allow Only Alphanumeric Characters in a ASP.NET TextBox using Jquery


.aspx Markup:

<div class="bigDiv">
<h2>Allow Only Alphanumeric Characters in a TextBox</h2>
<asp:TextBox ID="tb1" runat="server" class="alpha"
Text="" ToolTip="Try entering Non alphanumeric char"/>
<br />
Tip: Examples of some non-alphanumeric characters 
are ~!@~#$%^&*
</div>

Jquery Script:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#tb1").bind('keyup blur', function () {
if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}
});
});
</script>

Jquery script will allow only alphanumeric characters to be typed in the text box. if the user type any non-alphanumeric character it will be replaced with a empty string.

No comments:
Write comments
Recommended Posts × +