Be and mg have abnormally small size when compared to other alkali metals. Due to this, electrons in the atom are tightly hold and thus, they have high Ionisation energy. Hence, Be and mg donot undergo transition and don't impart characteristic colour to the flame.