Using Javascript in functions.php
-
I just put this code into functions.php to create a new field on the registration form. Works great. The only thing is: I want the field to allow number only input.
To accomplish this I would like to use the following javascript, which works great:
<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>
which I use in the actual field call thusly:
<input type="number" name="yearly_income" id="yearly_income" class="input" value="<?php echo esc_attr(stripslashes($yearly_income)); ?>" size="25" onkeypress="return forceNumber(event);" /></label>
The only thing is I am having a difficult time finding out how to include it. At first I just plugged it in to the very top of the functions.php file – it worked great on the field but did not like it and gave me an error on the registration page “cannot modify header information”.
After reading a number of tutorials and posts about wp_enqueue I tried to include it in various ways with no luck. I did create a seperate js file “forceNumber.js” and have tried to call it a number of ways without success.
Could anyone offer clear guidance on how to utilize the function of that javascript with the new registration field as entered in the functions.php file?
- The topic ‘Using Javascript in functions.php’ is closed to new replies.