I make a route application on Yandex maps with Yandex API.

There is a line:

ymaps.route( [routeFrom, routeIn, routeIn2, routeIn3, routeIn4, routeIn5, routeTo], {mapStateAutoApply:true}) 

routeFrom , routeIn , routeIn2 and so on are input fields, or rather their values

var routeFrom = document.getElementById ('route-from'). value

That is, the routeFrom field is the starting point, routeTo is the destination. routeIn , routeIn2 , routeIn3 , routeIn4 , routeIn5 are intermediate points.

It turns out that the user must fill in the routeFrom and routeTo fields, but the routeIn, routeIn2, etc. - not necessary.

But it turns out that if the user did not fill in the routeIn field (or did not fill in all (but all of them are 5)), in this case, it gives an error, because here they are declared:

 ymaps.route( [routeFrom, routeIn, routeIn2, routeIn3, routeIn4, routeIn5, routeTo], {mapStateAutoApply:true}) 

Is it possible to somehow check the fields for fullness, and if they are filled, then substitute them into this function?

I would be very grateful for the help!

    1 answer 1

     if(routeFrom != '' && routeTo != '' && (routeIn != '' || routeIn2 != '' || routeIn3 != '' || routeIn4 != '' || routeIn5 != '')){ ymaps.route([routeFrom, routeIn, routeIn2, routeIn3, routeIn4, routeIn5, routeTo],{mapStateAutoApply:true}) }else if(routeFrom != '' && routeTo != ''){ ymaps.route([routeFrom, routeTo],{mapStateAutoApply:true}) }else{ alert('Ошибочка! Неприятна :(') }; 

    If understood correctly, then like this

    • Not really. Field routeIn, routeIn2 and so on, (there are 5). But it may be that only 2, or 1, or 3, etc. are filled, and so on, as the user wants - iKey
    • @ Denis, corrected. Try this - Yuri