Validation To Enter Only Numbers

Posted by Santosh198318 under JavaScript on 12/14/2010 | Points: 10 | Views : 27129 | Status : [Member] | Replies : 4
Validation To Enter Only Numbers
======================================================================================================*/
This java script code is used at keypress evnt it showing correct result in IE BROWSER but not in other browsers please provide the js code for all browsers..please provide wat is d difference.......


function isNumericEntry()
{
if( event.keyCode ==13 ||event.keyCode ==32 ||event.keyCode ==45 || event.keyCode >= 48 && event.keyCode <= 57 ) //|| event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57 )
{

event.returnValue = true;
}

else
{
alert('Numerical Entry Only');
event.returnValue = false;
}
}




Responses

Posted by: SheoNarayan on: 12/14/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Use jQuery to save yourself from the scenario that your code is working in one browser and not in other. jQuery works in most of the browsers.

Use below code to validate integer numbers, just copy paste in the html page and you should be able to see how its working.

    Age: <input type="text" id="txtNumber" />

<input type="button" id="btnValidate" value="Validate Integer" />

<script language="javascript" type="text/javascript">
$("#btnValidate").click(function () {
if ($('#txtNumber').val() != "") {
var value = $('#txtNumber').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var intRegex = /^\d+$/;
if (!intRegex.test(value)) {
alert("Please write numbers only");
// you can return false
}
else {
// you can return true
}
} else {
alert("Please enter your age");
// you can return false
}
});
</script>


Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Posted by: Santosh198318 on: 12/14/2010 [Member] Starter | Points: 25

Up
0
Down
required in text box only ...when am entering values into the text box(IE browsers)its working properly.
.but in in other browsers its not working .i want to validate client side only...for that i required please provide ...
i dont want to transfer rto server side


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

Posted by: SheoNarayan on: 12/14/2010 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
The code I have provided works in client side not server side and its for textbox only.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Posted by: Lalji_mer on: 12/14/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,
you can also use below code sample

function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;

if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;

}


OR

<script language="JavaScript">

function allownums(a)
{

if(a <48 ||a > 57)
alert("invalid")
else
alert("vaild")
}

</script>
<input type='text' id='numonly' onkeypress='allownums(event.keycode)'>

more guide will get here
http://www.cambiaresearch.com/c4/029c978b-aac5-472e-97a8-95b256f5febd/How-Can-I-Use-Javascript-to-Allow-Only-Numbers-to-Be-Entered-in-a-TextBox.aspx
http://aruunkumaar.blogspot.com/2009/04/enter-only-numbers-in-text-field-using.html
http://www.dotnetspider.com/resources/26149-Javascript-allow-only-numbers-enter.aspx

LP MER

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

Login to post response