The code below is suppossed to take an input in a textbox and upon the click of a button call a function to check the input. I cannot get this to work at all the text box and button appear but the code for the function appears on the page and the button press does nothing. Any advice here please // Creates text box for bank sort code to be entered <p>The layout used in this input is three pairs of numbers seperated by -. Thanks for taking the time to read this!</p> <input type="text" name="Bank_Sort_Code" maxlength="8" /> <p><input type="button" value="Click here" onClick="validateBankNo();"></p> <script language="javascript" type="text/javascript"> function validateBankNo() { if(ereg('^[0-9]{2}-[0-9]{2}-[0-9]{2}$', $Bank_Sort_Code)) return True; else return false; } </script>
I am not sure what is $Bank_Sort_Code... Are you atring to write any php code inside the javascript or are you using any json or jquery like libraries ? If not here is one way you can do this <script language="javascript" type="text/javascript"> function validateBankNo() { var Bank_Sort_Code = document.getElementById("Bank_Sort_Code"); if(ereg('^[0-9]{2}-[0-9]{2}-[0-9]{2}$', Bank_Sort_Code)) alert("Okay"); return True; else return false; } </script> <input type="text" id="Bank_Sort_Code" name="Bank_Sort_Code" maxlength="8"> <p><input type="button" value="Click here" onClick="validateBankNo();"></p> Code (markup):