Reputation: 13492
I am using below regular expression pattern
pattern="^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$|\s*
It will accept any one of the below ip address
1.1.2.3 or 192.122.134.1 or 198.23.45.56
But i want it should accept single as well as ip addresses with comma separated as well like below
1.1.2.3,192.122.134.1,198.23.45.56
What changes i have to do in my regular expression?
Upvotes: 0
Views: 6129
Reputation: 1642
<input type="text" name="country_code" pattern="^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))(,(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])))*$">
Use this input field in your HTML file
Upvotes: 0
Reputation: 2565
this should work
^((2([0-4]\d|5[0-5])|1\d\d|[0]?[1-9]\d|[0]?[0]?\d)(\.(2([0-4]\d|5[0-5])|1\d\d|
[0]?[1-9]\d|[0]?[0]?\d)){3}([\s,](?!$)|$))*$
it will accept comma separated and space separated ips
Upvotes: 0
Reputation: 326
([1-9]\d?\d?(\.\d{1,3}){2}\.\d{1,3},?)+
A little over Reductionist perhaps, but that should handle it. (you will need to trim out the commas from the matches though) Edited to restrict matches to the range of 1...* -> 999...*
Upvotes: 1
Reputation: 12122
It slowly becomes incomprehensible, but here you are:
^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))(,(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])))*$
What happens here is:
let's call your IP regexp IP:
IP = ((\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])).(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])))
so we just have to repeat it with comma:
^IP(,IP)*$
Upvotes: 5
Reputation: 7347
I´d rather not use regex as @biziclop did say.
You could just split the whole String
and pass the ip adress to the InetAddress.html#getByName method. from the documentation of the method:
If a literal IP address is supplied, only the validity of the address format is checked
Basicly you could just pass the adress to this method, and the class itself would supply a validation of the adress. If the addres would be invalid you would run into a java.net.UnknownHostException
which you would just have to catch. This way you would just have to create a regex that will successfully split all the ip-adresses
public static void main(String[] args) throws UnknownHostException {
String adresses = "1.1.2.3.15,192.122.134.1,198.23.45.56";
for(String s : adresses.split(",|\\sor\\s")) {
try {
InetAddress adress = Inet4Address.getByName(s);
} catch (UnknownHostException e) {
System.out.println("Invalid format for ip adress " + s);
}
}
adresses = "1.1.2.3 or 192.122.134.1 or 198.23.45.56 ";
for(String s : adresses.split(",|\\sor\\s")) {
try {
InetAddress adress = Inet4Address.getByName(s);
} catch (UnknownHostException e) {
System.out.println("Invalid format for ip adress " + s);
}
}
}
output:
Invalid format for ip adress 1.1.2.3.15
Upvotes: 0