Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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){
           boolean IsValidData=true;
           String _strServer=((EditText)findViewById(R.id.txtserver)).getText().toString().trim();
       }
       //it is IP
       else{
           Toast.makeText(getBaseContext(), "Please Check Your IP Format: ", Toast.LENGTH_SHORT).show();
       }
       //it is not IP


   }
   public void loginbuttonclick(View v)
   {
       ProgressDialog pDialog = new ProgressDialog(LoginActivity.this);
       // Set progressbar title
       pDialog.setTitle("Authentication");
       // Set progressbar message
       pDialog.setMessage("Authenticating...");
       pDialog.setIndeterminate(false);
       pDialog.setCancelable(false);
       // Show progressbar
       pDialog.show();
       try
       {
           //Validate input data
           boolean IsValidData=true;
           String _strServer=((EditText)findViewById(R.id.txtserver)).getText().toString().trim();; //"192.168.0.172";
           String _strUser=((EditText)findViewById(R.id.txtuser)).getText().toString().trim();;//"admin";
           String _strPass=((EditText)findViewById(R.id.txtpass)).getText().toString();//"admin";

           if(_strServer==null || _strServer.isEmpty() || _strServer=="")
           {
               IsValidData=false;
               pDialog.dismiss();
               Toast.makeText(getBaseContext(),"Kindly enter valid server ip! : ",Toast.LENGTH_SHORT).show();
           }


What I have tried:

For null and Empty I have done, so How do I put checkIP? please help..
Posted
Updated 27-Sep-17 19:22pm
Comments
Richard Deeming 28-Sep-17 12:04pm    
Why are you limiting your users to IPv4 addresses, when IPv6[^] has been "nearly ready" for a couple of decades? :)

Would it not be easier to use IPAddressUtil[^] to test whether the string is a valid IPv4 or IPv6 address?

1 solution

I would say use this regular expression to make sure it's in the proper format
([0-9]{1,3}\.){3}[0-9]{1,3}
and then
split it on '.', parse it to an integer and check to see whether it lies in range (0-255) or not
 
Share this answer
 
v3
Comments
Member 13233111 28-Sep-17 1:48am    
Can you please provide me code on my given sample?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900