poofacer Posted May 7, 2007 Share Posted May 7, 2007 i think my problem is simple to solve but i am a beginner to javascript but not coding. <script type="text/javascript"> <!-- function validateanswer(form) { if (form.answer.value == "") { alert ("Please enter an answer!"); form.answer.focus(); return false; } return true; } //--> </script> So here is my javascript code between the head tags. this should alert me if my 'answer' field is empty. simple enough but it doesnt. onsubmit="return validateanswer(form);" I have this in my <form> tag before the >. so on submit the script should run. ok. my form action takes me to another page. does that matter? I dont think it does. What is wrong here? form.answer.value, the word 'form' here does this have to resemble my form name? I took this coding from a book but it doesnt seem to work. the alert doesnt execute. I have some php code in the file however it doesnt interfere. yes i do have js enabled. thanks in advance. help me debug. Link to comment https://forums.phpfreaks.com/topic/50309-form-validation/ Share on other sites More sharing options...
paul2463 Posted May 7, 2007 Share Posted May 7, 2007 an assumption I will make here is 1. the input text field of your form has an id of answer such as <input="text" name="answer" id="answer"> <script type="text/javascript"> function validateanswer() { if (document.getElementById("answer").value == "") { alert ("Please enter an answer!"); document.getElementById("answer").focus(); return false; } return true; } </script> and as you ahve in your form element tags you must have onSubmit="return validateanswer();" Link to comment https://forums.phpfreaks.com/topic/50309-form-validation/#findComment-247150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.