Welcome to WebmasterWorld Guest from 23.22.250.113

Forum Moderators: open

Message Too Old, No Replies

forcing a specific mandatory text in text field

     

keneso

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



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 (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() {
$("#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

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



Am I asking something I shouldn't have? ;)

MichaelBluejay

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;
}
}
</script>

<form onsubmit="return validate()" action=post.php>
<input id=myTextField>
<input type=submit value=Submit>
</form>

keneso

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.

keneso

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.

daveVk

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.

keneso

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?

daveVk

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")

keneso

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

Hot Threads This Week

Hot Threads This Month