"A series of precautionary measures are laid down by the halakhah to prevent the possibility of masturbation. A man should not bring about a voluntary erection, think about sexual matters, sleep on his back, watch animals at sex play, handle his genitals even while urinating, or wear tight pants." page 134
"Whoever holds his membrum when he makes water is as though he had brought a flood on the world'. But, they said to R. Eliezer, would not the spray bespatter his feet and he would appear to be maimed in his privy parts so that he (Being assumed to be incapable of procreation) would be the cause of casting upon his children the reflection of being illegitimate? — It is preferable, he answered them, that a man should be the cause of casting upon his children the reflection of being illegitimate than that he should make himself a wicked man, even for a while, before the Omnipresent.... It is possible for a man to stand on a raised spot and to make water or to make water in loose earth and thus to avoid making himself wicked, even for a while, before the Omnipresent.... What is he to do when he can find no raised spot or loose earth', he answered them, 'It is preferable that a man should be the cause of casting upon his children the reflection of being illegitimate than that he should make himself a wicked man, even for a while, before the Omnipresent'.
R. Abba the son of R. Benjamin b. Hiyya learnt: But he may support the testicles from below. And if you prefer I might say: It was this that he taught them, viz., that which R. Abbahu stated in the name of R. Johanan: It has a limit; from the corona downward [touch] is permitted.
Babylonian Talmud Niddah 13a