Jump to content

max characters in field


Stefan83

Recommended Posts

Hi

 

I'm trying to limit the number of characters in a postcode field in wordpress gravity form. This is what I have so far but its not working.

add_filter("gform_field_validation_24_47", "custom_validation");
    function custom_validation($result, $value, $form, $field){
		
		if(!$result["is_valid"] && $result["message"] == "Enter full address."){
			$zip = $value["47.5"];
       
           if(strlen($zip) > 10)
{
               $result["is_valid"] = false;
               $result["message"] = "Max 10 characters";
           }
		}
       return $result;
    }

Can anyone tell me what the issue is?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/289416-max-characters-in-field/
Share on other sites

Bollocks.  ::)

 

Nano-optimizations like that are just plain silly and make the code absolutely unreadable. As soon as you work in a team or publish the code, people will hate you for “great ideas” like that.

 

If you have trouble with the performance of your application, it's not because of strlen().

 

 

 


Can anyone tell me what the issue is?

 

The issue is that you haven't said anything about your problem. No, “doesn't work” is not a sufficient error description.

Thanks for the replies, managed to solve it in the end. Here's my final code if anyone interested

// Added custom validation for maximum characters count
    add_filter("gform_field_validation_24_47", "validate_chars_count", 10, 4);
	
    function validate_chars_count($result, $value, $form, $field){
		$street = $value["47.1"];
		$city = $value["47.3"];
		$country = $value["47.6"];
		$zip = $value["47.5"];
		if (empty($street) && empty($city) && empty($country)){
            $result["is_valid"] = false;
	            $result["message"] = "This field is required. Please enter your full address.";
	        }
if (strlen($zip) > 20) { // Maximum number of characters
    $result["is_valid"] = false;
    $result["message"] = "Post Code must be no more than 20 characters.";
}
return $result;
    
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.