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> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?