Advertisement
poli1993_

Instance Validation

Jan 9th, 2020
133
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.email = email;
  10.         this.firstName = firstName;
  11.         this.lastName = lastName;
  12.     }
  13.     get clientId(){
  14.         return this._clientId;
  15.     }
  16.     set clientId(value){
  17.        if(value.length !== 6){
  18.         throw new TypeError('Client ID must be a 6-digit number');
  19.        }
  20.        this._clientId = value;
  21.     }
  22.    
  23.     get firstName(){
  24.         return this._firstName;
  25.     }
  26.  
  27.     set firstName(value){
  28.         this.nameValidation(value,'First');
  29.         this._firstName = value;
  30.     }
  31.  
  32.     get lastName(){
  33.         return this._lastName;
  34.     }
  35.  
  36.     set lastName(value){
  37.         this.nameValidation(value, 'Last');
  38.         this._lastName = value;
  39.     }
  40.  
  41.     nameValidation(value, prop){
  42.         if(value.length < 3 || value.length > 20){
  43.             throw new TypeError(`${prop} name must be between 3 and 20 characters long`);
  44.         }
  45.        const pattern = /^[A-Za-z]+$/g;
  46.        if(!pattern.test(value)){
  47.         throw new TypeError(`${prop} name must contain only Latin characters`);
  48.        }
  49.     }
  50. }
  51. let acc = new CheckingAccount('1314', 'ivan@some.com', 'Ivan', 'Petrov');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement