Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-29-2012, 05:33 PM   PM User | #1
keneso
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
keneso is an unknown quantity at this point
forcing a specific mandatory text in text field

Hi,

I am trying to add another text field to an existing form with mandatory fields, this extra field should be filled in with a specific text, i.e I accept.
I know the other option would be the check-box, but I want the users to actually type "I accept the terms".

I have added these bits:

Code:
accept_terms: {// compound rule - my addition
required: true,
accept: true
},

accept: "<?php echo _ACCEPTTERMS_;?>", // my addition
to this script, but I don't know how to tell it to validate only the "I accept the terms" phrase.

Code:
<script type="text/javascript">
$(document).ready(function() {
$("#frmReg").validate({
rules: {
user_firstname: "required",// simple rule, converted to {required:true}
user_lastname: "required",
username: {
required: true,
minlength: 2
},
user_email: {// compound rule
required: true,
email: true
},

accept_terms: {// compound rule - my addition
required: true,
accept: true
},

},
messages: {
user_firstname: "<?php echo _FIRSTNAMEMANDATORY_;?>",
user_lastname: "<?php echo _LASTNAMEMANDATORY_;?>",
username: {
required: "<?php echo _USERNAMEMANDATORY_;?>",
minlength: "<?php echo _MINUSERNAMECHAR_;?>"
},
email: "<?php echo _EMAILVALID_;?>",
accept: "<?php echo _ACCEPTTERMS_;?>", // my addition
}
});
});
</script>
As a curiosity do you think this solution could work as a rudimental captcha?

Thank you
keneso is offline   Reply With Quote
Old 09-29-2012, 06:01 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type = "text/javascript">

function checkaccept() {
var terms = document.getElementById("accept-terms").value;
terms = terms.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
terms =terms.replace(/\s{2,}/g," ");  // replace multiple spaces with one space
if (terms.tolowerCase() != "i accept the terms") {
alert ('You must enter the words "I accept the terms" in the textbox');
document.getElementById("accept-terms").value = "";  // clear the field;
return false;
}
}
</script>
Yes, it would work as a captcha. Your users may also be put off by being made to type the words.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 09-29-2012 at 07:05 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
keneso (09-29-2012)
Old 09-29-2012, 07:12 PM   PM User | #3
keneso
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
keneso is an unknown quantity at this point
Thank you.
I forgot to say I am an illiterate of coding.

Now, where would I put the code you suggested?

I tried adding it to my existing code (right before the closing </script>), of course without the <script></script> tags,

and added this to html
Code:
Please type I accept the terms <input type="text" id="accept_terms" name="accept_terms" value="<?php echo $_POST['accept_terms'];?>" />
Obviously as you can guess it didn't work.
Also is this
Code:
(terms.tolowerCase()
to make it lowercase, or can I have it case sensitive by removing that?

At first try I thought it was due to the difference between your "accept-terms", as opposed to "accept_terms", I changed it, but no luck.

Thanks for the patience.
keneso is offline   Reply With Quote
Old 09-29-2012, 08:02 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You need to call the function when the form is submitted.

onsubmit = "return checkaccept()"

You can make the comparison case-sensitive if you insist but this risks alienating your users (even more).

Yes, accept-terms is not the same as accept_terms. These days most people prefer acceptTerms.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
keneso (09-29-2012)
Old 09-29-2012, 08:42 PM   PM User | #5
keneso
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
keneso is an unknown quantity at this point
Thank you ... BTW do I have to click the thank you next to each post?

if I don't type anything error says:
"this field is required"
instead of alert text you put.
Then I type:
"i accept the terms"
and get this error:
"Please enter a value with a valid extension."

I searched the file and found nothing similar to that!
keneso is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:40 AM.


Advertisement
Log in to turn off these ads.