0

I'm trying to set up input validation in Angular 2. I'm unsure how to create a regular expression pattern to the validators.pattern

Pattern should look like any of these: 1M01, 12M12, 2M04, 2M04a, 2M04b, 2M04aa, 2M04ba.

For example 1M01 as pattern

pattern: any = '[0-9]{1}[M]{1}[0-9]{1}[0-9]{1}'
cuenumber: any = new FormControl('', [Validators.required, 
                                Validators.pattern(this.pattern)
                                ]);

2 Answers 2

2

You did it right, it does not work ? Try this :

const myPattern: string = '[0-9]{1}[M]{1}[0-9]{1}[0-9]{1}';
this.heroForm = new FormGroup({
    'name': new FormControl('', [
        Validators.pattern(myPattern)
    ])
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try This,

 <input class="form-control"  #EmpCode="ngModel" [(ngModel)]="EmpCode" name="EmpCode"
  type="text" value="" pattern="EmpCodePattern">

EmpCodePattern: any = '[0-9]{1}[M]{1}[0-9]{1}[0-9]{1}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.