Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

requiring form input

Posted on 2000-03-26
5
193 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 27
How to check if a session is disconnected in php 9 32
SSRS 2016 Rendering HTML tables 3 26
Media Queries  not Firing 6 17
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

856 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