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?

Recommended Answers

All 2 Replies

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));
}
Member Avatar for stbuchok

Please check for duplicates on the server side and send a message to the client.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.