Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. export class NumberValidators {
  2.  
  3. static integer(maxDigits: number = 22){
  4.  
  5. return this.decimal(22, 0);
  6. }
  7.  
  8. static decimal(maxDigits: number = 22, maxDecimals: number = 22){
  9.  
  10. return (control: AbstractControl): ValidationErrors | null => {
  11.  
  12. let value:string = control.value;
  13.  
  14. if(isEmpty(value)) return null;
  15.  
  16. let matches = String(value).match(FLOAT_REGEX);
  17.  
  18. if(matches === null) return {'float': true};
  19.  
  20. if(matches[2] !== undefined) {
  21.  
  22. if((matches[1].length + matches[2].length) > maxDigits) return
  23. {'float': true};
  24.  
  25. if(matches[2].length > maxDecimals) return {'float': true};
  26.  
  27. } else {
  28.  
  29. if((matches[1].length) > maxDigits) return {'float': true};
  30.  
  31. }
  32.  
  33. return null;
  34. };
  35. }
  36. }
  37.  
  38. rowIndexControl = new FormControl('', [NumberValidators.integer()]);
  39. positionIndexControl = new FormControl('', [NumberValidators.integer()]);
  40. rowDistanceControl = new FormControl('', [NumberValidators.integer()]);
  41. colDistanceControl = new FormControl('', [NumberValidators.integer()]);
  42.  
  43. Validators.pattern(/^[0-9]+$/)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement