I use an input box to search a database.
Each 12 character text in teh Input field returns some information from a row in the collection and populates a table
I am using the standard code posted by Yoav and Nayeli:
How can I add a validation protocole, where the search query will not run if there are less or than 12 characters?
How can I return a message and possibly show a form if the 12-character text searched by the user returns nothing? (I would like to propose to the users to contact us and ask us to add it)
Regarding your first question… You can set the pattern validation to the Regular Expression [1]{12}$ which checks for exactly 12 alphanumeric characters. If you only want letters: [2]{12}$
But I can’t get the search to run if the Input is valid!
I using the .contains which pulls any similar database entry but not the exact one. Is there another string that can match exactly the 12 character Input? (I have tried hasAll unsuccessfully)
Last, how could I modify the code below to add an error message if the exact Inout is not in the database please?
nd thanks for the warm welcome.
Tony
import wixData from ‘wix-data’;
$w.onReady(function(){
let searchValue = $w(‘#searchInput’).value;
$w(‘#searchButton’).onClick( (event, $w) => {
///12 character validation: if not ok
$w("#searchInput").onCustomValidation((value, reject) => {
if (value.length < 12) {
$w("#text17").text = "Please enter a valid Input"
} else
/// if ok:
wixData.query('my-collection')
/// look at hasAll? "contains" worked
.contains('input', searchValue)
//actually run the query
.find()
//when the query returns:
.then(res =>
{
let foundItems = res.items;
//now find the table component and make it display the results.
$w('#resultsCurrency').rows = res.items;
$w('#resultsCurrency').show();
$w('#text17').hide();
});
});
});