Angular短信模板校驗代碼
更新時間:2020年09月23日 09:13:36 作者:蝸??癖?
這篇文章主要介紹了Angular短信模板校驗代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
1、短信模板內(nèi)容
驗證碼 ${username} 12345 驗證碼 ${username} 12345 驗證碼 ${username} 12345
從代碼中提取 username, 并判斷驗證碼 username中只存在英文字母
2、內(nèi)容校驗,提取模板中${}的內(nèi)容并且,內(nèi)容只能使用英文
smsTemplateContentChange(value){ // 短信模板內(nèi)容 校驗 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){ // 設(shè)置錯誤信息 this.smsTemplateForm.get('templateContent').setErrors({errorParams: '參數(shù)只能使用英文'}); }else{ this.paramsList = resultList; } } // console.log(value.match(reg).replace('${', '').replace('}', '')); }
3、前端html
<se label="短信模板" [error]="{ required: '請輸入短信模板', pattern: '最大長度不超過200!', errorParams: '${}參數(shù)中只能使用英文'}"> <textarea formControlName="xxx" [(ngModel)]="smsTemplateVo.xxx" (ngModelChange)="smsTemplateContentChange(smsTemplateVo.xxx)" nz-input required></textarea> <div ><strong>提取可用參數(shù):</strong><nz-tag *ngFor="let tag of paramsList" nzColor="default">{{tag}}</nz-tag></div> </se>
4、最終效果
到此這篇關(guān)于Angular短信模板校驗代碼的文章就介紹到這了,更多相關(guān)Angular短信模板校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
angular.foreach 循環(huán)方法使用指南
本文主要介紹了angular.foreach 循環(huán)方法使用格式及參數(shù),是篇非常基礎(chǔ)的文章,與需要的小伙伴參考下2015-01-01關(guān)于AngularJS中幾種Providers的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于AngularJS中幾種Providers的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用AngularJS具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-05-05angular 未登錄狀態(tài)攔截路由跳轉(zhuǎn)的方法
今天小編就為大家分享一篇angular 未登錄狀態(tài)攔截路由跳轉(zhuǎn)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10