import promptAction from '@ohos.promptAction'; export class MyRegExp { //手机号正则字符串 REGEXP_PHONE: string = '((1[3|4|5|7|8][0-9]{9})$)' REGEXP_NAME: string = '^[\u4e00-\u9fa5_a-zA-Z]{1,18}$' REGEXP_DATE: string = '((0[1-9])|(10|11|12))((0[1-9])|(1[0-9])|(2[0-9])|(30|31))' REGEXP_CARD: string = '^(([1-9]{1})([0-9]{5})(18|19|20)[0-9]{2}(' + this.REGEXP_DATE + '))([0-9]{4})|([0-9]{3}(x|X))$' // REGEXP_PASSWORD: string = '^(?!([A-Z]*)$)(?!([a-z]*)$)(?!([0-9]*)$)(?!([~`!@#$%^&_/.,]*)$)(([A-Za-z0-9])|([~`!@#$%^&_/.,])){8,18}$' REGEXP_PASSWORD: string = '^(?!([A-Z]*|[a-z]*|[0-9]*|[!-/:-@\[-`{-~]*|[A-Za-z]*|[A-Z0-9]*|[A-Z!-/:-@\[-`{-~]*|[a-z0-9]*|[a-z!-/:-@\[-`{-~]*|[0-9!-/:-@\[-`{-~]*)$)[A-Za-z0-9!-/:-@\[-`{-~]{8,18}$' checkPhone(phoneNumber: string): boolean { if (phoneNumber.length === 0) { promptAction.showToast({ message: '请输入您的注册手机号码', bottom: 100, duration: 1000 }) return false } let reg: RegExp = new RegExp(this.REGEXP_PHONE); if (!reg.test(phoneNumber)) { promptAction.showToast({ message: '请输入正确格式的 11 位手机号码', bottom: 100, duration: 1000 }) return false } return true } checkName(name: string): boolean { console.log('name', '。。。。。。' + name) //支持输入中文、英文,不支持数字、特殊符号 if (name.length === 0) { promptAction.showToast({ message: '请输入您的姓名', bottom: 20, duration: 1000 }) return false } let reg: RegExp = new RegExp(this.REGEXP_NAME); if (!reg.test(name)) { promptAction.showToast({ message: '请删除数字或特殊符号', bottom: 20, duration: 1000 }) return false } return true } //检查数字或字母是否连续 checkPasswordCharCode(str: string) { for (let i = 1; i < str.length - 2; i++) { let firstIndex = str.charCodeAt(i - 1); let secondIndex = str.charCodeAt(i); let thirdIndex = str.charCodeAt(i + 1); let fourIndex = str.charCodeAt(i + 2); // fourIndex - thirdIndex == 1; // thirdIndex - secondIndex == 1; // secondIndex - firstIndex==1; if ((fourIndex - thirdIndex == 1) && (thirdIndex - secondIndex == 1) && (secondIndex - firstIndex == 1)) { console.log("check", '不支持输入4位以上连续的数字和字母'); return false; } if ((fourIndex - thirdIndex == -1) && (thirdIndex - secondIndex == -1) && (secondIndex - firstIndex == -1)) { console.log("check", '不支持输入4位以上连续的数字和字母'); return false; } } return true; } //检查数字或字母是否相同 checkPasswordSame(str: string) { for (let i = 1; i < str.length - 2; i++) { let firstIndex = str.charCodeAt(i - 1); let secondIndex = str.charCodeAt(i); let thirdIndex = str.charCodeAt(i + 1); let fourIndex = str.charCodeAt(i + 2); if ((fourIndex == thirdIndex) && (thirdIndex == secondIndex) && (secondIndex == firstIndex)) { console.log("check", '不支持输入4位以上相同的数字和字母'); return false; } } return true; } checkPassword(password: string): boolean { console.log('password', '。。。。。。' + password) if (password.length === 0) { promptAction.showToast({ message: '请输入您的登录密码', bottom: 20, duration: 1000 }) return false } if (!this.checkPasswordCharCode(password)) { promptAction.showToast({ message: '密码强度不符合要求,请根据温馨提示的内容重新设置', bottom: 20, duration: 1000 }) return false } if (!this.checkPasswordSame(password)) { promptAction.showToast({ message: '密码强度不符合要求,请根据温馨提示的内容重新设置', bottom: 20, duration: 1000 }) return false } let reg: RegExp = new RegExp(this.REGEXP_PASSWORD); if (!reg.test(password)) { promptAction.showToast({ message: '密码强度不符合要求,请根据温馨提示的内容重新设置', bottom: 20, duration: 1000 }) return false } return true } checkIdCard(idcard: string,): boolean { console.log('idcard', '。。。。。。' + idcard) if (idcard.length === 0) { promptAction.showToast({ message: '请输入您的身份证号码', bottom: 20, duration: 1000 }) return false } let reg = RegExp(this.REGEXP_CARD); if (idcard.length != 18 || !reg.test(idcard)) { promptAction.showToast({ message: '请输入正确的身份证号码', bottom: 20, duration: 1000 }) return false } return true } }
鸿蒙os 正则判断名字、手机号、id、密码
最新推荐文章于 2024-09-14 17:52:43 发布
本文介绍了在Ohos平台中,一个名为`MyRegExp`的Java类,用于进行手机号、姓名、密码和身份证号的格式验证,包括长度、连续字符和重复字符的检查,确保数据的准确性。
摘要由CSDN通过智能技术生成