The valency of magnesium is:
The number of electrons lost or gained by the atom to achieve nearest noble gas configuration is called its valency.
Magnesium has two electrons in the outermost shell. It attains nearest noble gas electronic configuration on losing them.
So, the valency of magnesium is 2.