Back to all patterns
Network Port Number Validation
Web & Network
Validates TCP/UDP port numbers in the valid range 1-65535.
/^([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/How it works
Valid port numbers range from 1 to 65535. Port 0 is reserved. Ports 1-1023 are well-known ports requiring root/admin privileges. Ports 1024-49151 are registered ports. Ports 49152-65535 are dynamic/ephemeral ports.
Test Cases
Should Match
- 80
- 443
- 8080
- 65535
Should NOT Match
- 0
- 65536
- 99999
- -1
Quick Summary
Validates TCP/UDP port numbers in the range 1-65535. Port 0 is excluded (reserved). Ports 1-1023 require elevated privileges. Use this before binding or connecting to a port.
Key Takeaways
Key Takeaways
- Valid range: 1-65535 (port 0 is reserved and excluded)
- Well-known ports (1-1023): HTTP=80, HTTPS=443, SSH=22, DNS=53
- Registered ports (1024-49151): application-specific
- Ephemeral ports (49152-65535): dynamically assigned by the OS
Use Cases
When to use it
- Validating port number inputs in server configuration forms
- Checking port values in Docker or Kubernetes port mappings
- Parsing port numbers from connection strings
Watch out
Common Mistakes
- Allowing port 0, it is reserved and cannot be bound to directly
- Not checking if the port requires elevated privileges (< 1024) on Linux/macOS
FAQ
Network Port Number Validation, Frequently Asked
What ports are commonly used in web development?
3000 (React dev), 4200 (Angular), 5173 (Vite), 8080 (generic HTTP), 5432 (PostgreSQL), 3306 (MySQL), 6379 (Redis).
How do I check if a port is in use?
Use lsof -i :PORT on macOS/Linux or netstat -ano | findstr :PORT on Windows.