I'm trying to use the following javascript to restrict a field on my registration form to accept only numbers:
<script language="javascript">
function forceNumber(event){
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 32 && keyCode != 37 && keyCode != 39 && keyCode != 40 && keyCode != 41 && keyCode != 43 && keyCode != 45 && keyCode != 46)
return false;
}
</script>
I first just added the code to the top of functions.php. It worked but gave me an "cannot modify header information" error.
I looked into it further and saved it as forceNumber.js and attempted to call it in functions.php using the wp_enqueue function which is supposed to be the normative way of calling it like so:
function forceNumber() {
wp_enqueue_script( 'force-number', get_template_directory_uri() . '/js/forceNumber.js', array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'forceNumber' );
This did not work at all, even though this is how the documentation suggests to do it. When I check it out with Firebug the javascript is not even being called in the header (?!). So no wonder it is not working. I just don't understand how the js is not being called correctly.
Thanks and kind regards, Helena