Home Forums General Issues URL Field Validation


URL Field Validation

  • Hi,

    Is there any way that I can make the basic URL field accept invalid URLs? I need it for testing purposes and dummy links. Thanks.

  • any reason you couldn’t just use a ‘text’ field and change it to a URL field when your testing is done?

  • Normally there wouldn’t be, but the site I need to know this for wasn’t built by me, and uses the URL field in, oh, around 30 places. So instead of replacing all of those, I’d prefer to just find away to strip away the validation from the field.

  • ACF doesn’t check the URLs to see if they are real, it just check them to see if they either contain ‘://’ or start with ‘//’, so you can put anything in there that looks like a url like

    If you mean you need to override the checking and allow content that does not look like a link then, it might be possible. You would need to remove the filters that acf add, and that could be difficult.

    This is a guess, so it might not work. The guess is the first part of the array, figuring that out is where you might have a problem that that’s my best guess at it.

    remove_filter('acf/validate_value/type=url', array(acf()->fields, 'validate_value'), 10,);
  • Hello,

    I just faced a case like this and this worked with me

    add_filter('acf/validate_value/key=field_5d3c9ebc8acdd', 'disable_field_validation', 999, 4);
    // you can change key with name or type=url.
    function teasdasdad($valid, $value, $field, $input ) {
        // Also you can write your own validation here.
        return true;
  • I’m trying to do something somewhat similar to this. I don’t want to allow invalid URLs, but I’d like to be able to allow internal links within the same page without having to include the full URL. In other words, I’d like the validation to pass if the URL begins with #.

    I’m trying to use add_filter('acf/validate_value/type=url') to apply the following logic:

    if ((!$valid || strlen($valid) > 1) && strpos($value,'#') === 0) { return true; }

    I find that if I do this, it changes the validation error message from “Value must be a valid URL” to “Enter a URL”… but it’s still failing to validate.

    I find the whole way this filter works a bit confusing, as $valid can be one of three values: false meaning the data is invalid; true meaning it’s valid; or an error message string (which would evaluate to true) if it’s invalid.

    Besides that… I would think I’m not alone in wanting/needing in-page anchor links to validate in a URL field. Yes, I could change this to a regular text field, but I want URL validation on the input… I just want to also be able to allow in-page anchor links to pass validation.

    What am I missing?

    (Edit: Strangely, I did a scan of the entire codebase of ACF PRO and I could not find a single instance of the phrase “Enter a URL” anywhere. So I really don’t have a clue where this extra validation step is coming from.)

  • Follow-up on my last post and the mysterious “Enter a URL” message. I scanned my entire codebase and could not find that phrase anywhere, so on a hunch I investigated whether it might be a browser-specific issue, and it appears to be. I’m getting the “Enter a URL” error that is preventing saving in Safari; but if I edit the exact same page in Chrome, it works. Then if I open the page — now containing my # URL — in Safari and try to save without any changes, it fails with the same “Enter a URL” message again.

    It seems to be a browser-specific handling of the new HTML5 <input type="url"> field. Is there a way to customize the pattern attribute for URL fields in ACF? If so I haven’t seen it anywhere.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘URL Field Validation’ is closed to new replies.