S
S
Sergey2019-07-31 16:07:39
JavaScript
Sergey, 2019-07-31 16:07:39

How to do simple validation in JS?

There is a simple code - the user enters the first and last name separated by a space until he clicks Cancel, after which the data array is output to the console.

function User(firstName, lastName){ 
    this.firstName = firstName;
    this.lastName = lastName;
    this.regDate = new Date();
}

function UserList(){
    this.users = [];
    this.add = function(user) {
        this.users.push(user);
    }
    this.getAllUsers = function(){
        return this.users;
    }
}  

var userList = new UserList();

while (true) {
    var i = prompt('Введите имя и фамилию через пробел!');
    if(!i) break;
    i = i.split(' '); 
    var user = new User(i[0], i[1]);
    userList.add(user);
}

console.log(userList.getAllUsers());


The question is how to make it valid so that the script checks whether two words are entered separated by a space, otherwise it would issue an alert ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question