user3620931
user3620931

Reputation: 131

Validation code for IDN (Domain) with regex (regular expression)

I want to check domains with regex. My old code was:

/^([a-z0-9]+([-a-z0-9]*[a-z0-9]+)?.){0,}([a-z0-9]+([-a-z0-9]*[a-z0-9]+)?){1,63}(.[a-z0-9]{2,7})+$/i

It is okey but this code doesn't validate IDNs (internationalized domain names) such as öü.com or öü.öü

My domain format is:

Besides, I don't want:

Important note: user can add the domains;

Upvotes: 2

Views: 2169

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99011

Idn's such as भारत.icom.museum use Punycode encoding, as defined in RFC 3492, before submission for DNS resolution.

It seems that you're using php, based on that, you should use the idn_to_ascii() function to convert the idn's, ex:

echo idn_to_ascii("भारत.icom.museum");
//xn--h2brj9c.icom.museum

Upvotes: 2

Helio Santos
Helio Santos

Reputation: 6815

You can add support for IDNs by replacing a-z by \pL

Upvotes: 3

Related Questions