Advertisement
Guest User

07.Instance Validation - Classes Exercise - Softuni

a guest
Oct 16th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CheckingAccount {
  2.     _clientId;
  3.     _email;
  4.     _firstName;
  5.     _lastName;
  6.  
  7.     constructor(clientId, email, firstName, lastName) {
  8.         this.clientId = clientId;
  9.         this.firstName = firstName;
  10.         this.lastName = lastName;
  11.     }
  12.  
  13.     get clientId() {
  14.         return this._clientId;
  15.     }
  16.  
  17.     set clientId(value) {
  18.         if (value.length !== 6) {
  19.             throw new TypeError('Client ID must be a 6-digit number');
  20.         }
  21.  
  22.         this._clientId = value;
  23.     }
  24.  
  25.     get firstName() {
  26.         return this._firstName;
  27.     }
  28.  
  29.     set firstName(value) {
  30.          this.nameValidation(value, 'First');
  31.  
  32.          this._firstName = value;
  33.     }
  34.  
  35.     get lastName() {
  36.         return this._lastName;
  37.     }
  38.  
  39.     set lastName(value) {
  40.         this.nameValidation(value, 'Last');
  41.  
  42.         this._lastName = value;
  43.     }
  44.  
  45.     get email() {
  46.         return this._email;
  47.     }
  48.  
  49.     set email(value) {
  50.         let patternEmail = /^[a-zA-Z0-9]+@[a-zA-Z.]+$/g;
  51.         if (!patternEmail.test(value)) {
  52.             throw new TypeError(`Invalid e-mail`);
  53.         }
  54.  
  55.         this._email = value;
  56.     }
  57.  
  58.     nameValidation(value, prop) {
  59.         if (value.length < 3 || value.length > 20) {
  60.             throw new TypeError(`${prop} name must be between 3 and 20 characters long`);
  61.         }
  62.  
  63.         let patternName = /^[A-Za-z]+$/g;
  64.         if (!patternName.test(value)) {
  65.             throw new TypeError(`${prop} name must contain only Latin characters`);  
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement