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?

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

6 Comments

after / isn't it a subnet mask?
if I don't put comma again it will tell valid
would you please tell me what are these?(/^[(?!,)
/^\[(?!,) means the line must begin with a bracket and should not be immediately followed by a comma.
I got it thanks jerluc, can you tell me 25[0-5] means 250 till 255? what is {3}? and the sign =>:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.