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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:34:22+00:00 2026-05-27T02:34:22+00:00

I have created code which works, it posts, an ajax call. It also posts

  • 0

I have created code which works, it posts, an ajax call. It also posts a success message. I want to add validation to the click function. So it only runs $.ajax if validation is true. I have a form validation script for this.

It is a syntax issue I can not find what though

ajax

  $(document).ready ( function () {
        $('#go').click (function () {


       $.ajax ( {
                type:       'POST',
                data:       $('#newsletter').serialize (),
                url:        $('#newsletter').attr ('action'),
                success:   function(){



                                $('#thankYou').show (
                                    1000,
                                    function () {
                                        setTimeout ( function() { $('#thankYou').hide(1000); }, 3000);
                                    }
                                );


            }

            } );
            return false;
        } );
    } )

I want to run checkform() inside $(‘#go’).click and only if form passes run $.ajax as a callback of checkform.

function checkform() {
  for (i=0;i<fieldstocheck.length;i++) {
    if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
      if (document.subscribeform.elements[fieldstocheck[i]].checked) {
      } else {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
    else {
      if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
  }
  for (i=0;i<groupstocheck.length;i++) {
    if (!checkGroup(groupstocheck[i],groupnames[i])) {
      return false;
    }
  }

  if(! compareEmail())
  {
    alert("Email Addresses you entered do not match");
    return false;
  }
  return true;
}

var fieldstocheck = new Array();
var fieldnames = new Array();
function addFieldToCheck(value,name) {
  fieldstocheck[fieldstocheck.length] = value;
  fieldnames[fieldnames.length] = name;
}
var groupstocheck = new Array();
var groupnames = new Array();
function addGroupToCheck(value,name) {
  groupstocheck[groupstocheck.length] = value;
  groupnames[groupnames.length] = name;
}

function compareEmail()
{
  return (document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);
}
function checkGroup(name,value) {
  option = -1;
  for (i=0;i<document.subscribeform.elements[name].length;i++) {
    if (document.subscribeform.elements[name][i].checked) {
      option = i;
    }
  }
  if (option == -1) {
    alert ("Please enter your "+value);
    return false;
  }
  return true;
}

markup

<div id="container">
<form id="newsletter"  method="post" action="http://www.officeyoganyc.com/lists/?p=subscribe" name="subscribeform"><input type="hidden" name="formtoken" value="a7d1884b463ed70e91fb62a5121e9846" />

<div id="fieldWrapper">
<div class="fieldHolder">
  <div class="attributeinput1"><input type=text name=email value="email" autofocus="autofocus" autocomplete="on" size="12"/> 
  <script language="Javascript" type="text/javascript">addFieldToCheck("email","Email");</script></div> 
  </div>


  <div class="fieldHolder2">
  <div class="attributeinput2"><input type=text name=emailconfirm value="confirm email" autocomplete="off" size="12"/> 
  <script language="Javascript" type="text/javascript">addFieldToCheck("emailconfirm","Confirm your email address");</script></div>
        </div> 
        </div>
  <input type="hidden" name="list[1]" value="signup">
  <input type="hidden" name="listname[1]" value="office yoga list"/>
  <div style="display:none"><input type="text" name="VerificationCodeX" value="" size="20"></div>

<input type="hidden" name="subscribe" value="Subscribe"/>
<div id="subscribe"><input type=image src="http://www.officeyoganyc.com/themes/zen/zen/images/yogaSubmit.png" id="go" name="subscribe" value="Subscribe onClick="return checkform();"></div>
    </form>

    </div>
  <div id="thankYou">Thank You For Signing Up</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-27T02:34:23+00:00Added an answer on May 27, 2026 at 2:34 am

    Alex,

    The checkForm function returns a boolean indicating success or failure, then put an if statement as below just above the ajax but inside the click event so you have the ajax called only if checkform gives true, otherwise write the else portion to alert the user to fix whatever is wrong.

    if(checkform()) {
       $.ajax(.....)
    } else {
       alert("Validation Failed");
    }
    

    On a side note, I caution you extremely highly not to rely on the javascript (client-side) validation only – you need to make sure your server script called by the ajax also checks to make sure the form post data hasn’t been doctored to hack you. Forewarned.

    R

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

Sidebar

Related Questions

I have created some python code which creates an object in a loop, and
I have created an iframe code that I want people to use as a
I have a functionality of posting messages to a group which works using AJAX
I have the following code which creates an ImageButton and plays a sound when
I have this code which should create a splash image with either no animation
I have some code which I did not originally create that uses _beginthreadex and
I have created this code, and when I run it, don't get any errors
I have created a report in MS Access report and write some VBA code
I have created a new solution with a minimal amount of code that represents
I have created an alert view with two buttons using the following code: UIAlertView

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.