Stefan83 Posted July 3, 2014 Share Posted July 3, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/289416-max-characters-in-field/ Share on other sites More sharing options...
TrickyInt Posted July 3, 2014 Share Posted July 3, 2014 Instead of using strlen() you should use isset() because it's way faster. Use it like this: if(isset($var[11)) { echo 'String is more that 10 chars'; } Quote Link to comment https://forums.phpfreaks.com/topic/289416-max-characters-in-field/#findComment-1483683 Share on other sites More sharing options...
Jacques1 Posted July 3, 2014 Share Posted July 3, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289416-max-characters-in-field/#findComment-1483689 Share on other sites More sharing options...
Solution Stefan83 Posted July 4, 2014 Author Solution Share Posted July 4, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/289416-max-characters-in-field/#findComment-1483830 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.