...

View Full Version : forcing a specific mandatory text in text field



keneso
09-29-2012, 05:33 PM
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:


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.


<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

Philip M
09-29-2012, 06:01 PM
<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.

keneso
09-29-2012, 07:12 PM
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

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

(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.

Philip M
09-29-2012, 08:02 PM
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.

keneso
09-29-2012, 08:42 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum