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、最終效果


個人博客 蝸牛
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/99158.html
標籤:Java
