0

I am developing ionic app in which i want to allow everything alphabets, number and special characters also.

Here is my code i have wrote but it is not allowing me to enter hyphen and some special characters.

home.html

<form [formGroup]="myForm2">
          <ion-item>
            <ion-label class="labelColor" stacked>
               Ship register Number
               <ion-icon name="ios-medical" class="iconStar"></ion-icon>
            </ion-label>
            <ion-input formControlName="ship_reg_no" type="text"  [class.invalid]="!myForm1.controls.ship_reg_no.valid && (myForm1.controls.ship_reg_no.dirty || submitAttempt)" maxlength="50" >   
            </ion-input>
        </ion-item>

</form>

home.ts

ngOnInit(){
    this.myForm1 = this._fb.group({
      ship_reg_no: ["", Validators.compose([Validators.pattern(GlobalValidators.Allow_All_Regex), Validators.required])]
});

validaotr.ts

export class GlobalValidators {

    public static EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    public static Allow_All_Regex = '[A-Za-z0-9_@./#&-+,!\-$(); ]*';

}

Please help and thank you in advance!

Apple developer
  • 137
  • 1
  • 11
  • You have not escaped the hyphen, in a string literal, you need to use ``\\-``, not ``\-``. Also, add all the symbols you want to allow inside the `[...]`. Remember to escape all chars you must escape there (see [this thread](https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions)). – Wiktor Stribiżew Sep 03 '18 at 10:04
  • Thanks for comment, It is allowing hyphen now, how do i allow other charcters like backslash `\\` and all – Apple developer Sep 03 '18 at 10:57
  • Add them to the character class, I also mentioned that. See https://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript for backslash. All you need has been thoroughly covered on SO. – Wiktor Stribiżew Sep 03 '18 at 11:03

0 Answers0