Android 手机号,密码正则验证
*accont—->手机号 以1开头,10位结尾。哈哈,还有其他各种写法,我这里使用了简单的。
*password—->密码 6到18位的字母大小写及数字下划线。
参考菜鸟教程Java正则
public class ppp {
public static boolean isMobileNO(String accont) {
Pattern p = Pattern.compile("^1\\d{10}$");
Matcher m = p.matcher(username);
return m.matches();
}
public static boolean isPasswordNO(String password) {
Pattern p = Pattern.compile("[0-9a-zA-Z_]{6,18}");
Matcher m = p.matcher(password);
return m.matches();
}
}
请求这里我是这样判断的
private void initView() {
mBtnLogin = (TextView) findViewById(R.id.main_btn_login);
user_username = (EditText) findViewById(R.id.username);
user_password = (EditText) findViewById(R.id.password);
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
account = user_username.getText().toString();
password = user_password.getText().toString();
if (TextUtils.isEmpty(account)) {
Toast.makeText(getApplicationContext(),"请输入账号",Toast.LENGTH_SHORT).show();
return;
}
if (!ppp.isMobileNO(account)) {
Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
if (!ppp.isPasswordNO(password)) {
user_password.setText(R.string.pwd_wrong);
Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
return;
}
// requestData();
}
});
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/login_bg">
<LinearLayout
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="65dip"
android:layout_height="65dip"
android:layout_gravity="center_horizontal"
android:src="@mipmap/oa" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:gravity="center"
android:text="Excelled"
android:textColor="#54ec97"
android:textSize="24sp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Fly away from here"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:orientation="vertical"
android:padding="10dip" >
<LinearLayout
android:id="@+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="36dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@drawable/radius_drawable_bg">
<ImageView
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/login_paw_code" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:background="#00000000"
android:hint="手机号"
android:padding="5dip"
android:maxLength="11"
android:digits="1234567890"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="5dip"
android:layout_marginTop="15dip"
android:background="#eeeeee" />
<LinearLayout
android:layout_marginTop="10dp"
android:background="@drawable/radius_drawable_bg"
android:id="@+id/input_layout_psw"
android:layout_width="match_parent"
android:layout_height="36dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/login_paw_left" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:background="#00000000"
android:hint="密码六位数以上字母组合"
android:inputType="textPassword"
android:padding="5dip"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="@+id/main_btn_login"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_marginTop="30dip"
android:background="@drawable/button_radius30_lightblue"
android:gravity="center"
android:text="登录"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center|right"
android:text="忘记密码?"
android:textSize="16sp"
android:textColor="#fcfcfc"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text=".@2017上海xxxxx有限公司"
android:textSize="12sp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>