Solved

jQuery onclick submit button of a form validate textbox value at the same time

Posted on 2014-03-31
10
14,946 Views
Last Modified: 2014-04-01
I have a contact form. I added simple captcha using php code like below

<?php $my_string = substr(str_shuffle(MD5(microtime())), 0, 5);  ?>

Open in new window



and my contact.php page, at the top of the page I added the above line and calling it inside the form like below

<form action="/cgi-bin/email.cgi" method="post" />
 <input type="hidden" name="recipient" value="contact" />
<input type="hidden" name="required" value="realname,phone,email,verify"> 
  <input type="hidden" name="sort" value="realname,city,state,zip,phone,email,verify,comments">
<input type="hidden" name="redirect" value="http://mysite.com/thank-you.html"> 
<input type="hidden" name="subject" value="Customer Contact Form">
<table id="tContact">
<tr>
<td>Name*</td>
<td><input name="realname" type="text" id="realname" size="45" /></td>
</tr>
<tr>
<td>Phone*</td>
<td><input name="phone" type="text" id="phone" size="35" maxlength="20" /></td>
</tr>
<tr>
<td>Email*</td>
<td><input name="email" type="text" id="email" size="35" maxlength="100" /></td>
</tr>
<tr>
<td>Re-enter Email*</td>
<td><input name="verify" type="text" id="verify" size="35" maxlength="100" /></td>
</tr>
<tr>
<td>Comments</td>
<td><textarea name="comments" cols="50" rows="5" id="comments"></textarea></td>
</tr>
<tr>
<td>Captcha</td>
<td><div class="captcha"><?php echo "$my_string" ?></div> 
<input type="text" name="ctext" id="ctext" class="ctext" value="" width="160" height="20" /></td>
</tr>
<tr>
<td colspan="2"><input name="Submit" type="submit" id="Submit" value="Send Form" /> </td>
</tr>

</table>
</form>

Open in new window


and I am using jquery to grab the value of the captcha and checking it against the customer entered value in the input text box .ctext

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#Submit").click(function(){
    var text = $(".captcha").text();
	var text1 = $(".ctext").text();
	if (text1 == text) {
          // need to submit the form if the they are equla
	} else {
	   alert("Please enter the correct characters & numbers");	
	}
  });
});
</script>

Open in new window


as you can see I want to submit the form only when the captcha is correct but as asson as I click the submit the alert is working fine but the form is also getting submitted

how I can submit the form only when the captcha is correct

Thanks
0
Comment
Question by:niceoneishere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 1

Author Comment

by:niceoneishere
ID: 39968302
Ok I have modified by script like below but still after the alert box is displayed the form is still getting processed.

I added a onsubmit to form like below

<form action="/cgi-bin/email.cgi" method="post" onsubmit="return checkform(this)" />
 <input type="hidden" name="recipient" value="contact" />
<input type="hidden" name="required" value="realname,phone,email,verify"> 
  <input type="hidden" name="sort" value="realname,city,state,zip,phone,email,verify,comments">
<input type="hidden" name="redirect" value="http://mysite.com/thank-you.html"> 
<input type="hidden" name="subject" value="Customer Contact Form">
<table id="tContact">
<tr>
<td>Name*</td>
<td><input name="realname" type="text" id="realname" size="45" /></td>
</tr>
<tr>
<td>Phone*</td>
<td><input name="phone" type="text" id="phone" size="35" maxlength="20" /></td>
</tr>
<tr>
<td>Email*</td>
<td><input name="email" type="text" id="email" size="35" maxlength="100" /></td>
</tr>
<tr>
<td>Re-enter Email*</td>
<td><input name="verify" type="text" id="verify" size="35" maxlength="100" /></td>
</tr>
<tr>
<td>Comments</td>
<td><textarea name="comments" cols="50" rows="5" id="comments"></textarea></td>
</tr>
<tr>
<td>Captcha</td>
<td><div class="captcha"><?php echo "$my_string" ?></div> 
<input type="text" name="ctext" id="ctext" class="ctext" value="" width="160" height="20" /></td>
</tr>
<tr>
<td colspan="2"><input name="Submit" type="submit" id="Submit" value="Send Form" /> </td>
</tr>

</table>
</form>

Open in new window


and my modified script is

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
  function checkform(){
	  var text = $(".captcha").text();
	var text1 = $(".ctext").text();
	if (text1 !== text){
		 alert("Please enter the correct characters & numbers");
		 return false;	
	}
		
	}
</script>

Open in new window


Thanks and appreciate it
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39969254
remove

 onsubmit="return checkform(this)"

from your form and add it to your submit button
0
 
LVL 2

Expert Comment

by:GowthamNatarajan
ID: 39969314
Try this...

Change the form tag
<form action="" method="post" id="Form1" >

and submit button
<input name="Submit" type="button" id="Submit" value="Send Form" onclick="return checkform(this) "/>

and script as
 function checkform() {
            var text = $(".captcha").text();
            var text1 = $(".ctext").val();
            if (text1 !== text) {
                alert("Please enter the correct characters & numbers");
                return false;
            } else {
               $("#Form1").submit();
            }

        }
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 1

Author Comment

by:niceoneishere
ID: 39969394
Thanks guys but this still does not work, here my test page I created. I have modified the code and added the onclick="return checkform(this)" to the submit button,

http://nehikingholidays.com/contact-us-testing.php

I have also noticed that the input text box is not holding the value

Thanks and appreciate it
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39969417
try changing

var text = $(".captcha").text();

to

var text = $(".captcha").html();

also, you may want to use a different variable name than text, as I'm not 100% sure it's not a javascript keyword.
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 39969431
I did the changes still no good :(
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 39969601
i got it working on a fiddle:

http://jsfiddle.net/bigmonty/yFK4z/
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 39969615
I got it to work I need to grab the value of the input box by changing it to .val() instead of .text()

Thanks
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 39969620
LOL we both had the same idea :)
0
 
LVL 1

Author Closing Comment

by:niceoneishere
ID: 39969621
Thanks
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question