代码之家  ›  专栏  ›  技术社区  ›  Cpt Kitkat Anu

如何使用regex删除mat输入中的空白和特殊字符

  •  0
  • Cpt Kitkat Anu  · 技术社区  · 6 年前

    我有一个表单,我想以一种角度进行验证,如果用户输入任何特殊字符,那么它应该显示错误。 表单有两个字段名称和说明。在“名称”字段中,我希望使用regex验证用户不应该输入字母数字字符以外的任何内容。

    HTML代码:

     <form (ngSubmit)="(submit)" #formControl="ngForm">
                        <div class="form">
                            <mat-form-field color="accent">
                                <input
                                    matInput
                                    #input
                                    class="form-control"
                                    placeholder="name"
                                    [(ngModel)]="data.projectName"
                                    name="name"
                                    (ngModelChange)="noWhiteSpaceOnChange()"
                                    required
                                    minlength="4"
                                />
    
                                <mat-error *ngIf="formControl.invalid">{{
                                    getErrorMessage()
                                }}</mat-error>
                            </mat-form-field>
                        </div>
                       </form>
    

    手写体代码:

     noWhiteSpaceOnChange() {
        const validationRegex = /^((?!\s{1,}).)*$/
        if (!validationRegex.test(this.data.projectName)) {
           // this.data.projectName= '';
          let str = this.data.projectName;
          str = str.replace(/[^A-Z0-9]+/ig, "_");
          this.data.projectName=str;
    
        }
      }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Cpt Kitkat Anu    6 年前

    在StackOverflow上寻找答案时从某个地方获取了代码。我找不到感谢代码作者的链接。

    我创建了一个新文件,粘贴了下面的代码,并在app.moule.ts声明中添加了指令。

    import { Directive, HostListener, ElementRef, Input } from '@angular/core';
    @Directive({
    selector: '[specialIsAlphaNumeric]'
    })
    export class SpecialCharacterDirective {
    
    regexStr = '^[a-zA-Z0-9_]*$';
    @Input() isAlphaNumeric: boolean;
    
    constructor(private el: ElementRef) { }
    
    
    @HostListener('keypress', ['$event']) onKeyPress(event) {
    return new RegExp(this.regexStr).test(event.key);
    }
    
    @HostListener('paste', ['$event']) blockPaste(event: KeyboardEvent) {
    this.validateFields(event);
    }
    
    validateFields(event) {
    setTimeout(() => {
    
      this.el.nativeElement.value = this.el.nativeElement.value.replace(/[^A-Za-z ]/g, 
    '').replace(/\s/g, '');
      event.preventDefault();
    
    }, 100)
    }
    
    }
    

    然后在mat输入中,我使用了声明 specialIsAlphaNumeric

    <mat-form-field color="accent">
                                <input
                                    matInput specialIsAlphaNumeric
                                    class="form-control"
                                    placeholder="name"
                                    [(ngModel)]="data.projectName"
                                    name="name"
                                    required
                                    minlength="4"
                                />
    
                                <mat-error *ngIf="formControl.invalid">{{
                                    getErrorMessage()
                                }}</mat-error>
                            </mat-form-field>