Learn all you should know about the Atomic Theory, an important topic in chemistry.
Mg - 2e- Mg2+ divalent cation
Al - 3e- Al3+ trivalent cation
On the other hand group 6and 7 elements gain 2 and 1 electrons respectively during their attempt to become stable.
O + 2e- O2- divalent anion
Cl + e- Cl- univalent anion
The number of electrons lost or gained by a particular element gives the valency of that element.
The valency of an element is the number of electrons an atom of that element must lose or gain in order to obtain a noble gas configuration.