简体   繁体   中英

Validation with javascript using regular expressions on asp.net page doesn't work

I'm trying to run a validation on the textboxes of an asp page using javascript, but my second javascript function doesn't work as expected. I run the validation on the onblur event of each textbox, so I do:

<asp:TextBox ID="txtRegApellidoPat" runat="server" onblur="validaFormato(this);">  </asp:TextBox></td><td><asp:Label ID="valtxtRegApellidoPat" class="validaCad"   runat="server">Únicamente texto</asp:Label>

Then on the textboxes that require integrers I do:

<asp:TextBox ID="txtRegEdad" runat="server" onblur="validaFormatoNum(this);"></asp:TextBox></td><td><asp:Label ID="Label1" class="validaNum" runat="server">* Solo números</asp:Label>

However this last function (validaNum) isn't working, and seems to return false always on the regularexpression.test method that I invoke.

My javascript code is like so:

<script type="text/javascript">
        var formato = document.getElementById('formReg');
        var textos = new Array("txtRegNombre", "txtRegApellidoPat",     "txtRegApellidoMat", "txtRegEdo");
        var numeros = new Array("txtRegEdad", "txtRegTel", "txtRegCP");
        var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d+]/g;
        var pattern2 = /^\d$/g;
        var invalidos1=document.getElementsByClassName("validaCad");
        var invalidos2=document.getElementsByClassName("validaNum");
        function validaFormato(obj) {
            var valido = false;

            if (!pattern.test(obj.value)&&!obj.value=="") {

                            invalidos1[0].style.display = "inline";
                            invalidos1[1].style.display = "inline";
                            invalidos1[2].style.display = "inline";
                            invalidos1[3].style.display = "inline";
                            return valido;
                        }
                        else {
                            invalidos1[0].style.display = "none";
                            invalidos1[1].style.display = "none";
                            invalidos1[2].style.display = "none";
                            invalidos1[3].style.display = "none";
                            valido = true;
                        }
                   return valido;
        }
           function validaFormatoNum(obj) {
                var valido = false;

                if (!pattern2.test(obj.value) && !obj.value == "") {

                invalidos2[0].style.display = "inline";
                invalidos2[1].style.display = "inline";
                invalidos2[2].style.display = "inline";
                return valido;
            }
            else {
                invalidos2[0].style.display = "none";
                invalidos2[1].style.display = "none";
                invalidos2[2].style.display = "none";
                valido = true;
            }

            return valido;
        }
    </script>

I'd really appreciate a hint on this as I can't realize what's the problem. The thing is that the number fields behave erratically, when I write a number on them the first time, the associated label doesn't show, which is correct behavior, but then when I tab through the form and tab out of the number field again, the label shows, as if the value was not a number, then on the following tab, the label doesn't show, then it shows, then it doesn't, etcetera.

I'm really at a loss when it comes to regular expressions so I don't know if the problem is with my second RegExp, but it's simply asking to have a number at the beginning and end of the string, isn't it?

I guess you have to change

var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d+]/g;

to (move the + out of the character class):

var pattern = /([A-Z]|[ÁÉÍÓÚáéíóúÄËÏÖÜäëïöü])+[^\d]+/g;

that could be rewritten:

var pattern = /[A-ZÁÉÍÓÚáéíóúÄËÏÖÜäëïöü]+\D+/g;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM