I would like to check if the INPUT value is a number, and if not, the field should be cleared. The script below always gives the alert if it's not numeric, but never cleares the input. Anyone an idea what I did wrong?
<script>
function numeric(input)
{
if(isNaN(input))
{
alert("Is not a numeric value");
input.value="";
}
}
</script>
<input type="number" value="" name="<?php echo $x; ?>" onblur="return numeric(value)"/>
Use this
$( "#numerictext" ).keyup(function() {
var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
var str = $('#numerictext').val();
if(!numberRegex.test(str)) {
alert("Is not a numeric value");
$('#numerictext').val('')
}
});
<input type="number" id="numerictext" value="" name="<?php echo $x; ?>"/>