Home > Code > Javascript > Restrict the characters in the textbox using JavaScript (Allow number only)

Restrict the characters in the textbox using JavaScript (Allow number only)

by Thamil   on Nov 14, 2010   Category: Javascript   |  Views: 3704    |  Points: 25   |  Gold 


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Valid Numbers</title>

<script type="text/javascript">
function ValidNumbers()
{
var exp = String.fromCharCode(window.event.keyCode)
var r = new RegExp("[,0-9\r]", "g");
if (exp.match(r) == null)
{
window.event.keyCode = 0
return false;
}
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" MaxLength="10" onKeyPress="ValidNumbers();" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>




Post Code  |  Code Snippet Home

User Responses


  Re :Restrict the characters in the textbox using JavaScript (Allow number only)   
Posted by Spidy
on 10/03/2011
Points : 5

useful code when we work with input textbox like pincode, mobile#, credit card no etc,.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts