The World Federation One Stop Fiqh

Ruling 1428

If a person recites Sūrat al-Ḥamd and the other surah thinking that the imam is in the third or fourth rakʿah, and before or after rukūʿ he realises that it was the first or second rakʿah, his prayer is valid. And if he realises this during Sūrat al-Ḥamd or the other surah, it is not necessary that he completes them.