homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
forcing a specific mandatory text in text field
keneso




msg:4498861
 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




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

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

MichaelBluejay




msg:4501778
 11:25 pm on Sep 29, 2012 (gmt 0)

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




msg:4501971
 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




msg:4502131
 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




msg:4502389
 8:39 am on Oct 1, 2012 (gmt 0)

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




msg:4504172
 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




msg:4504750
 4:46 am on Oct 6, 2012 (gmt 0)

if (document.getElementById('myTextField').value.replace(/\s/g, "").toLowerCase() != "iaccepttheterms")

keneso




msg:4505573
 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? ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved