Checking If String Is Web Address Or Ip On Android
I need to validate if string entered in TextEdit is a web address eg. 'www.stackoverflow.com' or an ip address eg. '64.34.119.12'. I have tried this two methods without success. I
Solution 1:
Short answer: Try using regex!
EDIT:
if(textEdit1.getText().matches(REGEX_URL)) {
//DO URL THINGS
}
if(textEdit1.getText().matches(REGEX_IPADDRES)) {
//DO IP THINGS
}
If you google you can find the correct REGEX strings for IP addresses and urls...
NOTE: A regex for urls can be different for what you want, do you only want http:// https:// or all valid urls (like market://)...
Solution 2:
Patterns.IP_ADDRESS.matcher(url).matches();
Solution 3:
Validation of IP Address in Kotlin which returns true or false
funisValidIPAddress(ip:String):Boolean {
val reg0To255 = ("(\\d{1,2}|(0|1)\\" + "d{2}|2[0-4]\\d|25[0-5])").toRegex()
return reg0To255.matches(ip)
}
val inputIP = "127.1.1.775"
println("Input: " + inputIP)
println("Output: " + isValidIPAddress(inputIP))
Input: 127.1.1.775 Output: false
Solution 4:
how about simpler approach? detect if it is IP address, e.g.
publicstaticbooleanisIP(String input) {
if (input.contains(".") && input.length()>1) {
returnTextUtils.isDigitsOnly( input.replace(".", "").trim() );
}
else {
returnfalse;
}
}
Post a Comment for "Checking If String Is Web Address Or Ip On Android"