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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:03:40+00:00 2026-05-22T15:03:40+00:00

i have my form validated using jQuery and should be submited using ajax the

  • 0

i have my form validated using jQuery and should be submited using ajax
the problem is. if any of the input is not filled out or wrong data is enered by the user the input should turn on red it works fine, but if i click on the input inorder to fill it out or edit it gets lost.

here is my code

$(document).ready(function(){
  // Place ID's of all required fields here.

  required=["name", "adres","huisnr","postcod", "phone","email"];

   name=$("#name");
   huisnr=$("#huisnr");
   email=$("#email");
   phone=$("#phone");
   postcod=$("#postcod");
   errornotice=$("#error");

   // The text to show up within a field when it is incorrect
     emptyerror="wrong";
    emailerror = "Invalid e-mail.";
     postcoderror="Invalid postcode.";
    phonerror = "Invalid phone number.";

    $(".submit").click(function(){
      //Validate required fields
      for(i=0;i<required.length;i++){
         var input=$('#'+required);

         if(input.val() == "" || input.val() == emptyerror){
            input.addClass("textfield error");
            input.val(emptyerror);
            errornotice.fadeIn(750);
         }else{
            input.removeClass("textfield error");
         }
      }

   //Validate the Postcode.first letter should not be 0, space then the two alphabit big letters from A-Z
          if(! postcod.val().match(/^([1-9]\d{3}\s[A-Z]{2})$/)){
           postcod.addClass("textfield error");
           postcod.val(postcoderror);
         }
   //Validate the phone      
          if(!phone.val().match(/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{5}$/)) {
           phone.addClass("textfield error");
           phone.val(phonerror);
         }      

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

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

      var dataString=   'name=' + name + '&huisnr=' + huisnr + '&email=' + email + '&phone=' + phone + '&postcod=' + postcod ;
      alert (dataString);


 $.ajax({   
  type: "POST",   
  url: "mail.php",   
  data: dataString,   
  success: function() {   
    $('#contact_form').html("<div id='message'></div>");   
    $('#message').html("<h2>Contact Form Submitted!</h2>")   
    .append("<p>We have received your order.</p>")   
    .hide()   
    .fadeIn(1500, function() {   
      $('#message').append("<img id='checkmark' src='images/check.png' />");   
  });
  }
 });
//cancel the submit button default behaviours 
    return false;      


});

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

HTML Form:
<form id="theform" action="mail.php" method="post">
                            <fieldset>
                                <legend></legend>
                                <table cellpadding="0" cellspacing="0" border="0" style="margin-top:7px;">
                                    <tr>
                                        <td><label for="">Name *</label></td>
                                        <td><input type="text" name="name" id="name" value="" class="textfield" tabindex="1" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="">Adres *</label></td>
                                        <td><input type="text" name="adres" id="adres" value="" class="textfield" style="width:160px; margin-right:10px;" tabindex="2" /> Huisnr. * <input type="text" name="huisnr" id="huisnr" value="" class="textfield" style="width:74px;" tabindex="3" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="">Postcode *</label></td>
                                        <td><input type="text" name="postcod" id="postcod" value="" class="textfield" maxlength="7" style="width:70px; margin-right:10px;" tabindex="4" /> 
                                    </tr>
                                    <tr>
                                        <td><label for="">Telefoonnummer *</label></td>
                                        <td><input type="text" name="phone" id="phone" value="" class="textfield" tabindex="6" /></td>
                                    </tr>
                                    <tr>
                                        <td><label for="">E-mailadres *</label></td>
                                        <td><input type="text" name="email" id="email" value="" class="textfield" tabindex="6" /></td>
                                        </tr>
                                        <tr>
                                        <td colspan="2" id="error">There were errors on the form, please make sure all fields are filled out correctly.</td>
                                    </tr>
                                </table>
                            </fieldset>
                        </form>
<div class="checkOut"><a href="" class="submit"><span>Submit Form</span></a><div class="spacer"></div></div>
                </div> 
  • 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-22T15:03:41+00:00Added an answer on May 22, 2026 at 3:03 pm

    the input gets lost when clicking on an input field with error because you have an onFocus listener in you code that does exactly that:

     $(":input").focus(function(){
            if($(this).hasClass("textfield error")){
              // here is your problem:
              $(this).val("");
              $(this).removeClass("textfield error");
            }
          });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.