Avoiding Duplicate Entries in User Profiles database

Hello,

I am new to wix and without coding skills, but i need to stop users of my site from entering duplicate data in my User Profile collection and to display an error message whenever this occur, while users are trying to update their Profiles.

I have created a profile update dynamic page where a user will have to create a unique user pin or code with numbers that must follow a XXX-XXXX entry pattern (eg. 121-4467). The code must start with 121- followed by four other digits of a user’s choice, but i want each code or pin to be unique to each user by AVOIDING DUPLICATED PIN or CODE.

I know that Hooks can be used to avoid dups but i need a detailed and specific help on how to do the following with codes:

  1. Avoid Duplicate Pin or Codes.
  2. Display error message if a duplicate pin is created during profile update.
  3. Make Submit Button inactive when Pins or Codes are improperly inserted during profile update.

Please can someone help me with the proper code lines to use?
See page capture below:


Thanks,

William Uba.

1 Like

Hi William,

You can create code that will run in the text input’s onChange event, then check whether or no the value is unique

For example:

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#input1").onChange((event, $w) => {
        wixData.query("myCollection")
            .eq('userId', event.target.value)
            .find()
            .then((results) => {
 if (results.length > 0) {
 //id is not unique. show error and disable submit button
                    $w('#submitBtn').disable();
                    $w('#errorText').show();
                } else {
 //id is unique. do nothing
                }
            }).catch((err) => {
 let errorMsg = err;
            });
    });
});

Hello Ido,

Thanks for the response. I will apply code to page and get back to you ASAP.

Thanks again.

Hello Ido,

I added the code to my page but it seem not to work. I don’t know what i may be doing wrong but can you write a specific code details as shown below, i want to be sure i am doing anything wrong.

Collection name name is “UserProfiles”
Field name is “BPIN” with “bpin” as field key
Error Text to display #text265
Submit button #iconbutton5
Input field #input10

Any help will be much appreciated.

Thanks

William

Hello Again:

I got the code to work in preview mode but it is not working in live mode what could be the problem
Please help.

I got the code to work in preview mode but it is not working in live mode what could be the problem
Please help me too!!!

I got the code to work in preview mode but it is not working in live mode what could be the problem. Please help.

import wixData from ‘wix-data’;

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

To make it work in live site make sure that your database is synced. Another thing that you need to keep in mind is that you need set your permissions for the databse, specifically set read and write content in collection to Anyone .

Doesn’t seem to work on mobile…?