jeudi 17 décembre 2020

How to get out of if statement and return to beginning question if answer entered is not proper in JavaScript?

A user suppose to enter date to get some analysis. The first question of the code requires him/her to enter date. If a user enters a date that is some future date the code can not go on with future analysis and returns the message to a user "You can not enter any future or date!", and return the process to the beginning requiring a user to enter date again. If user otherwise enter proper date it returns the message: "Ok!You can go on with process!". The code is following:

var anydate = prompt('Enter date(any date before)', anydate)
var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + "/"
                + (currentdate.getMonth()+1)  + "/" 
                + currentdate.getFullYear() + " @ "  
                + currentdate.getHours() + ":"  
                + currentdate.getMinutes() + ":" 
                + currentdate.getSeconds();

console.log(datetime);

if (anydate>datetime){
    alert('You can not enter future date!');
}else{
    alert('OK!You can proceed with analysis!')
};

The first: any value I enter no matter if it is future or some former date, the code is returning me the second case "Ok! You can go on with analysis!")?!

The second: if a user enters improper format which is not a date format, let's say some gibberish such as "ljlkj;jkl;jk;kj;kj" I would like the code to return him to the beggining and send the message "Sorry this is not a date format!"

Aucun commentaire:

Enregistrer un commentaire