亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Angular短信模板校驗代碼

標簽:
AngularJS

1、短信模板内容

验证码 ${username} 12345
验证码 ${username} 12345
验证码 ${username} 12345

从代码中提取 username, 并判断验证码 username中只存在英文字母

2、内容校验,提取模板中${}的内容并且,内容只能使用英文

smsTemplateContentChange(value){
    // 短信模板内容 校验
    const error = this.smsTemplateForm.get('templateContent').getError('pattern');
    if (error){
      return;
    }else{
      this.smsTemplateForm.get('templateContent').setErrors(null);
    }
    const reg = /\$\{((?!\{).)*\}/g;
    const matchStr = value.match(reg);
    const resultList = new Set();
    this.paramsList = new Set();
    const pattern = '^[a-zA-Z]{1,}$';
    const regex = new RegExp(pattern);
    let isError = false;
    if (matchStr){
      matchStr.forEach((item: string) => {
        const result = item.replace('${', '').replace('}', '');
        if (!result.match(regex)){
          isError = true;
        }
        resultList.add(result);
      });
      if (isError){
        // 设置错误信息
        this.smsTemplateForm.get('templateContent').setErrors({errorParams: '参数只能使用英文'});
      }else{
        this.paramsList = resultList;
      }

    }
    // console.log(value.match(reg).replace('${', '').replace('}', ''));

  }

3、前端html

<se label="短信模板" [error]="{
    required: '请输入短信模板',
    pattern: '最大长度不超过200!',
    errorParams: '${}参数中只能使用英文'}">
      <textarea formControlName="xxx" [(ngModel)]="smsTemplateVo.xxx"
                (ngModelChange)="smsTemplateContentChange(smsTemplateVo.xxx)" nz-input required></textarea>
      <div ><strong>提取可用参数:</strong><nz-tag *ngFor="let tag of paramsList" nzColor="default">{{tag}}</nz-tag></div>
    </se>

4、最终效果

file

file

个人博客 蜗牛

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消