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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:39:04+00:00 2026-06-17T13:39:04+00:00

My form validates on keyup, but when I try to validate it on submit

  • 0

My form validates on keyup, but when I try to validate it on submit too I just can’t think what to do.

This is my HTML:

   <form action="#" method="post">                      
        <section class="col">   

            <section class="row">                   
                <label for="name">Name</label>
                <input type="text" name="name" value="" id="name" class="field validate-field valid-name" />                    
            </section>

            <section class="row">                   
                <label for="email">Email Address</label>
                <input type="text" name="email" value="" id="email" class="field validate-field valid-mail" />                  
            </section>

            <section class="row">                   
                <label for="phone">Phone Number</label>
                <input type="text" name="phone" value="" id="phone" class="field validate-field valid-phone" />                     
            </section>                  
        </section>

        <section class="col">               
            <label for="message">Message</label>
            <textarea class="field validate-field valid-text" name="message" id="message-field"></textarea>
            <input type="submit" value="Submit" class="submit-button" />
        </section>
   </form>

JS:

function validate( field ){
    var value = field.val();
    var to_label = field.parent().find('label');
    var error = false;
    var error_message = '';

    to_label.find('span').remove();

    if ( field.hasClass('validate-field') && value == '' ) {
        error = true;
        error_message = 'Empty Field';  
    } else if ( field.hasClass('valid-name') && valid_name(value) == false ) {
        error = true;
        error_message = 'Name must consist characters only';
    } else if ( field.hasClass('valid-mail') && valid_email(value) == false ) {
        error = true;
        error_message = 'Invalid Email';
    } else if ( field.hasClass('valid-phone') && valid_phone(value) == false ) {
        error = true;
        error_message = 'Your phone must be digits only';
    };

    if ( error == true ) {
        to_label.append('<span>'+ error_message +'</span>');
    }
};


$('.validate-field').live('keyup', function(){
    validate( $(this) );
});

function valid_name(value){
    var valid = /^([a-zA-Z_\.\-\+])+$/;
    return valid.test(value);
};

function valid_email(value){
    var valid = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return valid.test(value);
};

function valid_phone(value){
    var valid = /^[0-9-+]+$/;
    return valid.test(value);

};

And I have to add something like this:

$('form').live('submit', function(){

    if ( "...validated..." ) {  
        $.post('send.php', $('form').serialize(), function(){
            alert('sent to PHP.');
        })  
    };
    return false;
});

What should be in the submit function?

I have tried:

$('form').live('submit', function(){

    var valid = validate( $('.field') )

    if ( valid  == true ) { 
        $.post('send.php', $('form').serialize(), function(){
            alert('sent to PHP.');
        })  
    };
    return false;
});

But this validates all the forms with all the validation (e-mail, phone …). I have tried in validation() function to add if (error == false){ return: true }, then in submit function ran validation() and added if ( validation() == true ){ .. to send php ..}. That didn’t work too. What I need to do ?

  • 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-17T13:39:06+00:00Added an answer on June 17, 2026 at 1:39 pm

    I guess you can validate the whole form by:

    function validateAll() {
        var valid = true;
        $('form').find('.validate-field').each(function (i, e) {
            if (!validate($(this))) {
               valid = false;
                return;
            }
        });
        return valid;
    }
    
    function validate( field ){
        var value = field.val();
        var to_label = field.parent().find('label');
        var error = false;
        var error_message = '';
    
        to_label.find('span').remove();
    
        if ( field.hasClass('validate-field') && value == '' ) {
            error = true;
            error_message = 'Empty Field';  
        } else if ( field.hasClass('valid-name') && valid_name(value) == false ) {
            error = true;
            error_message = 'Name must consist characters only';
        } else if ( field.hasClass('valid-mail') && valid_email(value) == false ) {
            error = true;
            error_message = 'Invalid Email';
        } else if ( field.hasClass('valid-phone') && valid_phone(value) == false ) {
            error = true;
            error_message = 'Your phone must be digits only';
        };
    
        if (error) {
            to_label.append('<span>'+ error_message +'</span>');
        }
        return !error;
    
    };
    
    $('form').live('submit', function(){
    
        if (validateAll()) {  
            $.post('send.php', $('form').serialize(), function(){
                alert('sent to PHP.');
            })  
        };
        return false;
    });
    

    That’s the option which requires the smallest amount of refactoring, on my opinion.

    Just let me explain you what the function validateAll does. It finds all fields with class validate-field and pass it as argument to the validate function. Because of the refactoring I made in validate it returns false if the field is not valid so when we call validate with specific input and it’s invalid we just return false (the form is not valid).

    Here is an example in JSfiddle.

    For more advance validation I can recommend you validation plugins like: http://docs.jquery.com/Plugins/Validation or jqxValidator.

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

Sidebar

Related Questions

my form validation does not firing...if i try to submit this form without entering
I'm try to validate form using jquery validation plugin my from code: source code
I'm not using any jQuery plugin to validate the form, just plain jQuery. My
My form validates and submits fine, but the variables are not being printed in
i'm using jQuery validate() plugin to validate a form. it does the validation but
this is my form and validation jQuery, but in the UI this show in
i'm using this plug in to validate a form http://bassistance.de/jquery-plugins/jquery-plugin-validation/ I've got 3 fields,
I am trying to validate my form using jquery but I cannot get it
I have the following script to validate my form. It is working fine but
[UPDATED] I'm using JQuery Validate plugin to validate a simple form. It's form HTML:

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.