Validation in EditText allows IP or Web site URLs

I need to give confirmation in my EditText so that it allows me to enter a valid

format of the ip address (?.?.?.?), for example, example 132.0.25.225

or

web url format (www.?.?), e.g. example www.example.com

the logic is that if the user enters any numerical value first, then the check (IP) will perform the action

else the user must write "www" before any web line

Note : it should execute onKeyListener () from EditText, I mean when the user enters input

In short, I will not check when the user enters input, and click OK

Any idea appreciated, thanks.

+6
source share
6 answers

f

private static final String PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; public static boolean validate(final String ip){ Pattern pattern = Pattern.compile(PATTERN); Matcher matcher = pattern.matcher(ip); return matcher.matches(); } 

URL

 try { new java.net.URI(url); } catch(MalformedURLException e) { // url badly formed } 
+12
source

try it.

 public void checkIP(String Value) { Pattern pattern = Pattern.compile("[0-255].[0-255].[0-255].[0-255]"); Matcher matcher = pattern.matcher(Value); boolean IPcheck = matcher.matches(); if(IPcheck) //it is IP else //it is not IP } 
+1
source

This ion works fine for me for checking url in android

  if (!URLUtil.isValidUrl(url)) { Toast.makeText(this, "Invalid URL", Toast.LENGTH_SHORT).show(); return; } 
+1
source

Add a TextWatcher to your EditText and to

 afterTextChanged (Editable s) 

Confirm your entry with a regular expression, and if the input character does not match the regular expression, just delete it using the following method.

 Editable.delete(int start, int end) 
0
source

Here's another solution for checking the IP address, but which can be used as a link also for checking the web url.

 EditText ipText = (EditText) findViewById(R.id.ip_address); ipText.setKeyListener(IPAddressKeyListener.getInstance()); 

.........

 public class IPAddressKeyListener extends NumberKeyListener { private char[] mAccepted; private static IPAddressKeyListener sInstance; @Override protected char[] getAcceptedChars() { return mAccepted; } /** * The characters that are used. * * @see KeyEvent#getMatch * @see #getAcceptedChars */ private static final char[] CHARACTERS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' }; private IPAddressKeyListener() { mAccepted = CHARACTERS; } /** * Returns a IPAddressKeyListener that accepts the digits 0 through 9, plus * the dot character, subject to IP address rules: the first character has * to be a digit, and no more than 3 dots are allowed. */ public static IPAddressKeyListener getInstance() { if (sInstance != null) return sInstance; sInstance = new IPAddressKeyListener(); return sInstance; } /** * Display a number-only soft keyboard. */ public int getInputType() { return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL; } /** * Filter out unacceptable dot characters. */ @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (end > start) { String destTxt = dest.toString(); String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend); if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) { return ""; } else { String[] splits = resultingTxt.split("\\."); for (int i = 0; i < splits.length; i++) { if (Integer.valueOf(splits[i]) > 255) { return ""; } } } } return null; } 

}

0
source

should use the java.net.InetAddress class. You can check all forms of IP address: host address (for example: 132.0.25.225) or host name (for example: www.google.com); The IPv4 or IPv6 format is fine.

The source code must run in the workflow because sometimes InetAddress.getAllByName (mStringHost) takes a lot of time. for example: you get the address on behalf of the host.

 Thread mThread = new Thread(new Runnable() { @Override public void run() { try { String mStringHost = "www.google.com"; //String mStringHost = "192.168.1.2"; InetAddress[] list_address = InetAddress.getAllByName(mStringHost); if(list_address != null && list_address.length >=1){ InetAddress inetAddress = list_address[0]; Log.d("test","inetAddress ="+ inetAddress.getHostAddress()); if( inetAddress instanceof Inet4Address){ //IPv4 process }else{ //IPv6 process } }else{ throw new Exception("invalid address"); } }catch(Exception e){ Log.e(TAG,"other exception",e); Toast.makeText(context, "Invalid host address or host name", Toast.LENGTH_SHORT).show(); //process invalide ip address here } } }); mThread.start() 
0
source

Source: https://habr.com/ru/post/897592/


All Articles