Both local and EU elections coming up in 2019, on May 26th.
The National Statistics Institute has published the requisite for foreign residents to be able to vote in both.
Being registered on the padrón PLUS having expressed their desire to make use of their right to vote, before January 30th, 2019. You can do so at your Town Hall, but the Institute will also actively seek out foreign residents by sending information by mail.
In order to facilitate the demonstration of the will to vote, the Electoral Census Office (ECO) will send a communication to the EU citizens residing in Spain in the last days of October, or the first of November, with the data of their censuses preprinted. The communications will be sent only to those who the ECO has not addressed on the occasion of elections to the European Parliament and previous municipal (registered in the Register as of September 1, 2013) and have not made the formal declaration
You can also do so online, but only with a digital certificate installed on your computer, or a combination of your NIE number and a code, sent to you by ordinary mail by the Institute (the mailing referred to above).
With regards to UK nationals, the Institute writes the following:
Aunque Reino Unido ha manifestado su intención de salir de la UE el 30 de marzo de 2019,
la Oficina del Censo Electoral (OCE), como previsión cautelar, no ha excluido a sus
ciudadanos de la formación del censo electoral para estas elecciones municipales, así como
para las del Parlamento Europeo. No obstante, su inclusión dejará de tener efecto en cuanto
se produzca la separación de Reino Unido de la UE.
Although the UK has manifested its intention to leave the EU on March 30th 2019, the Electoral Census Office, as a precautionary provision, has not excluded its citizens from the formation of the electoral roll for these local elections, nor from that for the European Parliament. Nevertheless, their inclusion will cease to have affect once the separation of the UK from the EU takes place.
Update 17.1.2019 UK and Spain are trying to reach an agreement to allow UK residents in Spain to vote in upcoming elections.
Update 21.1.2019 UK and Spain have signed a treaty, confirming the continued right, even in case of a no-deal Brexit, for UK residents in Spain to vote and stand for upcoming local elections (May 26) and reciprocally, for Spanish residents in the UK, for the upcoming local elections there (May 2). The deal will still have to go through Parliament, but will be put in place provisionally as soon as the UK leaves the EU.
For the 2019 local elections, being registered on the padrón and having expressed your desire to vote before the 30th of January is enough, for future local elections a continued residency of 3 years will be a requisite for entitlement to vote.