Jump to content

form validation.


poofacer

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.