Jump to content
confused and dazed

How Do I Check A Value Before Submitting A Form

Recommended Posts

Hello folks.Here is my problem.I have a bunch of hidden id's with values but I need to check to see if those values are right before I submit my form. I tried an if statement but it is just not working. I am trying something like Function checkquad(){if(r1.value="");if(r2.value="");{alert("r1 and r2 not filled out right");}} What I am trying to say with my if statements is that if the value r1 and r2 (from a from I named tourney) do not have a value set yet then send out the alert and dont submit the form... Not working...

  • Like 1

Share this post


Link to post
Share on other sites

Putting tags on the page doesn't define arbitrary variables in every Javascript function. You need to actually get references inside Javascript to the HTML elements on the page. HTML and Javascript aren't the same thing. Check the link I posted. Your if statement also isn't structured correctly, check the tutorial about if statements: http://www.w3schools.com/js/js_if_else.asp Note that you don't put a semicolon after the condition. Writing this:if(r1.value=""); Is the same as doing nothing: if(r1.value=""){ } Example 3 on the above link shows an example where they test two conditions with an AND, but you probably want to use OR. This page has examples of using AND and OR operators: http://www.w3schools.com/js/js_comparisons.asp Also on that page, look at the == operator. That's the one you need to use in an if statement. When you use = it assigns a value, when you use == it compares values.

Share this post


Link to post
Share on other sites

O.K. i was able to fix my comparison issue with ==, thanks. But I still cannot seem to pull the value of r1 into the value for quad function show_alert(){quad ="document.tourney.r1.value"; //tourney is the name of the form where r1 exsists if(quad=="higher") //higher is the value of the hidden value r1 { alert("Hello! yes"); } else { alert("nope"); }}

Share this post


Link to post
Share on other sites

For the third time, look at the tutorial and use document.getElementById. var r1 = document.getElementById('r1');alert(r1.value);

Share this post


Link to post
Share on other sites

First, you have to get the value of it:if(quad.value != "") {Also, you might be trying to check the value before you have set it.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...