Tuesday, 4 January 2022

Validator.ts

 import { FormControl } from '@angular/forms';


const alphanumericLetters = /^[0-9a-zA-Z\s]+$/;       //   /^[\w\s]+$/;

const alphabetLetters = /^[a-zA-Z\s]+$/;

const floatingNumbers =  /^(?:\d*\.\d{1,2}|\d+)$/ ; // /^\d{1,8}(\.\d{1,4})+$/;   //   [0-9]+(\.[0-9]{1,2})


const specialCharacters = /^[-.,//0-9a-zA-Z\s]+$/;

const nospace = /^[^\s].*/;


const minNumber = /^[1-9][0-9]*$/;

const panCardNumber = /^[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}$/;

const mobileNumber = /^[0][1-9]\d{8}$|^[5-9]\d{8}$/;

const emailPattern =  /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

const solvezyEmail =  /^[a-zA-Z0-9._-]+@[sS]+[oO]+[lL]+[vV]+[eE]+[zZ]+[yY]+\.[cC]+[oO]+[mM]+$/;




const numberValidator = (control: FormControl) => {

  return isNaN(control.value) ? { invalid: true, numeric : true } : null;

};


const alphanumericValidator = (control: FormControl) => {

  if (!control.value || control.value.match(alphanumericLetters)) {

    return null;

  } else {

    return { invalid: true, alphanumeric : true };

  }

};

const panCardNumberValidator = (control: FormControl) => {

  if (!control.value || control.value.match(panCardNumber)) {

    return null;

  } else {

    return { invalid: true, panNumber : true };

  }

};

const mobileNumberValidator = (control: FormControl) => {

  if (!control.value || control.value.match(mobileNumber)) {

    return null;

  } else {

    return { invalid: true, mobNumber : true };

  }

};


const emailValidator = (control: FormControl) => {

  if (!control.value || control.value.match(emailPattern)) {

    return null;

  } else {

    return { invalid: true, emailValid : true };

  }

};


const solvezyemailValidator = (control: FormControl) => {

  if (!control.value || control.value.match(solvezyEmail)) {

    return null;

  } else {

    return { invalid: true, solvezyEmailValid : true };

  }

};


const fltNumValidator = (control: FormControl) => {

  if (control.value.match(floatingNumbers)) {

    return null;

  } else {

    return { invalid: true, decimalValid: true };

  }

};


const textOnlyValidator = (control: FormControl) => {

  if (!control.value || control.value.match(alphabetLetters)) {

    return null;

  } else {

    return { invalid: true, textOnly : true };

  }

};


const splCharValidator = (control: FormControl) => {

  if (control.value.match(specialCharacters)) {

    return null;

  } else {

    return { invalid: true };

  }

};

const minAmount = (control: FormControl) => {

  if (!control.value || control.value.match(minNumber)) {

    return null;

  } else {

    return { invalid: true };

  }

};


const nospaceValidator = (control: FormControl) => {

  return null;

};


const compareStrings  = (c: FormControl) => {

  const pass = c.get('new').value;

  const confirm = c.get('confirm').value;

  if (pass === confirm) {

    return null;

  } else {

    return {invalid: true, mismatch: true};

  }

};


export {

  numberValidator,

  alphanumericValidator,

  textOnlyValidator,

  fltNumValidator,

  splCharValidator,

  nospaceValidator,

  minAmount,

  panCardNumberValidator,

  mobileNumberValidator,

  emailValidator,

  compareStrings,

  solvezyemailValidator

};


PostedBy: pankaj_bhakre

No comments:

Post a Comment