2

I want to have a validation function it must accept 3 types:

var
portvar
ipvar

one of my problems is in IPvar user input must be in this syntax as example:

[192.168.1.0/24,10.1.1.0/24]

how can I accept just such Ips from textboxes?

Nickool
  • 3,662
  • 10
  • 42
  • 72

1 Answers1

1

You could check it against a regular expression like this:

var textVal = ...;
if ((/^\[(?!,)(,?(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/[1-9]\d*)+\]$/).test(textVal)) {
  alert('Valid!');
}
else {
  alert('Invalid!');
}

The regular expression identifies valid IP's with each part of the IP being a number between 0 and 255. Additionally, as your example shows, each IP must be followed by a single / and then a number representing the subnet mask. Lastly, multiple IPs are separated by commas (however the regular expression does not allow a comma at the very beginning or the very end).

(By the way, the second IP address in your example isn't valid).

jerluc
  • 4,186
  • 2
  • 25
  • 44