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.

Number Format Mask

  1. thehorse
    Member

    I am trying to validate that a number is a social security number.

    Can REGEX expressions be used in a number field, much like the built-in phone number field works? Or better yet- a built-in SSN field that validates a) 9 digit number in the format of 3-2-4 and b) that it is actually a VALID 9 digit SSN (a la http://www.ssa.gov/employer/highgroup.txt and jrez.com's function that validates the SSN usinga GNU function)
    //.

    I have started to build a validated function for this, but I find that I am using the field so often on so many forms, it is becoming cumbersome customizing for which field that function fires

    Here is my function, but it was starting to get out of control"

    //add_filter('gform_validation', 'valid_ssn');
    function valid_ssn($validation_result){
        $form = $validation_result["form"];
        // if not form ID 2, return result unmodified
        if($form["id"] != 2)
            return $validation_result;
    
        if (isset($_POST['input_3'])){
          if (  !isValidSSN($_POST['input_3'])){
            // 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"][2]["failed_validation"] = true;
            $form["fields"][2]["validation_message"] = "This is an invalid Social Security Number. Please check.";
    
            // update the form in the validation result with the form object you modified
            $validation_result["form"] = $form;
    
            return $validation_result;
    
          }else {
            $validation_result["is_valid"] = true;
            $form = $validation_result["form"];
            //return $validation_result;
          }
        }
    Posted 13 years ago on Tuesday January 25, 2011 | Permalink
  2. Assuming that your isValidSSN() function works, here is some updated code that will require a little less code to do the same thing:

    http://pastie.org/1500617

    There is one catch though... instead of specify the ID of each field and each form in the code, you'll need to add a classs (via the GF form admin) to each field that should be validated as a SSN. You'll see what I mean when you check out the code. :)

    Posted 13 years ago on Wednesday January 26, 2011 | Permalink