0

I'm looking for a correct regex expression that Facebook uses to validate entered Website URL or Display link when user wants to create an ad in Ads Manager (you will see the mentioned input fields on attached screenshot below).

enter image description here

I need that because platform we're developing uses Facebook Marketing API to create ads on Facebook. So in order not to get API request failure from Facebook I would like to perform a validation before we fire a request to the API. I've been using a few different regexes so far to perform validation but the issue comes back again and again. That's the reason why I come up with question to you.

So my question is, does anyone here used some url validation regex that works well with Facebook? I couldn't find anything on Facebook docs related to this validation, so any help from your side is welcome.

Here is the list I've been using to validate urls https://pastebin.com/0zU6MSme

@update I moved the list to the pastebin because previous link didn't work

The original list I took from here: Original URL List

Grzegorz Pasnik
  • 164
  • 2
  • 7
  • Where can I find that web page you're creating facebook ads from? The validation is possibly visible in the JavaScript. – Tom Lord Feb 01 '18 at 12:21
  • ...Beyond that, you probably won't be able to get an answer from random people on the internet! If there's no documentation for this, then I won't know the answer either. You can try contacting Facebook's development team? It's possible that they don't even *use* a regex; perhaps it actually tries to load the URL, to see if it's valid? – Tom Lord Feb 01 '18 at 12:27
  • What is the actual issue, which kinds of URLs cause errors? – CBroe Feb 01 '18 at 12:38
  • @TomLord If you look for a help on facebook developers and it's implementation issue, they direct you here to stackoverflow The web page I showed on the screenshot is actually Facebook's Power Editor: https://www.facebook.com/ads/manage/powereditor – Grzegorz Pasnik Feb 01 '18 at 13:20
  • @CBroe The issue is that I can't find regex that will work the same as one that Facebook uses on their Power Editor or Ads Manager. The expected results are here https://pastebin.com/0zU6MSme – Grzegorz Pasnik Feb 01 '18 at 13:22
  • @GrzegorzPasnik I still don't know how to reproduce your problem. Can you please provide reproduction steps on how to use that tool? I'm not familiar with it, and after 5 minutes of clicking around, I wasn't able to find that page your screenshot is taken from. – Tom Lord Feb 01 '18 at 15:26
  • Your question would get a better answer from some of the FB experts here if it was edited to be clearer, like by putting the most important parts of the pastebin link in the question itself. – Elliott Beach Feb 20 '18 at 03:27
  • I think this is a bug in FB. If most unicode urls match, then `http://➡.ws/䨹` and `http://☺.damowmow.com/` should as well. – Elliott Beach Feb 20 '18 at 03:28

1 Answers1

0

The issue you are experiencing with certain urls such as http://➡.ws/䨹 being unexpectedly rejected is due to internationalized domain names. Domain names need to be ASCII text, so you have to convert the domain to IDN form before sending it to Facebook (unicode query strings are OK). The browser does this automatically, but apparently Facebook doesn't.

There are libaries like punycode and encoding.py that you could use to convert the domain name to IDN before sending your request.

As for a regex, I would expect the one Django uses will work - Python - How to validate a url in python ? (Malformed or not), except you should disallow ftp, and apply the regex after converting to IDN.

Elliott Beach
  • 10,459
  • 9
  • 28
  • 41