Each of our new members is required to have a Unique code that they enter in the sign up sheet. How do I search the code against the database to see if it is availableand show the user an error if its not ?
Defined Terms: Form Name: Registrationform5 Text Field I want to validate "input8" Database/collection I want to search "AllAvailableCodes" FieldName I want to search "Input 8" values against: "allCodes" (column 2) FieldName I want to validate: "assignedCodes" (column 3) Submit Button: "Button2" After the user clicks the submit ("Button2"), the query should search the value inputted in to input8 against the Field "AllCodes" in the database collection of available codes: "AllAvailableCodes" If input8 does NOT match any value in field "AllCodes" then: ---> it should result in error message that says "Code Invalid" and member registration form should not submit. If the input8 value matches any value in field "AllCodes" then: --> it should validate there has been a "1" inputted in to column 2, field name "AssignedCodes". If there is a "1", then submit successfully. --> If there is no "1" it should send an error message "Code Not Assigned"
Here is my Code so far but I don't know how to finish it:
import wixData from'wix-data'exportfunction button2_click(event,$w){
wixData.query("AllAvailableCodes")
.find()
.then( (results) => {
if(results.items.length > 0) {
let itemsAmount = results.length
let firstItem = results.items[0];
//looking if the entered value is in ---> AllCodes [if ---> YES then ----> ??? do something / if ---> NO ---> do something else]
for (var i = 0; i < itemsAmount; i++) {
if ($w('#input8').value == results.items[i].AllCodes){ } // <------- Matching items found here (YES)
else { } // <------- No matching items found here (NO)
}
} else { }
} )
.catch( (err) => {
let errorMsg = err;
} );
Thank you!!!
Here you can se a live-example,
CREATED JUST 4 YOU!
So please take a look how it works! Study it and then improve it by your needs.
https://russian-dima.wixsite.com/meinewebsite/validation
import wixData from 'wix-data'; $w.onReady(function () { }); export function button1_click(event) {set_NewValue()} function start_VALIDATION (parameter) { wixData.query("Validation") .find() .then( (results) => { if(results.items.length > 0) { let itemsAmount = results.length let firstItem = results.items[0]; for (var i = 0; i < itemsAmount; i++) { if ($w('#input1').value === results.items[i].column1){console.log("KEY-FOUND"), $w('#TXToutput').text = "KEY-FOUND", $w('#TXTkey').text = results.items[i].column1} else {console.log("NO-KEY-FOUND"), $w('#TXToutput').text = "NO-KEY-FOUND", $w('#TXTkey').text = ""} } } else { } }) .catch( (err) => { let errorMsg = err; } ); } export function button2_click(event) {start_VALIDATION()} function set_NewValue() { console.log("SET") let myValue = $w('#input1').value let toInsert = {"column1": myValue}; wixData.insert("Validation", toInsert) .then( (results) => { let item = results; $w('#dataset1').refresh() } ) .catch( (err) => { let errorMsg = err; } ); }
You can also PRESS-F12 to see all the action in CONCOLE (using google-chrome)
EDIT: some code-improvements were done.
import wixData from 'wix-data'; $w.onReady(function () { }); export function button1_click(event) {set_NewValue()} function start_VALIDATION (parameter) { wixData.query("Validation") .find() .then( (results) => { if(results.items.length > 0) { let itemsAmount = results.length let firstItem = results.items[0]; let foundKey = "" for (var i = 0; i < itemsAmount; i++) { console.log(results.items[i].column1) if ($w('#input1').value === results.items[i].column1){foundKey = results.items[i].column1, console.log("KEY-FOUND")} else {console.log("NO-KEY-FOUND")} } if (foundKey>0) {$w('#TXToutput').text = "KEY-FOUND", $w('#TXTkey').text = foundKey} else {$w('#TXToutput').text = "NO-KEY-FOUND", $w('#TXTkey').text = ""} } else { } }) .catch( (err) => { let errorMsg = err; } ); } export function button2_click(event) {start_VALIDATION()} function set_NewValue() { console.log("SET") let myValue = $w('#input1').value let toInsert = {"column1": myValue}; wixData.insert("Validation", toInsert) .then( (results) => { let item = results; $w('#dataset1').refresh() } ) .catch( (err) => { let errorMsg = err; } ); }
🤣🤣🤣 hello again my friend.
Like i see you let me do your work!!!
I gave you the code, so that you complete it bay your own.
As you can see, this part here was not complete.......
for (var i = 0; i < itemsAmount; i++) { if ($w('#input8').value == results.items[i].AllCodes){ } // <------- Matching items found here (YES) else { } // <------- No matching items found here (NO) }
Of course it will not work, because you did anything to get it to work.
if($w('#input8').value == results.items[i].AllCodes){} else{ }
The ---> { } <---- are EMPTY ! ! !
You have to tell the programm what to do, when result = valid
and when result = invalid ! ! ! What shall hapen ???
And please NO-CROSS-POSTINGS !
https://www.wix.com/corvid/forum/community-discussion/how-to-query-database-to-see-if-unique-code-entered-in-member-registration-form-matches-available-assigned-codes-and-show-error-if-not