鸿蒙os 正则判断名字、手机号、id、密码

本文介绍了在Ohos平台中,一个名为`MyRegExp`的Java类,用于进行手机号、姓名、密码和身份证号的格式验证,包括长度、连续字符和重复字符的检查,确保数据的准确性。
摘要由CSDN通过智能技术生成
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
  }
}
### 回答1: 在 PHP 中可以使用正则表达式来判断用户输入的手机号是否符合格式。下面是一个示例代码: ``` <?php $phone = $_POST['phone']; if (preg_match("/^1[34578]\d{9}$/", $phone)) { echo "手机号码格式正确"; } else { echo "手机号码格式不正确"; } ?> ``` 该正则表达式 ^1[34578]\d{9}$ 可以判断是否是以1开头,第二位是3~9的数字,并且总共11位数字 注意: 上述代码为基本的校验,需要根据实际地区手机号码的不同变化 还有,可以考虑在服务端使用验证码进行手机号码的确认来防止冒用. ### 回答2: PHP正则表达式可以用于判断用户手机号是否符合特定的格式要求。以下是一个例子: ```php $phone_number = "15812345678"; if (preg_match("/^1[3-9]\d{9}$/", $phone_number)) { echo "手机号格式正确"; } else { echo "手机号格式错误"; } ``` 这个正则表达式的含义是,以1开头,后面跟着3到9中的任意一个数字,然后再跟着9个数字,总长度为11位。如果用户输入的手机号满足这个要求,就会输出"手机号格式正确",否则输出"手机号格式错误"。 该正则表达式解释如下: - `^` 是表示匹配整个字符串的开始位置 - `1` 是表示必须以1开头 - `[3-9]` 表示下一位数字必须是3到9之间的任意一个 - `\d{9}` 表示后面需要跟着9个数字 - `$` 表示匹配整个字符串的结束位置 这样设计的正则表达式是基于中国手机号的特定规则,即以1开头,第二位是3到9中的任意一个数字,后面跟着9个数字。这个正则表达式可以有效地判断用户输入的手机号是否符合这个规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值