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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:14+00:00 2026-05-27T16:10:14+00:00

I’m working on a JQuery form validation script (first project – don’t laugh) and

  • 0

I’m working on a JQuery form validation script (first project – don’t laugh) and so far I have the following code which seems to work.

I’d like to have this validation code execute in a sequence, examining the form values top down. Right now everything is firing all at once. How can I accomplish this?

Thank you!

    // -----------------------------------------------
// FORM VALIDATION
// -----------------------------------------------
function mcValidateForm() {

    // -----------------------------------------------
    // CHECK - EMPTY INPUT TEXT
    // -----------------------------------------------
    $('.mcRequired').each(function() {
        var mcEmptyCheck = $.trim($(this).val());
        if(mcEmptyCheck == '' || mcEmptyCheck.length < 3) {
            mcResponse('- Please fill in the required field!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else {
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK - VALID EMAIL FORMAT
    // -----------------------------------------------
    $('.mcEmail').each(function() {
        var mcEmailCheck = $(this).val();
        var mcEmailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if(!mcEmailCheck.match(mcEmailRegex)) {
            mcResponse('- Incorrect Email format!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
    });

    // -----------------------------------------------
    // CHECK - VALID WEB ADDRESS - URL
    // -----------------------------------------------
    $('.mcWebsite').each(function() {
        var mcUrlCheck = $(this).val();
        var mcUrlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;

        if(!mcUrlCheck.match(mcUrlRegex)) {
            mcResponse('- Incorrect Website Address format!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else {
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK - SINGLE SELECT SELECTION
    // -----------------------------------------------
    $('.mcMenu').each(function() {
        var mcMenuCheck = $(this).val();
        if(mcMenuCheck == null || mcMenuCheck == 'Please Select One') {
            mcResponse('- Please make a Selection!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else if(mcMenuCheck != null) {
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK - MULTI SELECT SELECTION
    // -----------------------------------------------
    $('.mcList').each(function() {
        var mcSelectCheck = $(this).val();
        if(mcSelectCheck == null) {
            mcResponse('- Please make a Selection!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else if(mcSelectCheck != null) {
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK SINGLE CHECKBOX
    // -----------------------------------------------
    $('.mcCbxSingle').each(function() {
        var mcCbxCheck = $(this);
        if(!mcCbxCheck.is(':checked')) {
            mcResponse('- Please check the checkbox!', true);
            $(this).parents(':eq(1)').addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else{
            $(this).parents(':eq(1)').removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK CHECKBOX GROUPS
    // -----------------------------------------------
    $('.mcCbxGroup').each(function() {
        if($(this).find('input[type=checkbox]:checked').length == 0) { 
            mcResponse('- Please check at least one checkbox in the group!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // CHECK RADIO GROUP
    // -----------------------------------------------
    $('.mcRadGroup').each(function() {
        if($(this).find('input[type=radio]:checked').length == 0) { 
            mcResponse('- Please select a radio button!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // FILE UPLOAD - SINGLE
    // -----------------------------------------------
    $('.mcFileUpSingle').each(function() {
        if($(this).find('input[type=file]').val() == '') { 
            mcResponse('- Please select a file to upload!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });

    // -----------------------------------------------
    // FILE UPLOAD - GROUP
    // -----------------------------------------------
    $('.mcFileUpGroup').each(function() {
        $(this).addClass('mcError').fadeOut().fadeIn();
        $('.mcFileUpGroup input[type=file]').each(function() {
            if($(this).val() == '') { 
                mcResponse('- Upload file not selected!', true);
                $(this).parent().addClass('mcError');
                $('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
                return false;
            }
            else{
                $(this).removeClass('mcError');
                $(this).parent().removeClass('mcError');
            }
        });
    });

    // -----------------------------------------------
    // CHECK RECAPTCHA
    // -----------------------------------------------
    var mcRecaptchaDiv = $('#recaptcha_area');
    var mcReCaptcha = $('input[id=recaptcha_response_field]');
    var mcReCaptchaVal = mcReCaptcha.val();
    if(mcReCaptcha.is(':visible')) {
        if($.trim(mcReCaptchaVal) == ''){
            mcResponse('- Please enter the Captcha text as presented below!', true);
            $(mcRecaptchaDiv).addClass('mcError').fadeOut().fadeIn();
            $('html,body').stop().animate({scrollTop: $(mcRecaptchaDiv).offset().top},'slow');
            $(mcReCaptcha).focus();
            return false;
        } else {
            $(mcRecaptchaDiv).removeClass('mcError');
        }
    }
}
  • 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-27T16:10:14+00:00Added an answer on May 27, 2026 at 4:10 pm

    The elements will be validated in the order which you have specified. Your script will be run on a single thread, starting from all .mcRequired fields in the order in which they appear in the DOM (which is the order defined in your html). Then the .mcEmail element will be examined and so forth.

    For your information, there are plenty of plugins which do what your are trying to accomplish, best of which omho is the validation plugin

    UPDATED: OK, I now understand what is that you want to achieve. There is the code that will hopefully do it. note that I have commented out all your “return false” and body.stop statements and added a bit of code at the end of the function to select all mcError elements. The additional benefit is that a lot of duplicating code is removed, although there is room for more re-factoring.

    // -----------------------------------------------
    // FORM VALIDATION
    // -----------------------------------------------  
    function mcValidateForm() {
    
    // -----------------------------------------------
    // CHECK - EMPTY INPUT TEXT
    // -----------------------------------------------
    $('.mcRequired').each(function() {
        var mcEmptyCheck = $.trim($(this).val());
        if(mcEmptyCheck == '' || mcEmptyCheck.length < 3) {
            mcResponse('- Please fill in the required field!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else {
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK - VALID EMAIL FORMAT
    // -----------------------------------------------
    $('.mcEmail').each(function() {
        var mcEmailCheck = $(this).val();
        var mcEmailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    
        if(!mcEmailCheck.match(mcEmailRegex)) {
            mcResponse('- Incorrect Email format!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
    });
    
    // -----------------------------------------------
    // CHECK - VALID WEB ADDRESS - URL
    // -----------------------------------------------
    $('.mcWebsite').each(function() {
        var mcUrlCheck = $(this).val();
        var mcUrlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
    
        if(!mcUrlCheck.match(mcUrlRegex)) {
            mcResponse('- Incorrect Website Address format!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else {
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK - SINGLE SELECT SELECTION
    // -----------------------------------------------
    $('.mcMenu').each(function() {
        var mcMenuCheck = $(this).val();
        if(mcMenuCheck == null || mcMenuCheck == 'Please Select One') {
            mcResponse('- Please make a Selection!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            ///return false;
        }
        else if(mcMenuCheck != null) {
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK - MULTI SELECT SELECTION
    // -----------------------------------------------
    $('.mcList').each(function() {
        var mcSelectCheck = $(this).val();
        if(mcSelectCheck == null) {
            mcResponse('- Please make a Selection!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else if(mcSelectCheck != null) {
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK SINGLE CHECKBOX
    // -----------------------------------------------
    $('.mcCbxSingle').each(function() {
        var mcCbxCheck = $(this);
        if(!mcCbxCheck.is(':checked')) {
            mcResponse('- Please check the checkbox!', true);
            $(this).parents(':eq(1)').addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else{
            $(this).parents(':eq(1)').removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK CHECKBOX GROUPS
    // -----------------------------------------------
    $('.mcCbxGroup').each(function() {
        if($(this).find('input[type=checkbox]:checked').length == 0) { 
            mcResponse('- Please check at least one checkbox in the group!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // CHECK RADIO GROUP
    // -----------------------------------------------
    $('.mcRadGroup').each(function() {
        if($(this).find('input[type=radio]:checked').length == 0) { 
            mcResponse('- Please select a radio button!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // FILE UPLOAD - SINGLE
    // -----------------------------------------------
    $('.mcFileUpSingle').each(function() {
        if($(this).find('input[type=file]').val() == '') { 
            mcResponse('- Please select a file to upload!', true);
            $(this).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
            //return false;
        }
        else{
            $(this).removeClass('mcError');
        }
    });
    
    // -----------------------------------------------
    // FILE UPLOAD - GROUP
    // -----------------------------------------------
    $('.mcFileUpGroup').each(function() {
        $(this).addClass('mcError').fadeOut().fadeIn();
        $('.mcFileUpGroup input[type=file]').each(function() {
            if($(this).val() == '') { 
                mcResponse('- Upload file not selected!', true);
                $(this).parent().addClass('mcError');
                //$('html,body').stop().animate({scrollTop: $(this).offset().top},'slow');
                //return false;
            }
            else{
                $(this).removeClass('mcError');
                $(this).parent().removeClass('mcError');
            }
        });
    });
    
    // -----------------------------------------------
    // CHECK RECAPTCHA
    // -----------------------------------------------
    var mcRecaptchaDiv = $('#recaptcha_area');
    var mcReCaptcha = $('input[id=recaptcha_response_field]');
    var mcReCaptchaVal = mcReCaptcha.val();
    if(mcReCaptcha.is(':visible')) {
        if($.trim(mcReCaptchaVal) == ''){
            mcResponse('- Please enter the Captcha text as presented below!', true);
            $(mcRecaptchaDiv).addClass('mcError').fadeOut().fadeIn();
            //$('html,body').stop().animate({scrollTop: $(mcRecaptchaDiv).offset().top},'slow');
            //$(mcReCaptcha).focus();
            //return false;
        } else {
            $(mcRecaptchaDiv).removeClass('mcError');
        }
    }
    var $errors = $("mcError");
    if($errors.size() > 0){
        $('html,body').stop().animate({scrollTop: $errors.filter(":last").offset().top},'slow');
        return false;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.