Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 09-29-2012 at 07:05 PM. Reason: Typo

    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.

  • Users who have thanked Philip M for this post:

    keneso (09-29-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • Users who have thanked Philip M for this post:

    keneso (09-29-2012)

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •