Home > database >  Angular - why does Validators.pattern do nothing in prod but works on my local dev?
Angular - why does Validators.pattern do nothing in prod but works on my local dev?

Time:01-14

Angular - why does Validators.pattern do nothing in prod but works on my local dev?

I added Validators.pattern and '*ngIf="email.errors.pattern"' to an already existing form doing client side validation on email. This all works in development mode, I see the alert if I input an email that violates the pattern. However, when building a dist folder and migrating it to production this new client side check does nothing in the prod environment.

my-add-user-component.ts

  email: new FormControl(this.user.email, [
    Validators.required,
    Validators.email,
    Validators.maxLength(50),
    Validators.pattern("(?!.*[\\ |#|$|%|^|\\*|\\?]).*")
  ]),

my-add-user.html

        <div *ngIf="email.invalid && (email.dirty || email.touched)" >
            <div *ngIf="email.errors.required">
                Email is required
            </div>
            <div *ngIf="email.errors.maxlength">
                Email cannot exceed 50 characters
            </div>
            <div *ngIf="email.errors.email">
                Email is invalid
            </div>
            <div *ngIf="email.errors.pattern">
                Special characters not allowed in email
            </div>
            <div *ngIf="email.errors.duplicateEmail">
                Email already exists
            </div>
        </div>

CodePudding user response:

Answer - Clear Browser Cache. Even though I have startup settings in Chrome, I still get bit.

CodePudding user response:

The reason why the pattern validation is not working in production could be due to different reasons.

One possible reason is that the pattern you are using is not valid in the production environment. This could happen if the pattern is specific to your development environment or has some special characters that are not properly escaped.

Another possible reason is that the minification process is causing the validator to be skipped. This could happen if the minification process is removing or changing the name of the validator function.

Another possible reason is that the form is being submitted before the validation has a chance to run. This could happen if there is a bug in the code that is submitting the form before the validation has completed.

Another possible reason is that the prod environment has different configurations of the application.

I would recommend checking all the above possible reasons and try different ways to fix the issue. If the problem persists you might want to try to debug the application in production mode and check the console logs

  • Related