Hello
I want to allow only numeric field to be entred into textbox. How can I do that in C#.Net2,ASP.Net2 or Javascript.
thanks
How can I allow only numeric value into textbox field in C#.Net and ASP,Net 2?
%26lt;script language="javascript"%26gt;
function maskBox(e)
{
var keynum = null;
if(window.event)
{
keynum = e.keyCode;
}
else if(e.which)
{
keynum = e.which;
}
if(keynum)
{
if(keynum==8 || keynum==9)
{
return true;
}
else
{
return isInt(String _
.fromCharCode(keynum));
}
}
}
function isInt(test)
{
var ints = "0123456789";
if(ints.indexOf(test)%26gt;-1)
{
return true;
}
return false;
}
%26lt;/script%26gt;
In your code, simply write:
txtTextBoxId.Attributes _
.Add("onKeyDown", _
"return maskBox(event);")
This will work in IE, FF, Opera, and most others. For the few that don't understand it, it will just let them type anything; won't return an error. Sorry for the line breaks, Yahoo seems to be hiding code if I don't break it up.
Reply:You can do it easily via asp or javascript.
Just you need the ASCII code of a key.
So you can get them via textpad.Textpad has all of them on the left side panel.
Find the key code(number) from 0 to 9.
Then you should write some code for Text_Changed event of the textbox.Just you should unhandle the key press function while a number is not entered.
If(e.KeyCode%26lt;num1 | e.KeyCode%26gt;num2)
{
e.Handled=false;
}
Reply:The easiest way is to use a Range Validator control.
%26lt;asp:TextBox ID="txtTest" runat="server" /%26gt;
%26lt;asp:RangeValidator ID="ValidateTest" runat="server" ControlToValidate="txtTest" Type="Integer" ErrorMessage="Input should be integer" MaximumValue="1000" MinimumValue="0" /%26gt;
Notice: In order to make the field required, you have to add a Required Field validator. i.e. the code above considers that an empty textbox has valid data.
Hope this helps
Reply:In VB, you can use the KEYPRESS event of the textbox to restrict the ASCII characters to between certain values. Not quite sure how to do that in C#, but that should give you a startign point.
Reply:You validate the text using a regular expression.
I believe the regex for non-numeric is \D .
So any match for \D means there is non-numeric in the string.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment