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])$
6
  • Possible duplicate of How to check validation of IP Address in jquery Commented Jul 28, 2016 at 9:13
  • See this stackoverflow.com/questions/106179/… Commented Jul 28, 2016 at 9: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 Commented Jul 28, 2016 at 9:21
  • ipv4 or ipv6 ip addresses ? ::2:3:4:5 is a valid ipv6 address (0000:0000:0000:0000:0002:0003:0004:0005) Commented Jul 28, 2016 at 10:24
  • ipv4 for the moment. This may be the case but my application will not accept this kind of notation Commented Jul 28, 2016 at 10:32

2 Answers 2

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])$/
Sign up to request clarification or add additional context in comments.

14 Comments

it does work for some hostnames, but it will also validate something like IP-REGEX or Sample
Which are valid hostnames, according to rfc1035, as is localhost ; what problem do you have with them?
Maybe would you want them to end with a 2-3 letters TLD?
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
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.
|
0

Based on the AAron solution, the regex with a comma separation:

/((((([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])(\/([4-9]|[12][0-9]|3[0-2]))?)([,\s]+|$))*)|(((([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])([,\s]+|$))*)/gm

Your opinion (see https://regex101.com/r/JVHcUm/1) ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.