Advertisement
Lulunga

Classes 07. Instance Validation

Oct 18th, 2019
147
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.     set clientId(clientId){
  9.         let pattern = /^\d{6}$/g;
  10.         if(!pattern.test(clientId)){
  11.             throw new TypeError("Client ID must be a 6-digit number")
  12.         }
  13.         return this._clientId=clientId
  14.     }
  15.     get clientId(){
  16.         return this._clientId()
  17.     }
  18.     set email(email){
  19.         let emailPatern = /^[a-zA-Z0-9]+@[a-zA-Z.]+$/g
  20.         if(!emailPatern.test(email)){
  21.             throw new TypeError("Invalid e-mail")
  22.         }
  23.         return this._email=email
  24.     }
  25.     get email(){
  26.         return this._email
  27.     }
  28.     set firstName(firstName){
  29.         if(!(firstName.length>=3&&firstName.length<=20)){
  30.             throw new TypeError("First name must be between 3 and 20 characters long")
  31.         }
  32.         let pattern =/[a-zA-Z]/g
  33.         if(!pattern.test(firstName)){
  34.             throw new TypeError("First name must contain only Latin characters")
  35.         }
  36.         return this._firstName=firstName
  37.     }
  38.     get firstname(){
  39.         return this._firstName
  40.     }
  41.     set lastName(lastName){
  42.         if(!(lastName.length>=3&&lastName.length<=20)){
  43.             throw new TypeError("Last name must be between 3 and 20 characters long")
  44.         }
  45.         let pattern =/[a-zA-Z]/g
  46.         if(!pattern.test(lastName)){
  47.             throw new TypeError("Last name must contain only Latin characters")
  48.         }
  49.         return this._lastName=lastName
  50.     }
  51.     get lastName(){
  52.         return this._lastName
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement