如何在edittext上验证IP以完成此操作? [英] How do I complete this for validating IP on edittext?
本文介绍了如何在edittext上验证IP以完成此操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
}
我尝试过:
对于null和Empty我已经完成了,所以如何放置checkIP?请帮助..
What I have tried:
For null and Empty I have done, so How do I put checkIP? please help..
推荐答案
我想说使用这个正则表达式来确保它的格式正确
([0-9] {1,3} \。){3} [0-9] {1,3}
然后
将它拆分为'。',解析为一个整数并检查它是否在范围(0-255)或不在
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
这篇关于如何在edittext上验证IP以完成此操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文