Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Nick Ciske

    (@nickciske)

    See “other notes” for examples of custom validation filters. The one for limiting email domains would be a good starting point.

    Thread Starter hopptaustralia

    (@hopptaustralia)

    sorry i am not very good at coding but

    what is this line for?

    $domain = array_pop(explode(‘@’, $val));

    and i would like that if the country user type in australia then this validation will come out…

    I think I am doing it really wrong because when i tried on my website it goes true.. the validation doesnt work..

    add_filter('sfwp2l_validate_field','block_country_abbr', 10, 4);
    
    function block_country_abbr( $error, $name, /*$val, $field,*/ $country ){
    
        if( $name == 'State' & $country == 'Australia' ){
    
            $non_abbr = array( 'act', 'nsw', 'qld', 'sa', 'tas', 'vic,', 'wa', 'nt', 'NT', 'QLD', 'NSW', 'SA', 'ACT', 'TAS', 'VIC', 'WA' );
    
            //$domain = array_pop(explode('@', $val));
    
            if( in_array( /*$domain,*/ $non_abbr ) ){
                $error['valid'] = false;
                $error['message'] = 'Please enter Australian State in Full.';
            }
    
        }
    
        return $error;
    }

    I think i should put in the array the states spelt out in full instead?

    Thread Starter hopptaustralia

    (@hopptaustralia)

    I test it by putting states in abbr form and in full but it still goes through…. please help?

    Plugin Author Nick Ciske

    (@nickciske)

    Yeah, that’s not going to work…

    First off, you’d want a white list, not a black list.

    You can’t arbitrarily pass variables to a filter 😉

    Try this:

    add_filter('sfwp2l_validate_field','block_state_abbr', 10, 4);
    
    function block_state_abbr( $error, $name, $val, $field, ){
    
        if( strtolower( $name ) == 'state' && $_POST['country'] == 'Australia' ){
    
           $val = trim( ucwords( strtolower( $val ) ) );
    
            $states = array( 'New South Wales', 'Queensland', 'South Australia', 'Tasmania', 'Victoria', 'Western Australia' );
    
            if( ! in_array( $val, $states) ){
                $error['valid'] = false;
                $error['message'] = 'Please enter Australian State in Full.';
            }
    
        }
    
        return $error;
    }
    Thread Starter hopptaustralia

    (@hopptaustralia)

    you are a lifesaver Nick! initially i had another problem which is when people put the contry as australia instead of Australia .. the validation rule doesnt apply. but now i just put the countries in a drop down and it works! ‘

    though i still cant figure out how to make the country text insensitive of lower or upper case ….

    any hint?

    anyway thanks!

    Plugin Author Nick Ciske

    (@nickciske)

    strtower lower cases a string and allows you to make a case insensitive comparison. It’s already there in the code for field name.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘how to have a State validation rule for contact form?’ is closed to new replies.