0

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

4

2 回答 2

0

In your .js-file, you have to remove the html-tags < script language="javascript"> and < /script>.

于 2013-06-18T07:27:05.870 回答
0

I was thinking about possibly re-installing as I did not want to comb through code to see where Wordpress would have added the code mentioned. However, that appears to not have been the problem. The solution is here:

https://wordpress.stackexchange.com/questions/103456/javascript-on-registration-page/103459?noredirect=1#103459

于 2013-06-19T07:56:59.100 回答