Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  122] [ 3]  / answers: 1 / hits: 43664  / 12 Years ago, fri, july 13, 2012, 12:00:00

The Question



To help avoid end-user confusion, I want to add an alert message that pops up if/when the user clicks any other key [alert('Only Numerical data allowed')]. So if they press the 'k' the above message will pop up. Can anyone see how to set this code within this code base








The Code



jquery:



$('input.numberinput').bind('keypress', function (e) {
var w = e.which;
return (w != 8 && w != 0 && (w < 48 || w > 57) && w != 46) ? false : true;
});




html



<div class=containercontent>

<div class=label>Enter a number:</div>
<input type=text name=txtNumber1 id=txtNumber1 value= class=numberinput />

<div class=label>Enter a number:</div>
<input type=text name=txtNumber2 id=txtNumber2 value= class=numberinput />
</div>

More From » jquery

 Answers
6

Hello again :) I can help you out with this as well:



$(document).ready(function () {
$('input.numberinput').bind('keypress', function (e) {
var allow = (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
if (!allow) {
alert('Only Numerical data allowed');
}
return allow;
});
});?


JSFiddle: http://jsfiddle.net/EN8pT/3/



Enjoy and good luck!


[#84269] Thursday, July 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadap

Total Points: 101
Total Questions: 104
Total Answers: 98

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;