3

I'm trying to find a regex to validate IP-Addresses and one for Hostnames in Javascript.

I looked at many posts here and elsewhere but cannot quite find one that suits my needs.

For the IP I found two that work fine (dont know if there are differences other than the format):

1: (this is my preferred regex for IP-Addresses)

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

2:

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

For the Hostname I found this one:

/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/

which also works fine.

BUT^^ the problem is that the hostname regex will validate

192.168.178.1111

This is not a hostname, this is an invalid IP-Address.

I would like to fit both hostname & IP regex together in a single regex term but since the hostname regex will validate any non-valid IP-Address I cannot combine them.

Does anyone have an idea on how to create a hostname regex that will not validate an invalid IP-Address?

EDIT: I found this one as well: http://jsfiddle.net/opd1v7au/2/

but this will validate for example:

::2:3:4:5

which my application cannot accept.


Solution: thx to Aaron I have this regex for now which seems to work (in testing at the moment)

^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$

Combined version to validate IP-Addresses & Hostnames ->RegExr.com:

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$
Community
  • 1
  • 1
Alkahna
  • 441
  • 7
  • 21
  • Possible duplicate of [How to check validation of IP Address in jquery](http://stackoverflow.com/questions/17587994/how-to-check-validation-of-ip-address-in-jquery) – Ram Singh Jul 28 '16 at 09:13
  • See this http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address – Tony Vincent Jul 28 '16 at 09:13
  • those either work for IP only which I already have 2 working ones or do not work correctly on hostnames. Something like IP-Regex is shown as valid which is wrong. And you cannot combine IP & Hostname together to work correctly – Alkahna Jul 28 '16 at 09:21
  • ipv4 or ipv6 ip addresses ? `::2:3:4:5` is a valid ipv6 address (`0000:0000:0000:0000:0002:0003:0004:0005`) – HolyDanna Jul 28 '16 at 10:24
  • ipv4 for the moment. This may be the case but my application will not accept this kind of notation – Alkahna Jul 28 '16 at 10:32
  • I don't know why it shoud be [a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-] rather than [a-zA-Z0-9][a-zA-Z0-9\-]. – codexplorer Jun 13 '19 at 06:40

1 Answers1

3

Based on this SU answer, I propose this modification, which accepts labels starting with digits except for the top level domain :

/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/
Community
  • 1
  • 1
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • 1
    it does work for some hostnames, but it will also validate something like `IP-REGEX` or `Sample` – Alkahna Jul 28 '16 at 09:56
  • Which are valid hostnames, according to rfc1035, as is `localhost` ; what problem do you have with them? – Aaron Jul 28 '16 at 09:57
  • Maybe would you want them to end with a 2-3 letters TLD? – Aaron Jul 28 '16 at 09:59
  • they are valid? that seems odd. I would have expected there hast to be at least a dot in it like test.domain or test.tld or something like that – Alkahna Jul 28 '16 at 10:00
  • If you want at least 2 labels, change your first `*` into a `+`. It should be ok with "Internet domains" since they are all under TLDs. A well known example of 1-label domain name is `localhost`. – Aaron Jul 28 '16 at 10:03
  • the regex provided doesn't match 100.com, which is a valid hostname – enkryptor Jul 28 '16 at 10:05
  • I was about to say that `localhost` is the only 1 word domain that I would consider valid^^ I changed the **second** `*` into a `+` and now it seems to be working. Im going to run some tests with possible hostnames against it. – Alkahna Jul 28 '16 at 10:06
  • @Alkahna you can define custom domain names in your `/etc/host` file, and nothing restricts you from making them 1 word long. Your sysadmin could also expose some on your DNS server, although I don't think it's common practice – Aaron Jul 28 '16 at 10:08
  • @Alkahna I've edited the regex, looks like labels can start by digits, except when they're top domain. You should use the updated one – Aaron Jul 28 '16 at 10:16
  • I rather go with "do not start with digit" as this will not be the case for any hostname that will be used in my case ;) This way I can combine hostname & ip which is ideal – Alkahna Jul 28 '16 at 10:16
  • @Aaron I doubt Alkahna writes an rfc checker. I guess they want to check hostnames, that exist in reality. You still can distinguish them from invalid ip-addresses, since the root zone can't contain digits. – enkryptor Jul 28 '16 at 10:19
  • your updated regex term looks good. I did change the the second `*` into a `+` though to prevent hostnames like `sample` or `regex-test` to be valid – Alkahna Jul 28 '16 at 10:22
  • @enkryptor Weill hostnames should follow RFCs, just not the outdated one I was reading. Thanks for pointing out my shortcoming – Aaron Jul 28 '16 at 10:28
  • may it help you. https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address/3824105#3824105 – fahdshaykh Nov 01 '21 at 11:42