textbox only allow numbers

Posted by Klbaiju under JavaScript on 3/29/2012 | Points: 10 | Views : 11137 | Status : [Member] | Replies : 6
hi,
in my application i need 1 text box that doesn't allow characters and allow only numbers
how it is possible.
i want tot write the code in textchanged event


Regards

BAIJU




Responses

Posted by: Ujjal on: 3/29/2012 [Member] Starter | Points: 25

Up
0
Down
Try This :-

In html part of the section put the following javascript code :-

<script language="Javascript" type="text/javascript">
var isShift=false;
function isNumeric(keyCode)

{
if(keyCode==16)

isShift = true;
return (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 || keyCode == 189 || keyCode == 9 || (keyCode >= 96 && keyCode <= 105)) && isShift==false)

}
function isAlpha(keyCode)

{
if(keyCode==16)

isShift = true;
return (((keyCode >= 65 && keyCode <= 90) || keyCode == 8 || keyCode == 32 || keyCode == 190) && isShift==false)

}
function keyUP(keyCode)

{
if(keyCode==16)

isShift = false;

//alert(isShift);

}

</script>


and in the textbox section write down the following code

<asp:TextBox ID="txtrollno" runat="server" onkeyup = "keyUP(event.keyCode)" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;" ></asp:TextBox>



Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 3/29/2012 [Member] Bronze | Points: 25

Up
0
Down
function isNumber(inputStr) {
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.substring(i, i + 1)
if (oneChar < "0" || oneChar > "9") {
alert("Please enter only numeric value");
return false;
}
}
return true;
}

Thanks,
Rajni Shekhar

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 3/30/2012 [Member] Bronze | Points: 25

Up
0
Down
Try wit this....
function EnterOnlyNumbers(x)

{
var len=x.value.length;
var s_charcode=0;
for(var s_i=0;s_i<len;s_i++)
{
s_charcode=x.value.charCodeAt(s_i);
if(!((s_charcode>=48 && s_charcode<=57)))
{
alert("Enter only numbers");
x.value="";
x.focus();
return false;
}
}
return true;
}
<asp:textbox id="txt1" ruat="server" onChange="EnterOnlyNumbers(this)">


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Taanu51 on: 3/30/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

By using Jquery we can simply validate ..check this link

http://www.developerscode.com/2011/09/allow-only-numerics-using-jquery-in.html

let me know..if you have any doubts

If you found answer,pls mark as answer

Tanisha Sayyad
.Net and Android Developer
INDIA(Andhra Pradesh)

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/31/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Write down following javascript

function isNumeric(your-text){

var regX = "^[0-9]*$";
return regX.test(your-test);
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
<asp:TextBox ID="txtorderid" runat="server" TabIndex="1"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtorderid" ErrorMessage="only number are allowed" ValidationExpression="^\d+$" ValidationGroup="a"></asp:RegularExpressionValidator>


ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response