0

I want to check for duplicates when inserting a new contact in my database, then showing a msg that the contact is already in the db.
this is what i've done:

 function isDuplicate(names,phone,email,adresa){ 
    var isduplicate=false; 
    for(var i=0;i<addresslist.length; i++){ 
    if(addresslist[i].names.toLowerCase()==names.toLowerCase() 
&& addresslist[i].phone.toLowerCase()==phone.toLowerCase()&&
addresslist[i].email.toLowerCase()==email.toLowerCase()&&
addresslist[i].adresa.toLowerCase()==adresa.toLowerCase()
){ 
    isduplicate=true; 
    } 
    } 
    return isduplicate; 
    } 

later

if(isDuplicate(name,phone,email,adresa)){ 
$('#notice').empty().html('Kontakti ndodhet tashme ne databaze').show('slow'); 
}

and also i want to check that the phone form has only numbers.

if(isNaN(new Number(phone))){ 
$('#notice').empty().html('Telefoni duhet te kete vetem vlera te vlefshme numerike').show('slow'); 
}

is thi code ok?

Edited by pucivogel: forgot

3
Contributors
2
Replies
9
Views
4 Years
Discussion Span
Last Post by stbuchok
2

I think it's ok, but I'd use more like this:

function isDuplicate(names,phone,email,adresa){ 
        var isduplicate=false; 

        for(var i=0,il=addresslist.length, addr; i<il, addr=addresslist[i]; i=i+1){ 
            if(    addr.names.toLowerCase()==names.toLowerCase() 
                && addr.phone.toLowerCase()==phone.toLowerCase()
                && addr.email.toLowerCase()==email.toLowerCase()
                && addr.adresa.toLowerCase()==adresa.toLowerCase()
            )
            { 
                isduplicate=true; 
                break; // Stop loop if it's duplicated
            } 
        } 
        return isduplicate; 
    } 

    // Verify if value is only number
function isNumber(val) {
    return !isNaN(parseInt(val));
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.