Numeric Entry Only Tex Box

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?
 
S

Steve C. Orr [MVP, MCSD]

<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>
 
I

Ian Suttle

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
 
P

Peter Blum

OMG, that's a lot of solutions when you simply wanted to use the
CompareValidator. Set its Operator=DataTypeCheck and Type=Integer.

Even if you created a textbox that filters keystrokes, you must provide a
validator because users (and especially hackers) disable javascript on their
browser. The validator confirms the entry on the server side.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top