Solved

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

Posted on 2014-03-31
10
14,673 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
  • 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 32

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
 
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 32

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 56
Angular - "nest" service calls? 18 22
two layers of simple javascript validation 2 24
jQuery Validate 4 17
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now