验证手机号是否为空、位数是否正确以及是否有效

 private boolean judPhone() {
        EditText editText=findViewById(R.id.edit_number);
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            Toast.makeText(MainActivity.this, "请输入您的电话号码", Toast.LENGTH_LONG).show();
            editText.requestFocus();
            return false;
        } else if (editText.getText().toString().trim().length() != 11) {
            Toast.makeText(MainActivity.this, "您的电话号码位数不正确", Toast.LENGTH_LONG).show();
            editText.requestFocus();
            return false;
        } else {
            String phone_number = editText.getText().toString().trim();
            String num="^((13[0-9])|(15[^4,\\D])|(18[0,3,5-9]))\\d{8}$";
            if (phone_number.matches(num))
                return true;
            else {
                Toast.makeText(MainActivity.this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }

你可能感兴趣的:(验证手机号是否为空、位数是否正确以及是否有效)