Validate that the date is not in the future [duplicate]

0

In a file js, thanks to the help and more research, I have already validated the correction of a date provided by the user. As it is a date of birth I would like to be able to control that it was not superior to the current date. In the following code, how do I incorporate the described control? Thank you in advance.

function validaEdat(){
    vesSumant();
    vfalladata = false;

    //Comprovació de la data de naixement i el correu electrònic

    var vregexNaix = /^(((0{0,1}[1-9]|[12][0-9]|3[01])([/])(0{0,1}[13578]|10|12)([/])(\d{4}))|((0{0,1}[1-9]|[12][0-9]|30)([/])(0{0,1}[469]|11)([/])(\d{4}))|((0{0,1}[1-9]|1[0-9]|2[0-8])([/])(0{0,1}2)([/])(\d{4}))|((29)(\.|-|\/)(0{0,1}2)([/])([02468][048]00))|((29)([/])(0{0,1}2)([/])([13579][26]00))|((29)([/])(0{0,1}2)([/])([0-9][0-9][0][48]))|((29)([/])(0{0,1}2)([/])([0-9][0-9][2468][048]))|((29)([/])(0{0,1}2)([/])([0-9][0-9][13579][26])))$/;
 -->

    vdataNaix = document.formu.dataNaix.value;
    vanyData = vdataNaix.substring(vdataNaix.length-4, 4);
    if((!vregexNaix.test(vdataNaix)) || (/^(\d{4})$/.test(vanyData))){
        alert("Bonvolu skribi vera naskighdato per tt/mm/jjjj. \n--------------------------------------\n Per favor, una data vera com dd/mm/aaaa.");
        document.formu.dataNaix.value = "";
        document.formu.dataNaix.focus();
        return false;
    }

    controlaCaselles();
    return true;
}
    
asked by Joan Inglada Roig 01.04.2017 в 12:35
source

2 answers

1

The correct thing would be to validate at least the two layers: client and server. If you manipulate the database manually, then you should also put a restriction in the database.

When you deal with dates, you have to be very careful. Not only do you have to take into account dates, if not, in many cases, also the time zone. If you do not take this into account, a user in America and another in Asia can see a different date.

There is a library called moment , which is exceptional and almost a de facto choice when dealing with dates. Moment lets you know very easily if the date you have entered is valid:

const isValidDate = moment('fake date').isValid();
console.log('Is a valid date?:', isValidDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

As simple as passing the date to moment and from there we have many functions available for that date, such as add or restart days / weeks / months / years, format, etc (for more information, see the documentation .To know if the date is not future, just use a comparison:

const isPastOrPresent = moment('2017-04-02') <= moment();
console.log('Is past or preset date?:', isPastOrPresent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    
answered by 01.04.2017 в 15:24
1

I think you should start by modifying your HTML so look:

<input id="datefield" type='date' min='1899-01-01' max='2000-13-13'></input>

If you notice Min and max in the tag it allows you to set the range of dates you want the user to choose the date.

Now using a bit of code we can do it with the current date.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
   dd='0'+dd
} 
if(mm<10){
   mm='0'+mm
} 
today = yyyy+'-'+mm+'-'+dd;
document.getElementById("datefield").setAttribute("max", today);

and that's it. I recommend you not to use the java script, and use a server marker. in ASP.MVC is something like that

@DateTime.Now.ToString('yyyy-MM-dd');

and I recommend it because if the user has an outdated date, or the user modifies it with the purpose of buging your system it will do so if you use the java script, if you use a server Mark it will take the date of the server. Greetings!

Demo JsFiddle

Only supports Chrome, FireFox, IE > 10

    
answered by 01.04.2017 в 15:26