Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set textbox to allow only numbers 2

Status
Not open for further replies.

Vandy02

Programmer
Jan 7, 2003
151
0
0
US
How do you make a textbox entry only allow numbers....?

Thanks
 
easiest way is to add a validation control but there afew more options for you to choose from

VB.NET Client side validation
Code:
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 176px"
				runat="server" OnKeyPress="IsNum()"></asp:TextBox>
 <script language="Javascript">

  function IsNum()

  {

    var keyCode = window.event.keyCode;

    if (keyCode > 57 || keyCode < 48)

      window.event.returnValue = false;

  }

 </script>
C# Server side
Code:
private void onKeyPress(object sender, KeyPressEventArgs e)

{

if (!Char.IsDigit(e.KeyChar))

e.Handled=true;

}

What would you attempt to accomplish if you knew you would not fail?
 
You can use CustomValidator and JavaScript to do
the job, here is the solution:

1. Add a CustomValidator control.
2. Set ErrorMessage property of the CustomValidator
to "numerical values only!"
3. Set the ClientValidationFunction property to
IsNumber. IsNumber is a JavaScript function.

<script>
function IsNumber(source,arguments)
{
var input;
var ValidNumbers = "0123456789.";

var value=document.getElementById("SomeTextBox").value;
for (i = 0; i < value.length; i++)
{
input = value.charAt(i);
if (ValidNumbers.indexOf(input) == -1)
{
arguments.IsValid = false;
return;
}
}
return;
}
</script>


- The Ultimate Resource for IT Professional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top