title: How to Remove WhiteSpace and Special Characters in Mat-Input using RegEx
body: <p>I have a form and I want to put a validation in angular in such a way that if a user enters any special character then it should show error.
  The form has two fields Name and Description. In name field I want to put validation using regex that the user should not be able to enter anything other than alphanumeric characters.</p>
  
  <p>HTML Code:</p>
  
  <pre><code> &lt;form (ngSubmit)="(submit)" #formControl="ngForm"&gt;
                      &lt;div class="form"&gt;
                          &lt;mat-form-field color="accent"&gt;
                              &lt;input
                                  matInput
                                  #input
                                  class="form-control"
                                  placeholder="name"
                                  [(ngModel)]="data.projectName"
                                  name="name"
                                  (ngModelChange)="noWhiteSpaceOnChange()"
                                  required
                                  minlength="4"
                              /&gt;
  
                              &lt;mat-error *ngIf="formControl.invalid"&gt;{{
                                  getErrorMessage()
                              }}&lt;/mat-error&gt;
                          &lt;/mat-form-field&gt;
                      &lt;/div&gt;
                     &lt;/form&gt;
  </code></pre>
  
  <p>TypeScript Code:-</p>
  
  <pre><code> 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;
  
      }
    }
  </code></pre>