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?
Verify that all of your
inputelements actually have anidattribute. Otherwise this line:…will result in an expression containing only an octothorpe —
$("#")— and the following error:…which ultimately prevents the code from reaching the
alert.