Jump to content

max characters in field


Go to solution Solved by 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.

  • Solution

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;
    
	}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.