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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.