android小知识之判断邮箱地址是否合法

  正确的邮箱格式如下,
 private static final String reg = "\\w+[\\w]*@[\\w]+\\.[\\w]+$";

但是并不代表一定是以".com"结尾,有可能在reg中包含多个如".comxx"非法格式,在此基础上还要加上一个结尾判断。整个代码如下:

private EditView autoview;

private static final String reg = "\\w+[\\w]*@[\\w]+\\.[\\w]+$";

str_email = et_autoview.getText().toString();

  if (str_email.matches(reg)&&str_email.endsWith(".com")) {

//合法
   btn_save.setBackgroundResource(R.drawable.bg_favtag_confirm_normal);
   btn_save.setEnabled(true);
  } else {

//非法
   btn_save.setBackgroundColor(Color.GRAY);
   btn_save.setEnabled(false);
  }

posted @ 2013-12-16 10:16  寡蛋  阅读(264)  评论(0)    收藏  举报