java
package com;
public class Main {
public static void main(String[] args) {
String mobile = "12345678901";
String password = "11111111";
String result = login(mobile, password);
if (!"SUCCESS".equals(result))
System.out.println(result);
else
System.out.println("登录成功");
}
public static String login(String mobile,String password){
if (mobile.length()!= 11) return "手机号长度错误";
int len = password.length();
if (len < 8 || len > 16) return "密码长度有误";
String mobileRegex = "[1-9][0-9]{10}";
if (!mobile.matches(mobileRegex)) return "手机号输入错误";
String pwdRegex = "[a-zA-Z0-9_]{"+len+"}";
if (!password.matches(pwdRegex)) return "密码只能是字母数字下划线";
return "SUCCESS";
}
}
python
import re
def login(mobile, password):
if len(mobile) != 11:
return "手机号格式错误"
mobile_pattern = re.compile(r"[1-9][0-9]{10}")
reply = re.match(mobile_pattern, mobile)
if reply:
pass
else:
return "手机号格式有误"
length = len(password)
if length < 8 | length > 16:
return "密码长度有无"
pwd_pattern = re.compile(r"[a-zA-Z0-9_]{" + str(length) + "}")
reply = re.match(pwd_pattern, password)
if reply:
pass
else:
return "密码只能是字母数字下划线"
return "SUCCESS"
if __name__ == '__main__':
mobile = "12345678901"
password = "23ded22d#"
result = login(mobile, password)
if "SUCCESS" != result:
print(result)
else:
print("登录成功")
JavaScript
<script>
let mobile = "12345678901"
let password = "sf32r23fqad"
if (mobile.length !== 11){
console.log("手机号错误")
}
let pwdLen = password.length
if (pwdLen <8 || pwdLen > 16){
console.log("密码长度为 8 ~ 16 位字符")
}
let mobileRegex = /[1-9][0-9]{10}/
if (!mobileRegex.test(mobile)){
console.log("手机号有误")
}
let pwdRegex = new RegExp("[0-9a-zA-Z]{"+pwdLen+"}")
if (!pwdRegex.test(password)){
console.log("密码只能是字母数字下划线")
}
</script>