Question

I'm trying to figure out how this jquery.each() could work on IE 7:

        var todosOsCampos = $(".validate_mail");

         jQuery.each(todosOsCampos, function(){
            //Verifica e-mail
            email = $(this).val();
            if(email!=''){
                er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;
                if(!er.exec(email)) {
                    erro = 1;
                    $(this).css("border", "solid 1px #F00");
                }   
            }
        });

I'm checking the e-mail but on Ie7 is not working properly

I Thought the problem was solved but I was wrong. I've changed the code like the example of user2246674 asked me to do.

        var todosOsCampos = $(".validate_mail");

        todosOsCampos.each(function(){
            email = this.value;
            console.log(email);
            if(email!=''){
                er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;
                if(!er.exec(email)) {
                    erro = 1;
                    this.style.border = "solid 1px #F00";
                }   
            }
        });

But the IE7 is returning the follow message:

Problem with this web page might prevent it from being displayed properly or functioning properly. In the future...

And then I hit the show details button:

Line: 528
Char: 5
Error: Object does not support this property or method

Code: 0

Line 528 correspond to this

email = this.value;

Someone could help me?

Was it helpful?

Solution

I would consider the code suspect - and prone to fail - because the wrong "each" is being used. (But who knows; surprising things can happen.)

There is jQuery.each (e.g. $.each(array, ..)), which is being used:

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays.

And .each (e.g. $(selector).each(..)), which should be used:

Iterate over a jQuery object, executing a function for each matched element.

In this case, use .each to iterate over the jQuery object, such as:

todosOsCampos.each(function(){ .. })

Remember that $(selector) always returns a jQuery object which is a collection of 0 or more elements matched.

If problems persist after correcting the usage, update the post with more details including warning/error messages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top