I have one form in which I have an address field where the user can enter many addresses. It’s up to the user. They can enter many addresses, it’s working fine.
I am checking that the user doesn’t enter the same address twice. For this I wrote some jQuery code.
jQuery('#EnrollmentEnrollUserForm').submit(function(){
// alert(jQuery('#EnrollmentServiceAddress1').val());
// alert(jQuery('.premise-body').html());
var add=jQuery('#EnrollmentServiceAddress1').val();
add=add.toLowerCase();
var address_string =jQuery('table.premise_details').find('tr:first td:last').html();
// alert(address_string);
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
// alert(address[0]);
if(add==address[0]){
alert("These premises already exist. Please enter other premises.");
return false;
}
// checking whether a premise added is same as the added premises or not
// var premise_check = true;
var retval = true;
jQuery('table.premise_details').each(function() {
var address_string = jQuery(this).find('tr:first td:last').html();
// alert(jQuery(this).find('tr:first td:last').html());
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
// alert(address[0]);
if(add==(address[0])){
alert("These premises were already added... Please enter other premises.");
retval = false;
}
});
if(!retval){
return false;
}
});
This code works only sometimes. The problem I am facing is, it’s showing an alert if the user enters the same address again, but the page is loading in an infinite loop.
Can anybody tell me what’s the problem with that.
Thank you for your help!
The reason why you are getting issue is you have call checking your retval after you are finishing you loop of each function, show its taking last set value of retval and creating issue for you, here i have change code and returning false as i match my condtion .
I have edited code.. please check this code