1

I want to display data in red colour when email is invalid.

I have n no. of data, out of them some email ids are not validated. I have used only dynamic class.

//ts file

email_regx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if (this.data.email_id) {
   this.email_regx = true;
} else {
  this.email_regx = false;
}

//html file
<span [ngClass]="{'redClass': email_regx}">{{ data?.email_id }}</span>

//css
  .redClass{ color: red}
4
  • 2
    <input type="email" /> would be so much easier... Commented Jun 16, 2020 at 10:28
  • no i want only ngclass Commented Jun 16, 2020 at 10:29
  • why are you doing this this.email_regx = true;. You should do something like this. this.data.email_id.test(this. email_regx) Commented Jun 16, 2020 at 10:29
  • Please, have a look at these sites: TLD list; valid/invalid addresses; regex for RFC822 email address Commented Jun 16, 2020 at 15:23

2 Answers 2

1

You are not using regex the right way. Have a look to this doc.

You can make a simple function to test your email and return a boolean.

Component ts file :

public isValidEmail(email: string): boolean {
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
}

html file :

<span [ngClass]="{'redClass': isValidEmail(data?.email_id)}">{{ data?.email_id }}</span>
Sign up to request clarification or add additional context in comments.

Comments

1

At first, please think about using an input field instead.

I would recommend to use FormBuilder from Angular Forms.
It will help you with a lean template and make validation a lot easier.

Could look like:

// TS
contactForm: FormGroup;

constructor() {
    this.contactForm = this.formBuilder.group({
        email: [
            '',
            Validators.compose([
                Validators.pattern('^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$'),
                Validators.required,
            ])
        ]
    });

The FormGroup called "contactForm" here holds one input field called "email". The validation is for your email.

// Template
<form id="contactForm" [formGroup]="contactForm">
    <ion-item lines="none" class="list-item">
        <div>
            <ion-label position="stacked"
                       [class.error]="contactForm.controls.email.value != undefined && !contactForm.controls.email.valid">
                E-Mail *
            </ion-label>
            <ion-input id="email" type="email" formControlName="email"></ion-input>
        </div>
    </ion-item>
</form>

The important part here is the formControlName and the [formGroup] to connect to your ts´s validation.
The part [class.error] adds the class "error" to the label. You also could use that for your input field.

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.