EasyPythonJavaScriptTypeScript

Parse Port

Node.jsValidationNumbers

Validate and parse a port string. Returns the port number if valid (integer
between 1 and 65535), else null.

Reject: empty, non-numeric, non-integer, out of range, leading/trailing whitespace.

Sample tests

Test #1Valid port
Input: ["8080"]
Output: 8080
Test #2Max port
Input: ["65535"]
Output: 65535
Test #3Zero invalid
Input: ["0"]
Output: null
Test #4Out of range
Input: ["65536"]
Output: null
Test #5Leading space invalid
Input: [" 80"]
Output: null