Prohibit users from entering duplicate values

Hi,

Im trying to create a database where i can allow users to input information. However, I want to restrict users from being able to submit information which is already in the database. If they try to submit a duplicate value which is already included i would like the system to reject the submission ideally displaying and error message.

Assistance would be greatly appreciated.

Two methods that I can think of are:

  1. Hook that will check the data before insert (and if needed update as well) that will check if value exists.

  2. On on the click of submitting the button you can do the validation as well.

Example:

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(‘#MySubmitButton’).onClick( async function () {
var results = await wixData.query(“MyCollection”).eq(“title”, $w(“#MyInputField”).value).find();
if (results.length) { /* Your code of what to do if key already exists */ }
else { $w(“#MyDataset”).save();}
})
});

Thanks very much Erez

Hi, I am using the following code to avoid duplicate data entries, but it’s only working in preview only not on the live site. Can any one explain why ? Please help me !

import wixData from ‘wix-data’;

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

Hi i wrote almost the Same facing the same problem, but i noted that my Live DB was empty so when i sync the sand box with the live DB work fine.