Messenger of Allah (S) said: "When he commits Hadath - meaning a man - and he is sitting in the end of his Salat before saying the Taslim, then his Salat is acceptable."
قَالَ رَسُولُ اللَّهِ ﷺ إِذَا أَحْدَثَ يَعْنِي الرَّجُلَ وَقَدْ جَلَسَ فِي آخِرِ صَلاَتِهِ قَبْلَ أَنْ يُسَلِّمَ فَقَدْ جَازَتْ صَلاَتُهُ