I have list of objects with key value & pair.when I am separating those to display each information on a specific field . I am getting type error data.validation[i].user is undefined But I have checked the variables have been assigned a value.
var data = {validation:[
{"user":"user may not be empty"},
{"date":"Date may not be null"},
{"mobile":"passengerMobile may not be empty"},
{"mobileSize":"passengerMobile size must be greater than 11"},
{"name":"passengerName may not be empty"},
{"nameSize":"passengerName size must be between 2 and 30"},
]};
var size = data.validation.length;
for(var i =0;i<=size;i++){
if(data.validation[i].user){
$("#username").html("<p>"+data.validation[i].user+"</p>");
}
if($("#mobile").val().length == 0){
$("#mobilesize").html("<p>"+data.validation[i].mobile+"</p>");
}
else if($("#mobile").val().length >= 1){
$("#mobilesize").html("<p>"+data.validation[i].mobilesize+"</p>");
}
}
$("#mobile") indicates an input field Id
Any Ideas are warm welcome
use only less than in tour condition here. because it start form 0,1,2,....
for(var i =0;i<size;i++)
you have length 6, but when fetching last then this should be
data.validation[5].user
You Correct code should be:
var size = data.validation.length;
for(var i =0;i<size;i++){
if(data.validation[i].user){
$("#username").html("<p>"+data.validation[i].user+"</p>");
}
if($("#mobile").val().length == 0){
$("#mobilesize").html("<p>"+data.validation[i].mobile+"</p>");
}
else if($("#mobile").val().length >= 1){
$("#mobilesize").html("<p>"+data.validation[i].mobilesize+"</p>");
}
}