I have on my page a form with an input text field that connected to the Title field.
Is it possible after someone filling the field to check for duplicate name in the database? Maybe getting True/False answer so I can set up rules for this situation?
It is possible, but how to do it depends on what exactly you want to do.
Do you want to have a button “Check availability”?
Do you want to show an error message once the user clicked submit?
Do you want to show no message but just to reject the double inserting (behind the scenes) ?
Hi J.D
I want to show error message after the user jump to the next field.
is it possible?
Thanks @jonatandor35
I can also work with “Check Availability” button as well.
Yes. That’s also possible.
But there’re more tan one way to do it, I’ll assume you’re using a dataset on your page (if you’re not, it’ll be done differently.
- Change the collection permissions so anyone will be able to read from it (unless you decide it’s not secure, then you’ll need to use a backend function with different code).
Here is a possible code for a collection of up to 1000 users (if you have more, you’ll need to use different code).
let allUsers;
$w.onReady(() => {
$w("#dataset1").onReady(() => {
$w("#dataset1").getItems(0, 1000)
.then( r => {
allUsers = r.items;
$w("#input1").onChange(event => {
let val = $w("#input1").value);
if (val.valid){
allUsers.some(e => e.name === $w("#input1").value) ? $w("#errorMsg").show() : $w("#errorMsg").hide();
}
})
})
})
})
also take into account that this code will fail if a record with this specific name has been submitted after the loading of this page (there’re solutions for this too).
Hi @jonatandor35
Code Not Working
I have on my test page 3 elements:
- $w(“#dataset1”)- is set for Read Only.
- $w(“#input1”) – Normal text field.
- $w(“#errorMsg”)– simple text.
If I want to check for duplicate names in the Title field on my database (dataset1), what should I do?
I have attached a screenshot of the code on my page.
What am I doing wrong?
Why am I getting an error in line 16?
There is also a Wix tutorial that you can look at using too.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-no-database-duplicates
I know, but I want to check for duplicates on item change and not before save.
@jonatandor35 Maybe you can help me with “Check Availability” button?
@jonatandor35 can you help me out? please…