11
votes

I'm using this regex (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3} to validate port numbers. Somehow this is not working. What is wrong with this? Can anybody point me out.

13
What are you trying to do? Maybe post some unit test code that is failing or something? - Bob Kuhar
what port numbers do you want 2 validate..are they in specific range or do u want it 2 just validate - Anirudha
@Fake.It.Til.U.Make.It I just want to validate from 1 to 65535. - Soham Dasgupta
@SohamDasgupta no need to use regex here..just parse the number to int and then check if it is in required range! - Anirudha

13 Answers

47
votes

What exactly do you mean by not working?

You could try something like so: ^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$ (obtained from here).

This will make sure that any given string is numeric and between the range of 0 and 65535.

Assuming your regular expression matches the same range, it is missing the start and end anchors (^ and $ respectively), so it would allow other strings besides the actual port.

11
votes

When, we search "how to validate port number" on Google we unfortunately land here

However (except if you have really no other choice...),

Regex is clearly not the way to validate a port number !

"One" (slightly better) way may be:

step 1: Convert your string into number, and return FALSE if it fails
step 2: return TRUE if your number is in [1-65535] range, and FALSE otherwise

Various reasons, why Regex is not the right way ?

  • Code readability (would takes few minutes to understand)
  • Code robustness (there are various ways to introduce a typo, a unitary test would be required)
  • Code flexibility (what if port number can be extended to a 64-bits number !?)
  • etc. ...
3
votes

Number() is the function you want "123a" returns NAN

parseInt() truncates trailing letters "123a" returns 123

<input type="text" id="txtFld" onblur="if(Number(this.value)>0 && Number(this.value)<65536){alert('valid port number');}" />
3
votes

Here is the example I'm using to validate port settings for a firewall. The original answer will match 2 strings. I can only have 1 string match.

(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[1-9](\d){0,3})

To get: 22,24:100,333,678,100:65535 my full validation (That will only return 1 match) is

(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3})(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?(,(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}){1}(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?)*
2
votes

A more strict approach is to have a regex matching all numbers up to 5 digits with the following string:

*(^[1-9]{1}$|^[0-9]{2,4}$|^[0-9]{3,4}$|^[1-5]{1}[0-9]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-4]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-4]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-5]{1}[0-3]{1}[0-5]{1}$)*
2
votes

The accpeted answer by npinti is not right. It will not allow to enter port number 1000, for example. For me, this one (not nice, I'm a beginner) works correctly:

/^((((([1-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9][0-9][0-9])|([1-6][0-5][0-5][0-3][0-5])))))$/

2
votes
"^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$"

It will allow everything between 0-65535 inclusive.

2
votes

Here is single port regex validation that excludes ports that start with 0

^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])


Here is validation for port range (ex. 1111-1111)

^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])(-([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$


link:

https://github.com/findhit/proxywrap/issues/13

1
votes

@npinti 's answer allows leading zeros in the port number and also port 0 means pick any available port so I would exclude that so the regex becomes

^([1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$

If you want to allow port 0 then

^(0|[1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
1
votes

The solution:

Dim Minlenght As Integer = 1
Dim Maxlenght As Integer = 65536

Regex.IsMatch(sInput,"(^\d{0},{1}$)", "{" + Minlenght, Maxlenght + "}")
1
votes

Landed here as well, searching specifically for REGEX to validate port number. I see the approved solution was not fixed yet to cover all scenarios ( eg: 007 port, and others ) and solutions from other sites not updated either (eg).

Reached same minimal solution as saber tabatabaee yazdi, that should cover the 1-65535 range properly:

^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$

Enjoy !

1
votes
^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0][0-9]{1,4})|([0-9]{1,4}))$

I have tested above regrex with Junit run the for loop from 0-65535

Ex: 00001 - 65535 with leading Zeros 1 - 65535 without leading Zeros Ex:====

  1. (6553[0-5]) : 65530-65535
  2. (655[0-2][0-9]) : 65500-65529
  3. (65[0-4][0-9]{2}): 65000-65499
  4. (6[0-4][0-9]{3}) : 60000-64999
  5. ([1-5][0-9]{4}) : 10000-59999
  6. ([0-5]{0,5}) : 00000-55555 (for leading Zeros)
  7. ([0][0-9]{1,4}) : 00000-09999 (for leading Zeros)
  8. ([0-9]{1,4}) : 0000-9999 (for leading Zeros)
0
votes

If variable is an integer between 1 and 65536 (inclusive) then...

if [[ "$port" =~ ^[0-9]+$ && $port -ge 1 && $port -le 65536 ]]; then