PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Custom field that supports validation by regular expressions

  1. kevinmcgillivray
    Member

    Hi there. I'd love to have a field type called "Custom" that allows me to specific my own regular expression for field validation. Seems like it would be a pretty simple thing to add on but I'm just guessing.

    Gravity Forms rocks!

    Thanks!
    kcm

    Posted 10 years ago on Thursday November 11, 2010 | Permalink
  2. That is a good idea, but meanwhile you can add your own custom validation using a hook and a few lines of PHP. If that is something you want to try, I can send you a code snippet.

    Posted 10 years ago on Thursday November 11, 2010 | Permalink
  3. kevinmcgillivray
    Member

    Hi, Alex. Yes, that would be great. Please send me the code snippet.

    Thanks!

    kcm

    Posted 10 years ago on Thursday November 11, 2010 | Permalink
  4. kevinmcgillivray
    Member

    Hi, Alex. Should I email you to get that snippet of code you mentioned?

    Thanks!
    kcm

    Posted 10 years ago on Saturday November 13, 2010 | Permalink
  5. Following is a sample code snippet. To get the submitted field value, you can use $_POST["input_XXX"]

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        // set the form validation to false
        $validation_result["is_valid"] = false;
        $form = $validation_result["form"];
    
        // specify the first field to be invalid and provide custom validation message
        $form["fields"][0]["failed_validation"] = true;
        $form["fields"][0]["validation_message"] = "This field is invalid!";
    
        // update the form in the validation result with the form object you modified
        $validation_result["form"] = $form;
    
        return $validation_result;
    
    }
    Posted 10 years ago on Saturday November 13, 2010 | Permalink
  6. Anonymous
    Unregistered

    Hi Alex, I have a similar question/issue. My client wants us to validate the email field to ensure it's not coming from any free email hosts (they want commercial entities only). Here's their original code snippet for this validation... my question is how do I incorporate this into GF?

    Validation code snippet can be found here:
    http://dl.dropbox.com/u/19790225/email_form_validation.php

    Thanks,

    Sean

    Posted 10 years ago on Thursday July 28, 2011 | Permalink