The interaction between thermal convection and an external uniform magnetic field in the vertical is numerically simulated within a computational domain of a horizontally periodic convective box between upper and lower rigid plates. The numerical technique is based on a spectral element method developed earlier to simulate natural thermal convection. In this work, it is extended to a magnetoconvection problem. Its main features are the use of rescaled Legendre-Lagrangian polynomial interpolants in expanding the flow variables except the pressure for which a modal expansion in terms of lower order polynomials is used to avoid the complicated staggered grid approach. The technique is validated in the steady roll and oscillatory convective regimes where various experimental and numerical results are available in the literature. The effect of a vertical magnetic field in such a way to inhibit the convective motions has been demonstrated.