kstoyanov

09. Instance Validation

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