Solved

requiring form input

Posted on 2000-03-26
5
165 Views
Last Modified: 2010-04-09
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
Comment
Question by:timshank
  • 4
5 Comments
 
LVL 3

Expert Comment

by:MHQ
Comment Utility
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
 

Author Comment

by:timshank
Comment Utility
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
 
LVL 3

Expert Comment

by:MHQ
Comment Utility
MHQ changed the proposed answer to a comment
0
 
LVL 3

Accepted Solution

by:
MHQ earned 50 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:MHQ
Comment Utility
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now