验证此付款表单时,为什么会出现此错误?

时间:2019-10-18 08:27:55

标签: javascript html validation bootstrap-4

我必须验证付款表格,如果出现错误,则必须在引导警报中显示它。 当我单击“提交”时,我有警报,但是,我关闭了它,然后再次单击“提交”,我收到此错误。

Uncaught TypeError: Cannot read property 'style' of null at HTMLButtonElement.campos_ob

这是我的代码:

HTML

            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <p id="texto">AAAA</p>
        </div>

JS

<script>

        //variables

        var name = document.getElementById('name').value;
        var email = document.getElementById('email').value;

        var password = document.getElementById('password').value;
        var email_paypal = document.getElementById('email-paypal').value;

        //Funcion comprobar si el campo están completos

        window.onload = function () {

            document.getElementById("validar").addEventListener("click", campos_ob);
            document.getElementById("formapago").addEventListener("change", cambio_formapago);
        }


        function campos_ob() {


            if (name == "" || email == "" || password == "") {
                // document.getElementById("texto").innerHTML = "";
                var ppp = document.getElementById('alerta');
                document.getElementById('alerta').style.display = "block";
                document.getElementById('texto').innerHTML = "Error, debes rellenar todos los campos";
                    // let = null;
                //document.getElementById("texto").value = texto;
                //location.reload();
                //event.preventDefault();

            }
            else if (!email.match(/^(?:[^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*|"[^\n"]+")@(?:[^<>()[\].,;:\s@"]+\.)+[^<>()[\]\.,;:\s@"]{2,63}$/i
            )) {

                {
                    document.getElementById('alerta').style.display = "block";
                    document.getElementById('texto').innerHTML = "Email erroneo";

                }
            }
            else if (validar_tarjeta())
            {
                document.getElementById('alerta').style.display = "block";
                document.getElementById('texto').innerHTML = "Tarjeta erronea";

            } else {
                document.getElementById('alerta').style.display = "none";
            }
        }

我希望你能帮助我

0 个答案:

没有答案