The isPort
function checks if the input value represents a valid port number. It returns true
if the value is a valid port number (within the range 1 to 65535), andfalse
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isPort } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isPort } = require('multiform-validator');
The function takes one parameter that can be either a string or a number:
value
(string | number) - The value to be checked if it represents a valid port number.// Example 1 - Valid port numbers
const result1 = isPort('80'); // true
console.log(result1);
const result2 = isPort(443); // true
console.log(result2);
const result3 = isPort('65535'); // true
console.log(result3);
// Example 2 - Not valid port numbers
const result4 = isPort(0); // false (port number must be greater than 0)
console.log(result4);
const result5 = isPort('65536'); // false (port number must be less than or equal to 65535)
console.log(result5);
// Example 3 - Not a valid input value
const result6 = isPort('Hello'); // false (not a valid port number)
console.log(result6);
The function first checks if the input value is either a string or a number. If the value is a string, it is converted to an integer using parseInt(value, 10)
. If the conversion is successful (resulting in a valid integer), the function checks if the integer is within the valid port range (1 to 65535). If the value is within this range and it is an integer, the function returns true, indicating that the input value represents a valid port number. Otherwise, it returns false.