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

  • Home
  • SEARCH
  • 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 6185099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:39:10+00:00 2026-05-24T01:39:10+00:00

What would a JavaScript script be that, on submit, gets all form elements with

  • 0

What would a JavaScript script be that, on submit, gets all form elements with class="required" and if they’re empty, displays an alert box, “you must fill out so-and-so”?

I was thinking of an if–else, and in the if section we would get a while that loops through all the class=required elements, and the else would submit the 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-05-24T01:39:11+00:00Added an answer on May 24, 2026 at 1:39 am

    It is fairly easy to loop over the elements of a form and check that those with a certain class have a value that meets certain criteria:

    <form name="f0" onsubmit="return validate(this);">
      <input name="inp0" class="required" >
      <input name="inp2" class="required" >
      <input type="submit">
    </form>
    
    <script type="text/javascript">
    
    var validate = (function() {
      var reClass = /(^|\s)required(\s|$)/;  // Field is required
      var reValue = /^\s*$/;                 // Match all whitespace
    
      return function (form) {
        var elements = form.elements;
        var el;
    
        for (var i=0, iLen=elements.length; i<iLen; i++) {
          el = elements[i];
    
          if (reClass.test(el.className) && reValue.test(el.value)) {
            // Required field has no value or only whitespace
            // Advise user to fix
            alert('please fix ' + el.name);
            return false;
          }
        }
      }
    }());
    
    </script>
    

    The above is just an example to show the strategy.

    Using an alert is less than optimal, usually an area is set aside adjacent to the required fields so that error messages can be written there to direct the user’s attention to the invalid fields. You can also set all the error messages in one go, then return, rather than one at a time.

    Edit—updating multiple errors

    Have an element adjacent to each control to be validated with an id like the element’s, so if an input is called firstName, the error element might have an id of firstName-err. When an error is found, it’s easy to get the related element and put a message in it.

    To do all at once, use a flag to remember if there are any errors, say “isValid” that is set to true by default. If you find any errors, set it to false. Then return it at the end.

    Using the example above, the HTML might look like:

      <input name="firstName" class="required" >
      <span id="firstName-err" class="errMsg"></span>
    

    Errors for firstName will be written to firstName-err.

    In the script, if an error is found:

    // At the top
    var isValid = true;
    var errEl;
    ... 
    
      // When entering the for loop
      el = elements[i];
      errEl = document.getElementById(el.name + '-err');
    
        // when error found
        isValid = false;
        if (errEl) errEl.innerHTML = '... error message ...';
    
        // else if error not found
        // remove message whether there is one or not
        if (errEl) errEl.innerHTML = '';
    
     ...
    
    // At the end
    return isValid;
    

    You can also use a popup to show the errors, however that is really annoying and the users must dismiss the popup to fix the errors. Much better to just write next to each one what is wrong and let the user fix things in their own time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a form that is not being read by serialize() function. <script type=text/javascript> function
I am wondering if anyone knows a java script library that would add a
Are there any good reasons for why you would include JavaScript like this: <script
Given that I have a JavaScript section in my HTML page like : <script
I want to make a form that uses jQuery's ajax function to submit the
I wrote a simple script that add extra form fields when needed by the
I am new to JavaScript and didn't arrive to find a working script that
How to pass form data using POST method and JS submit() ? I would
I have a form, when the user clicks on the submit button this javascript
I have a form that I use in my CMS that I would like

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.