jarus
jarus

Reputation: 1873

Function code after for loop doesn't get executed ever

I'm trying to create a registration form and validate input fields using javascript, but I'm having difficulties after the for loop executes...

function checkValidation1() {

    var elem = document.getElementsByTagName("input"); 

    for(var i = 0; i < elem.length; i++) {

        if($("#"+elem[i].id).val()=="") {
            $("#"+elem[i].id+"error").html("<img src='images/exclamationsmall.png' title='"+elem[i].name+" should not be blank'>");               

        } else if(elem[i].name=="email") {
            var emailReg = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/;

            if(!emailReg.test($("#"+elem[i].id).val())) {                        
                $("#"+elem[i].id+"error").html("<img src='images/exclamationsmall.png' title='"+elem[i].name+" is invalid'>");
            }
        } else {
            $("#"+elem[i].id+"error").html("");
        }
    } 
    alert("fasfasfasfasfasf");
}

The alert doesn't execute for some reason. Any ideas?

Upvotes: 0

Views: 346

Answers (1)

Wayne
Wayne

Reputation: 60414

Verify that all of your input elements actually have an id attribute. Otherwise this line:

if ($("#" + elem[i].id).val() == "") {

...will result in an expression containing only an octothorpe -- $("#") -- and the following error:

Syntax error, unrecognized expression: #

...which ultimately prevents the code from reaching the alert.

Upvotes: 2

Related Questions