Praise be to Allaah.
Shaykh Ibn Uthaymeen (may Allaah have mercy on him) stated that the basic ruling on moving whilst praying is that it is makrooh unless it is done for a reason. However it may be divided into five categories:
1- Obligatory movements
2- Forbidden movements
3- Makrooh movements
4- Mustahabb movements
5- Permissible movements
The obligatory movements are those on which the validity of the prayer depends. For example if a person notices something impure on his ghutrah (head gear), he has to move to remove it and take off his ghutrah. That is because Jibreel came to the Prophet (peace and blessings of Allaah be upon him) when he was leading the people in prayer and told him that there was something dirty on his shoe. So the Prophet (peace and blessings of Allaah be upon him) took it off whilst he was praying and carried on praying. Narrated by Abu Dawood, 650; classed as saheeh by al-Albaani in al-Irwa', 284.
Or if someone tells him that he is not facing the qiblah, he should move to face the qiblah.
The forbidden movements are continuous movements done for no reason, because this kind of movement invalidates the prayer, and whatever invalidates the prayer is not permissible, because it is like mocking the signs of Allaah.
The mustahabb movements are those done in order to do things that are mustahabb in prayer, such as if a person moves to make the row straight, or if he sees a gap in the row in front, so he moves forward whilst praying, or if there is a gap in his row and he moves to fill the gap and other such movements which allow one to do a deed that is mustahabb in prayer, because that makes the prayer more perfect and complete. Hence when Ibn Abbaas (may Allaah be pleased with him) prayed with the Prophet (peace and blessings of Allaah be upon him) and stood to his left, the Messenger of Allaah (peace and blessings of Allaah be upon him) took hold of his head from behind and made him stand on his right. (Agreed upon).
The permissible movements are small movements done when there is a reason, or large movements done in cases of necessity. Small movements done for a reason are like what the Prophet (peace and blessings of Allaah be upon him) did when he was praying and he carried Umaamah, the daughter of Zaynab the daughter of the Messenger of Allaah (peace and blessings of Allaah be upon him), whose maternal grandfather he was. When he stood up he picked her up, and when he prostrated he put her down. Al-Bukhaari, 5996; Muslim, 543.
Large movements in cases of necessity include praying whilst fighting. Allaah says (interpretation of the meaning):
Guard strictly (five obligatory) As‑Salawaat (the prayers) especially the middle Salaah (i.e. the best prayer ‑ Asr). And stand before Allaah with obedience [and do not speak to others during the Salaah (prayers)].
239. And if you fear (an enemy), perform Salaah (pray) on foot or riding. And when you are in safety, offer the Salaah (prayer) in the manner He has taught you, which you knew not (before)
If a person prays whilst walking he is undoubtedly moving a great deal, but in cases of necessity that is permitted and does not invalidate the prayer.
The makrooh movements are all movements other than those mentioned above. This is the basic principle concerning movements whilst praying. Based on this, we say to those who move whilst praying that their action is makrooh and detracts from their prayer. This is something that is seen in many cases, so you may see a person fiddling with his watch or pen or ghutrah or nose or beard and so on. All of that comes under the heading of makrooh, unless it is done a great deal and is continuous, in which case it is haraam and invalidates the prayer.
He also stated that there is no specific number of movements that invalidates the prayer, rather it is any movement that contradicts the idea of prayer in the sense that if anyone saw this man doing that it would look as if he were not praying. This is the kind of movement that invalidates the prayer. Hence the scholars (may Allaah have mercy on them) defined it as something based on custom (al-urf) and said: If movements are many and are continuous then they invalidate the prayer, without mentioning a specific number. Some scholars defined three such movements but that requires evidence because everyone who sets a certain limit or number or manner should produce evidence, otherwise he is introducing new ideas. Majmoo' Fataawa al-Shaykh, 13/309-311
Shaykh Abd al-Azeez ibn Baaz (may Allaah have mercy on him) was asked about a man who moved a great deal whilst praying: did that invalidate his prayer and how could he rid himself of this habit?
He (may Allaah have mercy on him) replied:
The Sunnah is to focus on one's prayer with proper humility, body and soul, whether it is an obligatory prayer or a naafil prayer, because Allaah says (interpretation of the meaning):
Successful indeed are the believers.
2. Those who offer their Salaah (prayers) with all solemnity and full submissiveness
He must pray in a tranquil and dignified manner. This is one of the most important pillars and obligations of prayer, because the Prophet (peace and blessings of Allaah be upon him) said to one who prayed badly and was not tranquil and dignified in his prayer: Go back and pray for you have not prayed. And he did that three times. Then the man said: O Messenger of Allaah, by the One Who sent you with the truth, I cannot do any better than this, teach me. The Prophet (peace and blessings of Allaah be upon him) said: When you stand up to pray, do wudoo' well, then turn to face the qiblah and say takbeer, and recite whatever you can of Qur'aan. Then bow until you are at ease in bowing, then raise your head until you are standing up straight. Then prostrate until you are at ease in prostration, then sit up until you are sitting up straight and are at ease in sitting. Then prostrate until you are at ease in prostration, then get up until you are standing up straight. Then do that throughout your prayer. (Agreed upon).
According to a version narrated by Abu Dawood, he said: Then recite the Essence of the Qur'aan (al-Faatihah) and whatever Allaah wills.
This saheeh hadeeth indicates that being at ease in the postures is a pillar or essential part of the prayer, and an important obligation, without which prayer is not valid. Whoever pecks out his prayer (by moving too rapidly), there is no prayer for him. Khushoo' (proper focus and humility) is the essence of prayer. It is prescribed for the believer to pay attention to that and strive to achieve it.
With regard to defining the movements that contradict this ease and focus in prayer and limiting them to three movements, there is no hadeeth from the Prophet (peace and blessings of Allaah be upon him) to support this idea. Rather that is the suggestion of some scholars for which there is no reliable evidence.
But it is makrooh to fidget during the prayer, such as touching one's nose, beard or clothes and being distracted by that. Fidgeting a great deal invalidates the prayer. But if it is done a little, according to what is customary, or is done a great deal but is not continuous, then the prayer is not invalidated thereby. But it is prescribed for the believer to maintain proper focus and humility, and to avoid fidgeting a little or a lot, and to strive to make his prayer perfect.
The evidence that small movements, or repeated movements that are not continuous, do not invalidate the prayer is the report which says that the Prophet (peace and blessings of Allaah be upon him) opened the door for Aa'ishah one day whilst he was praying. Narrated by Abu Dawood, 922; al-Nasaa'i, 601; classed as saheeh by Shaykh al-Albaani in Saheeh al-Tirmidhi, 601.
And it was proven in the hadeeth of Abu Qataadah (may Allaah have mercy on him) that the Prophet (peace and blessings of Allaah be upon him) led the people in prayer one day when he was carrying Umaamah the daughter of Zaynab. When he prostrated he put her down and when he stood up he picked her up.
Fataawa Ulama' al-Balad al-Haraam, 162-164.