I'm getting Uncaught ReferenceError: e is not defined.
Input field
<input class="form-control text-right" name="amount" maxlength="45" value="${exp.Amount}" onkeyup='evMoneyFormat( e );' required="required">
Script
<script type="text/javascript">
function evMoneyFormat(evt) {
//--- only accepts accepts number and 2 decimal place value
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
if (!regex.test(key)) {
theEvent.returnValue = false;
//--- this prevents the character from being displayed
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
How can i fix this ?
I'm getting Uncaught ReferenceError: e is not defined.
Input field
<input class="form-control text-right" name="amount" maxlength="45" value="${exp.Amount}" onkeyup='evMoneyFormat( e );' required="required">
Script
<script type="text/javascript">
function evMoneyFormat(evt) {
//--- only accepts accepts number and 2 decimal place value
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
if (!regex.test(key)) {
theEvent.returnValue = false;
//--- this prevents the character from being displayed
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
How can i fix this ?
Share Improve this question asked Jun 25, 2014 at 3:19 BishanBishan 15.8k53 gold badges172 silver badges268 bronze badges 2-
3
Well, what's
e
doing there? I'd suggest adding the event in JavaScript – elclanrs Commented Jun 25, 2014 at 3:20 -
Do you even need
e
here? – Nikhil Talreja Commented Jun 25, 2014 at 3:23
2 Answers
Reset to default 4I think it should be(the event object is available in the inlined context as event
not as e
)
onkeyup='evMoneyFormat( event );'
Since you have tagged it with jQuery, use a jQuery event handler instead of inlined one
You don't need to pass anything to onkeyup
.
It should just be: onkeyup='evMoneyFormat()';
.
When your handler is called, if you have provided a parameter to your function (which you have: evt
), then the event data will automatically be assigned to the parameter.
You can then use evt
in your handler for the event data.
However, seeing as you've tagged this with jQuery, a simpler way would be:
$(".form-control text-right").keyup(function(evt){
//--- only accepts accepts number and 2 decimal place value
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
if (!regex.test(key)) {
theEvent.returnValue = false;
//--- this prevents the character from being displayed
if (theEvent.preventDefault) theEvent.preventDefault();
}
});