android账号密码长度限制,限制输入密码长度

1、代码实现"密码至少为9位,并需包含大写字母、小写字母、数字或特殊字符等三种"

返回0、1、2为格式不正确,返回4为密码格式正确

-(int)checkIsHaveNumAndLetter:(NSString*)password

{

//数字条件

NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

//符合数字条件的有几个字节

NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password

options:NSMatchingReportProgress

range:NSMakeRange(0, password.length)];

//英文字条件

NSRegularExpression *sLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字条件的有几个字节

NSUInteger sLetterMatchCount = [sLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

//英文字条件

NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字条件的有几个字节

NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

if (password.length < 9) {

// 密码长度不正确

return 0;

} else {

// 没有大写或小写

if (tLetterMatchCount == 0 || sLetterMatchCount == 0) {

return 1;

} else {

if (tNumMatchCount > 0) {

return 4;

} else{

if(tNumMatchCount + tLetterMatchCount + sLetterMatchCount < password.length){

return 4;

} else{

return 2;

}

}

}

}

}

需注意:NSRegularExpressionOptions,如果不区分大小写可以使用NSRegularExpressionCaseInsensitive

NSRegularExpressionCaseInsensitive              = 1 << 0,  // 不区分大小写的NSRegularExpressionAllowCommentsAndWhitespace  = 1 << 1,  // 忽略空格和# -NSRegularExpressionIgnoreMetacharacters      = 1 << 2,  // 整体化NSRegularExpressionDotMatchesLineSeparators  = 1 << 3,  // 匹配任何字符,包括行分隔符NSRegularExpressionAnchorsMatchLines                  = 1 << 4,  // 允许^和$在匹配的开始和结束行NSRegularExpressionUseUnixLineSeparators      = 1 << 5,  // (查找范围为整个的话无效)NSRegularExpressionUseUnicodeWordBoundaries  = 1 << 6    // (查找范围为整个的话无效)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了,让我来为您解答。 首先,您需要创建一个新的项目并在 Android Studio 中打开。接下来,您需要创建登录界面的布局,包括单选按钮、文本框、复选框等控件。 以下是一个基本的登录界面布局示例: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" android:textSize="24sp" android:gravity="center"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="16dp" android:paddingBottom="16dp"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号密码登录" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证码登录" /> </RadioGroup> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入账号(11位)" android:maxLines="1" android:inputType="number" android:maxLength="11"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入密码(6位)" android:maxLines="1" android:inputType="textPassword" android:maxLength="6"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入验证码" android:maxLines="1"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示密码"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout> </LinearLayout> ``` 以上是登录界面的基本布局,其中包含账号密码登录、验证码登录、输入账号密码输入验证码、记住密码、显示密码和登录等控件。 接下来,在 Java 中编写代码处理登录逻辑,包括单选按钮的选择、文本框输入数据的验证、记住密码的勾选、显示密码的切换等。以下是一个大致的登录逻辑: ```java public class LoginActivity extends AppCompatActivity { private RadioGroup mRadioGroup; private RadioButton mRadioButtonAccount, mRadioButtonCode; private EditText mEditTextAccount, mEditTextPassword, mEditTextCode; private CheckBox mCheckBoxRemember, mCheckBoxShow; private Button mButtonLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mRadioGroup = findViewById(R.id.radio_group); mRadioButtonAccount = findViewById(R.id.radio_button_account); mRadioButtonCode = findViewById(R.id.radio_button_code); mEditTextAccount = findViewById(R.id.edit_text_account); mEditTextPassword = findViewById(R.id.edit_text_password); mEditTextCode = findViewById(R.id.edit_text_code); mCheckBoxRemember = findViewById(R.id.check_box_remember); mCheckBoxShow = findViewById(R.id.check_box_show); mButtonLogin = findViewById(R.id.button_login); mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio_button_account: mEditTextCode.setVisibility(View.GONE); mEditTextPassword.setVisibility(View.VISIBLE); break; case R.id.radio_button_code: mEditTextPassword.setVisibility(View.GONE); mEditTextCode.setVisibility(View.VISIBLE); break; } } }); mButtonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = mEditTextAccount.getText().toString(); String password = mEditTextPassword.getText().toString(); String code = mEditTextCode.getText().toString(); if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password) || TextUtils.isEmpty(code)) { Toast.makeText(getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show(); return; } if (validated(account, password)) { if (mCheckBoxRemember.isChecked()) { // 记住密码 } // 跳转到主界面或者其他界面 } else { Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); mCheckBoxShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { mEditTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { mEditTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } }); } private boolean validated(String account, String password) { if (account.length() == 11 && password.length() == 6) { // 数据库验证逻辑 return true; } return false; } } ``` 以上是基本的登录逻辑,其中包括单选按钮的选中处理、输入数据的验证、密码的显示与隐藏等常见功能。 最后,您需要在 AndroidManifest.xml 中添加登录界面的启动器,以便在应用程序启动时进入登录界面。 ```xml <activity android:name=".LoginActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> ``` 以上就是基于 Android Studio 的登录界面的实现方法。我希望这可以帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值