Solved

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

Posted on 2014-03-31
10
14,622 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 86
Add Background to a PDF Programmatically 1 24
onOpen 14 42
Cordova Camera plugin fails 2 21
This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

757 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

20 Experts available now in Live!

Get 1:1 Help Now