As for the first part of the topic, we’d like to cite from what the late Sheikh Sayyed Sabiq stated in his well-known book, Fiqh As-Sunnah in this regard:

“Some scholars say that semen is impure, but apparently it is pure, for it is only recommended to wash it off if it is still wet, and to scratch it off if it is dry. `A’ishah (may Allah be pleased with her) said: “I used to scratch the semen off the Messenger of Allah’s clothes if it was dry, and wash it off if it was still wet.” (Reported by Ad-Daraqutni and Al-Bazzar). It is also related that Ibn `Abbas (may Allah be pleased with him) said: “I asked the Messenger of Allah (peace and blessings be upon him) about semen on clothes. He said, ‘It is the same as mucus and spittle. It is sufficient to rub the area with a rag or cloth.’

As for your second question on the Islamic ruling concerning masturbation, we would like first to cite for you the words of the eminent Muslim scholar, Sheikh Yusuf Al-Qaradawi in his well-known book, The Lawful and the Prohibited in Islam, regarding the ruling on masturbation:

“The majority of Muslim scholars consider masturbation Haram (unlawful). For instance, Imam Malik bases his judgment on the verse: “Those who guard their sexual organs except with their spouses or those whom their right hands possess, for (with regard to them) they are without blame. But those who crave something beyond that are transgressors.”(Al-Mu’minoon:5-7)

Imam Malik argued that the masturbator is one of those who ‘crave something beyond that.’
On the other hand, it is reported that Imam Ahmad Ibn Hanbal regarded semen as an excretion of the body like other excrete, and permitted its expulsion the same way blood letting is permitted. Ibn Hazm holds the same view. However, the Hanbali jurists permit masturbation only under two conditions: first, the fear of committing fornication or adultery, and second, not having the means to marry.

We are inclined to accept the opinion of Imam Ahmad in a situation in which there is sexual excitation and danger of committing the Haram. For example, a young man has gone abroad to study or work, thereby encountering many temptations which he fears he will be unable to resist, may resort to this method of relieving sexual tension provided he does not do it excessively or make it a habit. Yet, better than this is the Prophet’s advice to the Muslim youth who is unable to marry, namely, that he seek help through frequent fasting, for fasting nurtures will-power, teaches control of desires, and strengthens the fear of Allah. The Prophet, peace and blessings be upon him, is reported to have said, “O youth, whoever of you is able to marry, let him marry, for it spares one looking at what one should not, or lapsing in adultery. And if he cannot marry, let him observe fasting, for it is a shield against evil.”‘ (Reported by Muslim.)”

Regarding the ruling on masturbation, Sheikh Ahmad Kutty, a senior lecturer and an Islamic scholar at the Islamic Institute of Toronto, Ontario, Canada , adds:

“Masturbation is generally considered forbidden in Islam since it is deemed to fall under the category of sexual satisfaction outside the framework of marriage.

According to the Qur’an, those who seek fulfillment outside the framework of marriage are deemed transgressors. (See the Qur’an: 23: 7). While explaining this verse, one often finds the commentators listing masturbation under the list of the forbidden categories of sexual fulfillment.

What we have stated above is the generally accepted view. However, according to some scholars, if a person is so tormented by his intense sexual desire or craving that he fears falling into zina (fornication), in such a case, masturbation is permitted as the lesser of the two evils. His case may be compared to that of a person who is permitted to eat pork because of fear of death by starvation.

However, having mentioned the above, we must state that one hardly needs to resort to it when we recognize the relatively flexible approach towards marriage that Islam adopts. Unfortunately, against the clear teachings of Islam, marriage has been rendered another difficult process today due to warped customs and conventions and undue expectations. When we approach marriage from the point of view of pristine Islam, we shall find less and less people being forced to exercise the above type of exceptional rulings.”

As for protection and remedy, we’d like to quote the following useful measures which can keep Muslim youths, who do not possess the means to get married, away from this sinful habit:
1-Observing fasting, because it bolsters one’s faith, preserves chastity, and protects one from evil thoughts.
2- Observing moderation in eating and drinking in order to avoid stimulating one’s desire.
3- Keeping away from anything that is sexually stimulating, such as pornographic pictures, erotic films and love songs.
4- Choosing good and righteous friends.
5- Keeping oneself busy in worship and spiritual acts.
6- Interacting with activities of the society in such away that it keeps you away from thinking about sex.
7-Avoiding gatherings and places that bring men and women physically close to each other.
8- Avoiding sleeping on beds that are so soft that they make one think about sex.
9- Trying to admire natural things such as flowers and beautiful scenery, which do not stimulate one sexually, instead of admiring girls and women.

10- Marrying as soon as possible.

Finally, we advise any Muslim involved in this sinful practice to resort to Allah seeking His help, and be assured that by returning to Allah with sincere repentance, He will never forsake him/her and will make it easy for him/her to give up such sinful act.