Database search - validation

Hi all,

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:

  1. How can I add a validation protocole, where the search query will not run if there are less or than 12 characters?

  2. 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)

Many thanks,

Tony

Hi Tony,

Welcome to the Wix Code forums.

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}$


Read more about Validating User Input with the Settings Panel and Validating User Input with Code .

And for more information, plus an answer to your second question, take a look at the Custom Validations example .

All of this should give you a pretty good background of what’s needed.

Have fun,

Yisrael


  1. a-z0-9 ↩︎

  2. a-z ↩︎

Thanks a lot Yisrael - the validation now works.

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(); 
   
    }); 
            }); 
                    });

sorry Yisrael
I found out that adding .limit(1) to the contains function does the job.

So only the 2nd part of my question needs some assistance,

Best,

Tony

use .eq not .contains

Yes - thanks for bring .eq to my attention.

But I am not sure why just replacing .contain with .eq in the code above does not yield any results at all?

Not sure of your case, but regarding eq and contain…

123 is not equal to 1234
but
123 is contained in 1234

It is 1234 i am searching for…

I was just illustrating the difference.

In order to search for exactly what you want, you should use .eq

I have just figured it out Yisrael: I had to add the following on the top:

export function searchButton_click(event) {

The export function was not previously defined.

Now what I can add to the code to display an error msg if users enter a valid input (i.e. 12 characters) that does not match what is in the database?

As I mentioned above, take a look at the Custom Validations example . This should give you some ideas how to display an error message.

Thanks again.
Can you please point m to articles or examples of how I could collect IP address, geo-location, etc of users searching the database?

You can refer to the post How to Use Google Maps Services in Wix Code . This should give you all you need to get geolocation info on the user.