numeric fields have a Regular Expression Validation control to make sure the input value is numeric.
asp:RegularExpressionValidator, if you enter other 0-9 , it will throw error!
<input type=text runat=server id=txtNo>
<asp:RegularExpressionValidator runat=server
ControlToValidate="txtNo"
ErrorMessage="Enter numbers alone."
ValidationExpression="^[0-9]$" />
or else use keydown event if its a windows app,
private void textBoxNumber_KeyDown(object sender, KeyEventArgs e )
{ if (Char.IsDigit(e.KeyChar) == true )
{
}
else
{
e.SuppressKeyPress = true;
}
}
or else,
write this code in ur form load event in .cs file
TextBox1.Attributes.Add("onkeypress","return CheckNemericValue(event);");//here textbox1 is ur server side control
then wite this javascript funciton in html
function CheckNemericValue(e)
{
var key;
key = e.which ? e.which : e.keyCode;
if(key>=48 && key<=57)
{
return true;
}
else
{
alert("please enter number only");
return false;
}
}
hope this will solve ur problem
just one more option,
if( document.getElementById('txtbox1') != null )
{
if( document.getElementById('txtbox1').value != "" )
{
var strValue = document.getElementById('txtbox1').value;
var objRegExp =/(^-?\d\d*$)/;
if( ! objRegExp.test(strValue) )
{
strMsg += "\n- Value must be numeric";
if( !errControlSet )
{
errControl = document.getElementById('txtbox1');
errControlSet = true;
}
result = false;
}
}
}