View difference between Paste ID: iFZKhM6B and LyKCMYbB
SHOW: | | - or go back to the newest paste.
1
<?php
2
// http://www.gravityhelp.com/forums/topic/custom-input-mask-for-a-valid-url#post-157749
3-
// change the 338 to your form ID
3+
// revised to handle multiple forms and their associated URL fields
4-
add_filter('gform_pre_submission_filter_338', 'force_http');
4+
add_filter('gform_pre_submission_filter_338', 'force_http_prefix');
5-
function force_http($form) {
5+
function force_http_prefix($form) {
6
    $formfields = array(
7-
        // change the 3 to your field ID where the URL will be entered
7+
        // add all your form IDs here with their corresponding field ID
8-
        // change it a few lines further down as well
8+
        // for example, this will look for field 3 in form 338
9-
        // trim the whitespace first, then make it lowercase
9+
        '338' => '3',
10-
        $haystack = strtolower(trim($_POST['input_3']));
10+
        '12' => '3',
11
        '2' => '10',
12
        '8' => '7'
13
        // for all 11 forms ...
14
        );
15
16-
        if (substr($haystack, 0, 7) == $needle) {
16+
    // see if the form ID is one of the keys in our array
17-
                return $form;
17+
    // if so, then run the code to check the appropriate field
18
    if (array_key_exists($form['id'], $formfields)) {
19-
        else {
19+
20-
                $_POST['input_3'] = $needle . $haystack;
20+
        // create a variable for the input we want to modify for this form
21-
                return $form;
21+
        $input_id = 'input_' . $formfields[$form['id']];
22
        $haystack = strtolower(trim($_POST[$input_id]));
23
24
        // check for http:// at the beginning of the string
25
        $needle = 'http://';
26
27
        // if the http:// is not at the beginning of the string, add it and modify the POST value
28
        if (substr($haystack, 0, 7) <> $needle) {
29
            $_POST[$input_id] = $needle . $haystack;
30
        }
31
    }
32
    // return the form either way
33
    return $form;
34
}