Dragon
Dragon

Reputation: 2481

JavaScript: FQDN regexp doesn't validate long names

I have a regexp that validates a string to be an FQDN:

var fqdnRegExp = new RegExp("^([a-z0-9]+\\.)?[a-z0-9][a-z0-9-]*\\.[a-z]{2,6}$");

It validates correctly such names as: google.com, mypage.mycompany.com. But when subdomain name is of 3d+ lvl, then validation crashes. For example, test.test2.test3.com hasn't been validated yet.

What is wrong with this regexp?

Upvotes: 1

Views: 573

Answers (1)

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

Just allow the central pattern ([a-z0-9][a-z0-9-]*\\.) to appear more than once :

var fqdnRegExp = new RegExp("^([a-z0-9]+\\.)?([a-z0-9][a-z0-9-]*\\.)+[a-z]{2,6}$");

Upvotes: 3

Related Questions