Home Forums General Issues URL Field Validation Reply To: URL Field Validation

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