Crying for fear of Allah is a virtuous deed. It brings about Allah’s mercy, forgiveness and pleasure. At-Tirmidhi narrated on the authority of Ibn `Abbas that Allah’s Messenger (peace and blessings be upon him) said: “Two eyes are never touched by the Hell-fire: the eyes (of a man who) shed tears for fear of Allah and the eyes (of a man who) kept awake to guard (Muslims) for the sake of Allah.”
The majority of Muslim jurists say that crying or weeping out of piety and fear of Allah for remembering the Hereafter — whether in a loud or a low voice— does not nullify prayer. In the same way, crying and moaning out of pain does not nullify prayer. However, scholars of the Shafi`i School of Fiqh opine that crying in general nullifies prayer, no matter what the reason for crying is.

It is stated in The Kuwaiti Encyclopedia for Fiqh: “Scholars of the Hanafi and the Shafi`i Schools of Fiqh see that moaning, wailing, sobbing and the like nullify prayer if it is accompanied by some utterance of two sounds at least. The Hanafi scholars exclude a patient who can’t help moaning out of pain. Prayer of such a person is not nullified by moaning or crying, even if it is accompanied by an utterance of two sounds, just for necessity.

Abu Yusuf said: If moaning is a result of pain that can be dispensed with, then it nullifies prayer. But if it is due to a severe unbearable pain, it does not nullify prayer. Muhammad, disciple of Abu Hanifah, said: If one’s illness is not so serious, his prayer is nullified by moaning. However, if his is so seriously illness that he cannot help moaning during prayer, then his prayer is valid.
Ibn `Abdin held the same view with some restrictions, i.e., moaning in this case there should not have any utterances (words), but just sound. The Hanafi scholars also exclude crying for fear
of the Hereafter, and on mentioning Paradise and Hellfire; crying in those cases does not nullify prayer, as this is a sign of piety. If one led in prayer is affected by the recital of the imam to the extent that he comments on it saying, yes or nay, his prayer is all right.
Ibn `Abdin reported Al-Kafi as saying that moaning out of piety or remembering Paradise and Hellfire carry the meaning of du`a’, as if one says: O Allah! I seek Your Paradise and seek refuge in You from the Hell-Fire. So if one says it explicitly, there is nothing wrong in his prayer. But if one moans out of pain or affliction, hence his moaning carry the meaning of a call for sympathy, as if he is seeking condolence. Hence any utterances in this course nullify prayer.

The Shafi`i scholars see no difference between crying out of piety or whatsoever. They see that in all cases crying nullifies prayer.
The Maliki scholars of Fiqh see it permissible to cry in prayer out of severe pain or out of piety, whether one cries heavily or lightly. Otherwise, there is a difference between intended and unintended crying; between heavy and light crying. Intended crying in this case nullifies prayer whatever its extent, whereas unintended sobbing does not nullify prayer except if it is heavy. But one has to offer two forgetfulness prostrations for unintended light crying. Ad-Dardir says: This applies to crying in a loud voice. Yet silent crying does not matter at all, even if intended, so long as it is not heavy.
The Hanbali School of Fiqh share the same view of the Maliki School. They agree that prayer is not nullified by crying out of fear of Allah, as it is uncontrollable. It is the same as one cannot help coughing, sneezing and yawning during prayer, even if, during this, one makes some utterance consisting of two sounds. Mohana said: I stood beside Abu `Abdullah while offering prayer and heard him yawning five times. He made some utterances like oh, oh. This does not nullify prayer because it does not incur any rulings that are related to speech. It is, by and large, blameworthy to intend crying or laughing, not to make any utterances that are made of two sounds, so that it nullifies prayer.”