Solved

requiring form input

Posted on 2000-03-26
5
199 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
ID: 2659228
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
ID: 2659476
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
ID: 2661142
MHQ changed the proposed answer to a comment
0
 
LVL 3

Accepted Solution

by:
MHQ earned 50 total points
ID: 2661171
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
ID: 2676201
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create a slidershow with the use of the property cover in css 15 65
What cart is this? 2 37
Link Anchor Not working 9 49
CSS question 4 27
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Find out what you should include to make the best professional email signature for your organization.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

726 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