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 8845243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:41:26+00:00 2026-06-14T11:41:26+00:00

I am trying to remake a jQuery script by (http://jorenrapini.com/blog/javascript/the-simple-quick-and-small-jquery-html-form-validation-solution). This script is checking

  • 0

I am trying to remake a jQuery script by (http://jorenrapini.com/blog/javascript/the-simple-quick-and-small-jquery-html-form-validation-solution). This script is checking if a from is filled, if not a error message will appear.

What I want to do is to only get the error message when one of two form input-fields are filled out, if none of them are then they should be ignored. The form fields are named “firstinput” and “secondinput” (you can see their id in the code).

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["firstinput", "secondinput"];
    // If using an ID other than #email or #error then replace it here
    email = $("#email");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";

    emailerror = "Please enter a valid e-mail.";

    $("#theform").submit(function(){    
        //Validate required fields
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }

        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function(){       
       if ($(this).hasClass("needsfilled") ) {
            $(this).val("");
            $(this).removeClass("needsfilled");
       }
    });
});

Can anybody please help me with a solution, I would really appreciate it.
/A girl that spend a LOT of time solving this without luck 🙁

  • 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-14T11:41:27+00:00Added an answer on June 14, 2026 at 11:41 am

    I would wrap your for loop in a conditional that evaluates if one or the other has a value.

    if($("#field1").val() == "" && $("#field2").val() == ""){
    //Ignore
    }else{
    //Do something
    }
    
    $(document).ready(function(){
        // Place ID's of all required fields here.
        required = ["firstinput", "secondinput"];
        // If using an ID other than #email or #error then replace it here
        email = $("#email");
        errornotice = $("#error");
        // The text to show up within a field when it is incorrect
        emptyerror = "Please fill out this field.";
    
        emailerror = "Please enter a valid e-mail.";
    
        $("#theform").submit(function(){    
            //Validate required fields
          if($("#firstinput").val() != "" || $("#secondinput").val() != "")
          {
            for (i=0;i<required.length;i++) {
                var input = $('#'+required[i]);
                if ((input.val() == "") || (input.val() == emptyerror)) {
                    input.addClass("needsfilled");
                    input.val(emptyerror);
                    errornotice.fadeIn(750);
                } else {
                    input.removeClass("needsfilled");
                }
            }
          }
            // Validate the e-mail.
            if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
                email.addClass("needsfilled");
                email.val(emailerror);
            }
    
            //if any inputs on the page have the class 'needsfilled' the form will not submit
            if ($(":input").hasClass("needsfilled")) {
                return false;
            } else {
                errornotice.hide();
                return true;
            }
        });
    
        // Clears any fields in the form when the user clicks on them
        $(":input").focus(function(){       
           if ($(this).hasClass("needsfilled") ) {
                $(this).val("");
                $(this).removeClass("needsfilled");
           }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to obtain the following layout in android: http://i54.tinypic.com/iz8enk.png Where in the white
I am trying to remake a program I have made in C# in OBJ-C.In
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I'm trying to remake some of my older projects to support Aero Glass. Although
Trying to create a simple document that gets the parentNode then applies a background
I have this code that draws a Rectangle ( Im trying to remake the
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
For starters: I'm working with Flash CS3 and Actionscript 2.0 I'm trying to remake
Trying to do a simple insert, and this one line is giving me issues.
Trying to convert a perl script to php. In perl I have a hash

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.