In order for prayer to be valid, it is stipulated that the person's clothes and body, and the place where he prays, should be pure and clean. If a person knowingly prays on unclean ground, or in an unclean garment, or with an unclean substance on his body, then his prayer is not valid. Allah, may He be exalted, says (interpretation of the meaning): And your garments purify! [al-Muddaththir 74:4]
It was narrated from Asma bint Abi Bakr as-Siddeeq that she said: A woman asked the Messenger of Allah (blessings and peace of Allah be upon him): O Messenger of Allah, what do you think if menstrual blood gets on the garment of one of us, what should she do? The Messenger of Allah (blessings and peace of Allah be upon him) said: If menstrual blood gets on the garment of one of you, let her scratch it off and sprinkle it with water, then let her pray in it.
Narrated by al-Bukhaari (307) and Muslim (291).
Ibn Abd al-Barr (may Allah have mercy on him) said: This hadeeth is the main guideline on washing the impurities from clothes.
End quote from al-Istidhkaar, 1/291
An-Nawawi (may Allah have mercy on him) said: Our view is that removing impurities is a condition of prayer being valid, whether it is an obligatory or supererogatory (naafil) prayer, or a funeral prayer, or the prostration of recitation, or the prostration of thankfulness. Removal of impurities is stipulated in all cases. This is our view and it is also the view of Abu Haneefah, Ahmad and all the scholars of the earlier and later generations.
Three reports were narrated from Maalik about removing impurities, the soundest and most well known of which is that if a person prays knowing that some impurity is present, his prayer is not valid, but if he was not aware of it or he forgot, then it is valid. This is an older view narrated from ash-Shaafai, and his later view is that the prayer is not valid, whether he was aware of it or not, or he forgot.
End quote from al-Majmoo, 3/139
Shaykh Abdullah ibn Saalih al-Fawzaan (may Allah preserve him) said: This indicates that one should not pray in an unclean garment; rather one should pray in a clean and pure garment. This is among the strongest evidence that it is obligatory to purify one's garments for prayer.
End quote from Minhah al-Allaam Sharh Buloogh al-Maraam (1/1808)
If a person prays on a carpet, the edge of which is impure or in contact with something impure, or it is spread over something that is impure, his prayer is still valid, because he is not in direct contact with the impurity.
Ibn Qudaamah (may Allah have mercy on him) said: If he prays on a cloth, the edge of which is impure, or if there is beneath his feet of rope that is tied to something impure, but what he is praying on is pure, then his prayer is valid, regardless of whether or not the impurity moves when he moves. That is because he is not carrying the impurity and is not praying on it; rather his prayer mat is in contact with it. This is like if he prays on pure ground that is connected to impure ground.
End quote from al-Mughni (402).
An-Nawawi (may Allah have mercy on him) said: If there is any impurity on the carpet or mat and the like, and he prays on the spot where the impurity is, then his prayer is not valid, but if he prays on a clean spot of the mat, then his prayer is valid. Our companions said: (This applies) regardless of whether or not the carpet moves when he moves, because he is not carrying or touching the impurity. The same applies if he prays on a bed the feet of which are standing on something impure; his prayer is valid, even if it moves when he moves.
End quote from al-Majmoo (3/159).
Based on the above, if a person prays in a room in which there is something impure, or he prays on the carpet, the edge of which is impure, or that is spread over something impure, but he does not have any direct contact with the impurity, then his prayer is valid.
Places where people pray or other places cannot be ruled to be impure unless it is known that impurity has indeed gotten onto them. The fact that people walk on the carpet after they come out of the washroom does not necessarily imply that the carpet has become impure. The basic principle is that this place is definitely pure and that there is some doubt concerning impurity. One of the ways in which Islam makes things easy for people is that the basic principle that a thing is definitely pure remains in effect and we do not rule that a place is impure unless it is known that it has indeed become impure.
And Allah knows best.
Reproduced from Islam QA