armisa-models
Version:
models of armisa!
100 lines (86 loc) • 2.79 kB
text/typescript
import { BaseSelfControl } from './BaseSelfControl';
import { ValidationEventArgs, ValidationChain } from '../chainOfResponsibility';
import { EnumValidateState } from '../enums';
export class SelfEmail extends BaseSelfControl<
string,
string,
string,
HTMLInputElement
> {
minLength: number | undefined;
maxLength: number | undefined;
private validateNormal = (eventArgs: ValidationEventArgs) => {
if (this.value) {
const email = /^(([^<>()[\]\\.,;:\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,}))$/g;
if (this.value.match(email)) {
return true;
} else {
eventArgs.error = 'ایمیل وارد شده معتبر نیست';
eventArgs.state = EnumValidateState.invalidValue;
eventArgs.cancel = true;
}
}
};
private validateRequired = (eventArgs: ValidationEventArgs) => {
if (this.required) {
if (!this.value) {
eventArgs.error = 'اطلاعاتی وارد نشده است';
eventArgs.state = EnumValidateState.empty;
eventArgs.cancel = true;
}
}
};
isValueEmpty = () => {
return typeof this.#value === 'string' && this.#value !== '';
}
isValueNotEmpty = () => {
return !this.isValueEmpty();
}
validate = () => {
var validationChain = new ValidationChain();
validationChain.validators.add(this.validateNormal);
validationChain.validators.add(this.validateRequired);
this.validation = validationChain.validate();
};
public cleaningClassInitializer = () => {
this.timerOfIncorrectChar && clearTimeout(this.timerOfIncorrectChar);
this.hasChange = false;
this.defaultValue = undefined;
this.initializeProperties = false;
this.initializeListener = false;
this.validation = true;
};
public refreshHasChange = () => {
if (this.showHasChangeFlag) {
this.hasChange = this.defaultValue !== this.#value;
}
};
public restartDefaultValue = () => {
this.defaultValue = this.value;
this.refreshHasChange();
};
#value: string;
public get value() {
return this.#value;
}
public set value(value: string) { }
public setValue = (value: string) => {
this.#value = value;
this.refreshHasChange();
};
public deserialize = (value: string) => {
this.#value = value;
this.restartDefaultValue();
};
constructor(value: string) {
super();
this.#value = value;
this.restartDefaultValue();
}
static empty(): SelfEmail {
return new SelfEmail('');
}
static deserialize(value?: string): SelfEmail {
return new SelfEmail(value || '');
}
}