The number of electrons present in the outer most shell of an element defines its valency. but if the number of outer shell is more than 8 than you need to subtract it from 8.
for example:-1) lithium-atomic number 3 but 2)nitrogen-atomic number-7
electric configuration- 2.1 electric configuration-2.5
therefore valency is -1 therefore valency is - 8-5=3