UNPKG

ws-form-builder

Version:
106 lines (80 loc) 2.36 kB
//import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Platform } from 'ionic-angular'; @Injectable() export class FormBuilderProvider { avalibleValidators:object = []; constructor( public platform: Platform ){ } detectDevice(){ if (this.platform.is('cordova')) { return true; }else{ return false; } } isEmail(email){ //returns true if email let re = /^(([^<>()[\]\\.,;:\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 re.test(email); } checkAgainstCustomRegex(regex,string){ return regex.test(string); } isNumbersOnly(string){ //returns true if string only containts numbers return /^\d+$/.test(string); } isEmpty(string){ //if empty return true if(string.length == 0){ return true; } return false; } validateAnswer(question,string){ let status = true; //check if field is required if(typeof question.required !== 'undefined'){ if(question.required){ if(this.isEmpty(string)){ status = false; } } } //check the string meets the minimum length if(typeof question.minLength !== 'undefined'){ if(string.length < question.minLength){ status = false; } } //check the string meets the maximum length if(typeof question.maxLength !== 'undefined'){ if(string.length > question.maxLength){ status = false; } } if(typeof question.validators !== 'undefined'){ if(typeof question.validators.validators !== 'undefined'){ if(question.validators.validators.includes('email')){ if(!this.isEmail(string)){ status = false; } } if(question.validators.validators.includes('numbersOnly')){ if(!this.isNumbersOnly(string)){ status = false; } } } if(typeof question.validators.customRegex !== 'undefined'){ if(!this.checkAgainstCustomRegex(question.validators.customRegex,string)){ return status; } } } return status; } }