We have a React form that has input field, user suppose to enter IP address with / symbol port number
Ex: 192.168.0.2/30
I have seen many answers related to ":" syntax but no in this syntax, Need your help to sorted this out on React project Thanks in advance
You can try the following regex expression that fits your case:
const expression = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/([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])$" const ip_port = "192.168.0.2/65535" const matches = ip_port.match(expression); console.log("Matches",matches[0]);
Though I would suggest if possible to have two input fields one for IP address and the second for the port number, this way you can validate each field by itself. This way you can use a is-ip package to validate the ip and you can use a simple regex ^([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])$
to valide the port number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.