| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { Injectable } from '@angular/core'; |
| import { |
| FormControl, |
| FormGroup |
| } from '@angular/forms'; |
| @Injectable() |
| export class ValidationService { |
| |
| /** |
| * Validates email address |
| * |
| * @param formControl |
| */ |
| public validateEmail(formControl: FormControl): {[error: string]: any} { |
| let EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\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,}))$/; |
| return EMAIL_REGEXP.test(formControl.value) ? null : { validateEmail: { valid: false } }; |
| } |
| |
| /** |
| * Validates required numeric values |
| * |
| * @param formControl |
| */ |
| public numericRequired(formControl: FormControl): {[error: string]: any} { |
| return (formControl.value && formControl.value > 0) ? null : { numericRequired: { valid: false } }; |
| } |
| |
| /** |
| * Validates matching string values |
| * |
| * @param controlKey |
| * @param matchingControlKey |
| */ |
| public matchingPasswords(controlKey: string, matchingControlKey: string): {[error: string]: any} { |
| return (group: FormGroup): {[key: string]: any} => { |
| if (group.controls[controlKey].value !== group.controls[matchingControlKey].value) { |
| return { mismatch: { valid: false } }; |
| } |
| } |
| } |
| } |