Solved

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

Posted on 2014-03-31
10
14,866 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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 add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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