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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:49:52+00:00 2026-06-14T02:49:52+00:00

What i want to do is when a form is posted, the script loops

  • 0

What i want to do is when a form is posted, the script loops through all inputs with a specific attribute, and if it’s empty, it marks it red and changes the text to “This field is required” for 2 seconds, then it changes back to the default text.

Each field has the title of the field as a value, until you click it, then it disappears. Like a placeholder.

What happens now is the script runs fine, but if i try to post the form during those two seconds when the error text is displayed, it submits. I have no idea why.

Here’s the code:

var isWorkingInputForm = false;

$(document).ready(function() {

    var defFormInputBgColor = null;

    // Warn user if required field is missing when posting form
    $('form').submit(function(e) {

        // Return is form is working
        if(isWorkingInputForm) console.log('is working');
        if(isWorkingInputForm) return false;

        // Halt is true by default.
        var halt = true;

        // Start working. If form is submitted between the start and stop it'll be ignored.
        isWorkingInputForm = true;

        // Nope, it's not working, let's loop through all inputs
        $('.formwrapper *[data-required="true"]').each(function(i, v) {
            console.log('not working');
            var val = $(this).val().trim();
            var defVal = $(this)[0].defaultValue.trim();
            var input = $(this);

            // Store bg color if not already stored
            if(!defFormInputBgColor) {
                defFormInputBgColor = $(this).css('background-color');
            }

            // Check if it's empty (or equal to the default value (placeholder))
            if( val == defVal || !val ) {
                halt = true;
                // Set red background
                $(input).css({
                    'background-color': '#FFDDDE'
                });
                // Show error text
                $(input).val('Det här fältet är obligatoriskt');

                // Wait for two seconds, then show the default text again
                setTimeout(function() {
                    $(input).val(defVal);
                }, 2000);
            } else {
                halt = false;
                $(input).css({
                    'background-color': defFormInputBgColor
                });
            }
        });

        isWorkingInputForm = false;

        if(halt) {
            // Form didn't validate, do nothing
            e.preventDefault();
        }
    });
});

Pay attention to the isWorkingInputForm variable. As soon as the form is submitted, it is set to True, and at the bottom of the script it’s set to False again.

As you can see at the top of the script, i check if isWorkingInputForm is set to true (meaning it’s being paused for two seconds), and if is, return false (don’t continue).

For some reason, the form submits if i press the submit button while it’s paused. Any ideas why?

  • 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-14T02:49:54+00:00Added an answer on June 14, 2026 at 2:49 am

    You are clearing the isWorkingInputForm too soon. You are clearing it when the handler returns, which is way before two seconds. Move isWorkingInputForm = false into the timeout:

    setTimeout(function() {
      $(input).val(defVal);
      isWorkingInputForm = false;
    }, 2000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check in zend, whether a posted form value 'name' contains a
I have a Html form. I want to get the file containing posted form's
Im using jquery and want each form text input to appear one by one,
I want a simple form with one text box and a submit button. For
me created a zend form and want to add dynamic text box in it.
I want to fetch posted data. But I am using no form. The data
I want a form where user can change password. I am able to encrypt
When a user clicks a submit button I want the form to be submitted.
I'm trying to create a splash screen using LWUIT. I want a form to
I want to change form attributes with JQuery. In other browsers it works fine,

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.