Numeric Entry Only Tex Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

We would like to use a validator to valid that only numeric data is allowed
in a text box. Is there a way to do this? If so, how?
 
<asp:textbox

runat="server" OnKeyPress="ValidateNumeric()"/>

<script language="Javascript">

function ValidateNumeric()

{

var keyCode = window.event.keyCode;

if (keyCode > 57 || keyCode < 48)

window.event.returnValue = false;

}

</script>
 
You can use the range validator from ASP.NET. If you wanted to go the
route of JavaScript, you will have to use a regular expression.
Certain functions like parseInt() will still pick up trailing alpha
characters such as "123a" will result in 123. I would suggest using
the following to return True or False;

function checkNum(val) {
return (/^[0-9]+$/.test(val));
}

This will allow 1 or more numbers, but will not allow decimals. It can
be modified to do such a thing.
Thanks,
Ian Suttle
http://www.IanSuttle.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top