By replacing one Al or N atom of aluminum nitride nanocage Al12N12 with an alkaline-earth metal atom, two series of compounds, namely, M@Al12N11 and M@Al11N12 (M = Be, Mg, and Ca), were constructed and investigated in theory. The substituted effect of alkaline-earth metal on the geometric structure and electronic properties of Al12N12 is studied in detail by density functional theory (DFT) methods. The calculated binding energies, HOMO–LUMO gaps, and VIE values of these compounds reveal that they possess high stability, though the NBO and HOMO analyses show that they are also excess electron compounds. Due to the existence of diffuse excess electrons, these alkaline-earth metal-substituted compounds exhibit larger first hyperpolarizabilities (β0) than pure Al12N12 nanocage. In particular, these considered compounds exhibit satisfactory infrared (IR) (>1800 nm) and ultraviolet (UV) (˂ 250 nm) transparency. Therefore, these proposed excess electron compounds with high stability may be regarded as potential candidates for new UV and IR NLO molecules.