Welcome to WebmasterWorld Guest from

Forum Moderators: open

forcing a specific mandatory text in text field

5:03 pm on Sep 23, 2012 (gmt 0)


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 (which I wouldn't know anyway), 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() {
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

As a curiosity do you think this solution could work as a rudimental captcha?

Thank you
11:38 am on Sep 29, 2012 (gmt 0)

Am I asking something I shouldn't have? ;)
11:25 pm on Sep 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

There's a lot of code. Most people don't want to wade through all if it. I certainly don't. When you need help, post the absolute *minimum* code you need in order to show your problem.

If I were trying to ensure that certain text was typed with Javascript, I'd do it like this:

<script type=text/javascript> 
function validate() {
if (document.getElementById('myTextField').value != "I accept the terms") {
alert("Please type 'I accept the terms' into the text box");
return false;

<form onsubmit="return validate()" action=post.php>
<input id=myTextField>
<input type=submit value=Submit>
11:55 am on Sep 30, 2012 (gmt 0)

Apologies for the code, I had understood to give as much info as possible to facilitate the help, leaving no room for guessing by the experts.

I'll try your suggestion.

Thnak you.
8:37 pm on Sep 30, 2012 (gmt 0)

I tried your suggestion, and here's what happens:

I filled-in the fields but the "I accept the terms", and indeed comes out the alert message, and an error message right under the text field saying "this field is required", then I typed "i accept the terms" (please note lowercase), and alert message comes out again; finally I typed "I accept the terms" (this time capital I), and I get under the text field the following message:
"Please enter a value with a valid extension."

Yhank you.
8:39 am on Oct 1, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

remove the following

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

I think the bold part is triggering the message [docs.jquery.com...]
accept. Makes the element require a certain file extension.

I am not familiar with this plugin, there is probably a rule for checking the text field content that may be preferable to using an alert.
7:06 pm on Oct 4, 2012 (gmt 0)

Thank you.

By removing what you suggested it works fine with the alert.

May I please ask for an extra feature?

How would I make it case INsensitive, and clearing spaces, leading, trailing, double?
4:46 am on Oct 6, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

if (document.getElementById('myTextField').value.replace(/\s/g, "").toLowerCase() != "iaccepttheterms")
6:26 pm on Oct 8, 2012 (gmt 0)

Thank you very much, and apologies for the delay.

Why am I not surprise it works? ;)

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved