A few nights in our life this incident occurred. Each person would have no idea how and when this can happen to him. Nightmare. But at least, as Muslims, we are expected to act in accordance manners of Muslims and Shariah taught and exemplified by our Prophet.
Nightmares are bad dreams that come from the devil and the Prophet forbade us to tell that bad experience dreams to anyone. And if it's a good dream we've got, then it comes from Allah. Prompts for not telling the dream is contained in the following hadith
عن جابر قال جاء أعرابى إلى النبى - صلى الله عليه وسلم - فقال يا رسول الله رأيت فى المنام كأن رأسى ضرب فتدحرج فاشتددت على أثره. فقال رسول الله - صلى الله عليه وسلم - للأعرابى «لا تحدث الناس بتلعب الشيطان بك فى منامك». وقال سمعت النبى - صلى الله عليه وسلم - بعد يخطب فقال «لا يحدثن أحدكم بتلعب الشيطان به فى منامه».
From Jabir, there is an Arab Bedouin came to the Prophet then said,
"O Allah's Apostle, I had a dream of my head was decapitated and rolling then I sprinted after it". The Prophet then said to the man, "Don’t you tell to other people, it was devil’s act who played you in the dreamland.” After that, I heard the Prophet delivering in one of his sermons, "Do not one of you tell the act of the devil who play him in a dream". (Narrated by Muslim, 6063)
The things should we do when we experience that is as written in the following hadith.
Narrated byAbu Qatadah, he said: I heard the Messenger of Allah said: "Good Dream (rukyah) comes from Allah and bad dreams (hilm) comes from the devil. So when one of you have an unpleasant dream he should spit to the left three times and seek refuge in Allah from the evil so that the dream will not harm him." (Muslim No.4195)
Narrated by Abu Hurairah, he said: From the Prophet that he said: "When the end was near, the dream of a Muslim almost no lies. One dream of you which is closer to the truth is the dream of the most honest one in speaking. Muslims dream is including one of the forty-five parts of prophethood. The dream was divided into three groups: a good dream, that is the good news that comes from Allah. The sad dream, that dream came from the devil. And the dreams that come from self whispers. If any one of you have an unpleasant dream, then he should get out of bed and salat, and should not tell the dream to the others.” He said: “I am happy when dreaming about bound with ropes and did not like when dreams with neck chained. The rope is a symbol of constancy in religion.” Abu Huraira said: I do not know if it includes the hadith or sayings of Ibn Sirin. (Muslim No.4200)
In another recommendation, the prayer said when we are having a nightmare is this:
ALLOOHUMMA INNI A'UUDZUBIKA MIN 'AMALISY SYAITHOONI WA SAYYI'AATIL AHLAAM
"O Allah, I seek refuge in You from the evil deeds of the bad dreams"
Besides we know how to act when it gets bad dream, we also pay attention to how good deeds done before we sleep. One of them is the ablution and salat two rakaah, clean our beds and then pray for a guard during sleep. Insha Allah we can avoid the nightmare and from other things, though so only Allah is the All Knowing and Almighty over His servants.
Hopefully our sleeps also be well assessed of worship to Allah.
Wallahua'lam bish showab
Source of reference: