I prayed with a friend dhuhr and I was imam then I realized that I had intention of asr not dhuhr. Then I switched my intention to ratiba of dhuhr because of that. Is my friends dhuhr prayer valid?

Ustadh AbdulAziz al-Haqqan
The prayer of the one behind him is valid. The invalidity of the imam's prayer does not invalidate the prayer of the ma'mum who is excused. However, I believe that you suffer from waswas so never ever do this again, just continue your salah.