Avoiding Duplicate Entries in user Input form

Hello,

I made a contact form for advanced booking of my service.
But I can’t avoid duplicate contact (phone number) in form.
Actually, I can see the wrong text (#text21 : this number already booked) in preview.
But I can’t see in LIVE page.

I don’t know why it is difference.
Plz help me.

This is my page code.

phone number input form : #input2
database : #booking
field : #contact
filed key : #contact
submit button : #button1
fale text : #text21

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#input2”).onChange((event, $w) => {
wixData.query(“booking”)
.eq(“contact”, event.target.value)
.find()
.then((results)=> {
if (results.length > 0) {
//id is not unique. show error and disable submit button
$w(‘#button1’).disable();
$w(‘#text21’).show();
} else {
//id is unique. do nothing
}
}). catch ((err) => {
let errorMsg = err;
});
});
});

console.log the query results to see if the query is returning a number of results >0 on your live page

Thank you for comment. But I don’t understand.
Could you let me know more more specifically?
I am not good at English, so it is hard to understand…

console.log(results.length);

try this…

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(‘#input2’).onChange( function () {

    console.log("inout changed"); 

    //console.log("event delay triggered"); 

   // setTimeout(() => { 

        //console.log("delay finished, query started"); 

        wixData.query("booking") 
            .eq("contact", $w('#input2').value) 
            .find() 
            .then((results) => { 

let numberOfResults = results.length;

                console.log(numberOfResults); 

if (numberOfResults > 0) {

                    console.log("id is not unique. show error and disable submit button") 

                    $w('#button1').disable(); 
                    $w('#text21').show(); 
                }  **else**  { 
                    console.log("id is unique. do nothing") 
                } 
            }). **catch** ((err) => { 

let errorMsg = err;
});

   // }, 2000); //setting the delay time period in miliseconds 

}); 

});

See the Prevent duplicates example.

I already see it… I can’t do it for 3 days, going crazy.
I’m going to try it by tomorrow, and if it doesn’t work, I’m not going to use Wix!

1 Like

how to allow a single value to be duplicated? help me