The electronic configuration of chlorine is 2, 8, 7. Chlorine will gain 1 electron easily instead of losing 7 electrons. Hence, the valency of chlorine is 1.
The electronic configuration of magnesium is 2, 8, 2. Magnesium will lose 2 electrons easily instead of gaining 6 electrons. Hence, the valency of magnesium is 2.
The electronic configuration of calcium is 2, 8, 8, 2. So the calcium has 2 electrons in its valence shell which it can easily lose. Hence, the valency of calcium is 2.