We present a fast , easy and real-time method for measuring the laser beam quality factor, M2 , using a spatial light modulator(SLM). We import computer-generated holograms into the spatial light modulator. Then the laser beams are reflected by the SLM and transfer through a fourier transform lens, composed into different kinds of modes which are received by a CCD. The M2 value can be obtained by analyzing and calculating the modal weighting coefficients of different modes. In this paper, two different kinds of softwares , Matlab and VirtualLab, are used to implement the numerical simulations of the experiment process, so that the feasibility of the algorithm can be proved. In the end, we set up the experimental platform and implement the experiments with measuring laser beam quality factor, M2 , using a he-ne laser as the light source. The comparison with the theoretical value and the simulative value proves the reliability of the experiments.