Solved

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

Posted on 2014-03-31
10
14,703 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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…

777 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