SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Both fields or none; popup alert otherwise

    What would be the necessary Javascript to check if a certain 2 fields are filled out? If neither or both have values, the form can submit. However, if only one of the two has a value, there's an alert with a message and the submission cancels.

    Thanks.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are the names of the 2 fields and of the form?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    l_distance, l_mileage_integers and the form doesn't have a name. If I need to name it somthing, I can name it "car."

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is one way to do it.

    Code:
    <script type="text/javascript"><!--
    function validate() {
      var f = document.forms['car'];
      if((f.l_distance.value != '') && (f.l_mileage_integers.value == '')) {
        alert('Please fill out one or both fields.');
        return false;
      } else if((f.l_distance.value == '') && (f.l_mileage_integers.value != '')) {
        alert('Please fill out one or both fields.');
        return false;
      }
    }
    // -->
    </script>
    Code:
    <form name="car" ... onsubmit="return validate();">
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Works like a charm. So you know, this is for my real hybrid mileage database:
    http://www.greenhybrid.com/compare/mileage/

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wish I could afford a hybrid.

    Anyway, you're welcome, I'm glad I could help.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Wizard geiger's Avatar
    Join Date
    Jul 2001
    Posts
    2,459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $20k for the Civic Hybrid.
    $21k for the Prius (if you can find one at MSRP).

    </offtopic>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •