Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:49:24+00:00 2026-06-11T04:49:24+00:00

How can I combine these two functions so that they both work? Right now,

  • 0

How can I combine these two functions so that they both work? Right now, I can only get one to validate and not the other.

SAMPLE OF THE JAVASCRIPT:

function validateForm() {

var x=document.forms["giftCert"]["gift_name"].value;

if (x==null || x=="") {

errMsg = "The recpient's name is required.";              
    $("div#emptyName").show();
    $("div#emptyName").html(errMsg);

return false;
  }
}

function validateForm() {

var x=document.forms["giftCert"]["gift_email"].value;

if (x==null || x=="") {

errMsg2 = "The recpient's email address is required.";              
    $("div#emptyEmail").show();
    $("div#emptyEmail").html(errMsg2);

return false;
  }
}

SHORT VERSION OF THE FORM HTML:

<form action="http://ww6.aitsafe.com/cf/voucher.cfm" method="post" name="giftCert" onsubmit="return validateForm()"  />
    *Name:<input type="text" name="gift_name">
    <div id="emptyName" class="error"></div>

    *Email: <input type="text" name="gift_email">
    <div id="emptyEmail" class="error"></div>

    <input class="button" type="submit" value="Add to Cart" />

</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:49:26+00:00Added an answer on June 11, 2026 at 4:49 am

    You can put the code in the same function.

    function validateForm() {
        return ["Name", "Email"].every(function(type) {
            var lower_type = type.toLowerCase(),
                x=document.forms["giftCert"]["gift_" + lower_type].value;
    
            if (x==null || x=="")
                $("div#empty" + type).show().html("The recpient's " + lower_type + " is required.");
            else
                return true;
        });
    }
    

    Since much of the code is nearly identical, you can put the "Name" and "Email" values in an Array, and loop the Array, invoking the identical code, dropping in the appropriate value where needed.

    This uses the Array.prototype.every to indicate if each value in the Array passed validation.

    If either item fails validation, it returns undefined, the loop halts and validateForm returns false.

    Every item that passes validation returns true. If all items pass validation, .every will return true, and the form proceeds.


    If you want to make sure all the validations run, you can use .filter(), and return true on a failure, so that it will add an item to the resulting Array. If there were no failures, the Array will be empty.

    Then test the .length of the Array, and return a comparison to 0.

    function validateForm() {
        return ["Name", "Email"].filter(function(type) {
            var lower_type = type.toLowerCase(),
                x=document.forms["giftCert"]["gift_" + lower_type].value;
    
            if (x==null || x=="") {
                $("div#empty" + type).show().html("The recpient's " + lower_type + " is required.");
                return true;
            }
        }).length === 0; // will be true if no validation failures
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I combine these two functions into one recursive function to have this
I need to combine/merge two php functions, but I can't get the syntax right.
I have the following. Is there some way that I can combine these two
I saw that even by writing separate commands, we can combine two different types
How can I combine this two selectors: $(td.listas select:last option:selected) This doesn't work! What
How can you combine two windows in Screen? I have two windows such that
I noticed that I can't combine --traditional options with the other one letter other
I have two functions that I want to combine the results of: drawAmbient drawDirectional
I have two XML docs that I've created and I want to combine these
I have two functions that are outputting XML data. Ideally I'd like to combine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.