• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

requiring form input

I have a form with 20 check boxes and two text input boxes. One for email, the other for zip code. I would like to make it so that the email is required and if it isn't entered and the submit button is pushed a error window will pop up stating that their email address is required to proceed. How can I do that?

Thanks
0
timshank
Asked:
timshank
  • 4
1 Solution
 
MHQCommented:
This code should do the trick:

<HTML>
<HEAD>
<SCRIPT>
function submit_page(form) {

            // CHECKES IF THERE IS AN ADRESS
        if(form.theField.value == "") {
            alert("You left the Email field blank.");
            return false;
        }

        // CHECKS IF IT IS A VALID ADRESS
            if(form.theField.value.indexOf('@', 0) == -1) {
              alert("You did not enter a valid email address.");
                  return false;
        }
            form.submit();      
    }
</SCRIPT>
</HEAD>

<BODY>
<form action="thepagetosendto.html" name="theform">
<input type="text" name="theField">
<input type="button" value="SUBMIT" onClick="submit_page(this.form)">
</form>
</BODY>
</HTML>
0
 
timshankAuthor Commented:
The error window comes up, however after I click on the ok button it takes me away from the form page. How can I keep my visitors at the form page until they put their email address in?

Thanks
0
 
MHQCommented:
MHQ changed the proposed answer to a comment
0
 
MHQCommented:
Sorry, that was a slight misstake by me. Try using this code instead:
It should work just as you want it to.

<HTML>
<HEAD>
<SCRIPT>
function submit_page(form) {
            check = true;
            // CHECKES IF THERE IS AN ADRESS
        if(form.theField.value.length < 1) {
            alert("You left the Email field blank.");
            check = false;
        }

        // CHECKS IF IT IS A VALID ADRESS
            if((check) && form.theField.value.indexOf('@', 0) == -1) {
              alert("You did not enter a valid email address.");
                  check = false;
        }
            if (check)
                  form.submit();
    }
</SCRIPT>
</HEAD>

<BODY>
<form action="to your next page" name="theform">
<input type="text" name="theField">
<input type="button" value="SUBMIT" onClick="submit_page(this.form);">
</form>
</BODY>
</HTML>

0
 
MHQCommented:
I'd appreciate if you could take time to evaluate my answer and if it's to your liking, award me the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now