Jump to content
  • 0

Require at least one Search Field to have data entered


RussellParker

Question

I have built a "Search and Report" data page with several fields available for search added under "Configure Search Fields".  Each of the fields has a minimum number of characters and is a "Contains" match.  This works perfectly if data is entered in any of them.  The problem is that if no data is entered in ANY of the Search Fields it brings back the entire data set.  I do not want to make any specific field required, I just one at least one of the Search Fields to have had a value entered to search for.  Said another way I want to disallow the "All Blanks" case.  Any thoughts?  Thanks is Advance... Russ

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
5 hours ago, RussellParker said:

I have built a "Search and Report" data page with several fields available for search added under "Configure Search Fields".  Each of the fields has a minimum number of characters and is a "Contains" match.  This works perfectly if data is entered in any of them.  The problem is that if no data is entered in ANY of the Search Fields it brings back the entire data set.  I do not want to make any specific field required, I just one at least one of the Search Fields to have had a value entered to search for.  Said another way I want to disallow the "All Blanks" case.  Any thoughts?  Thanks is Advance... Russ

Hi Russ, welcome to forum!

Try inserting the following code in the footer of your search form:

<SCRIPT LANGUAGE="JavaScript">

 function check()
 {
var first= document.getElementById("Value1_1").value;
var second = document.getElementById("Value2_1").value;
   
if (( first.length  === 0) &  ( second.length  === 0))
{
window.alert("Please enter a value in a field");
return false;
}
}
 document.getElementById("searchID").onclick=check;

</SCRIPT>

 

Link to comment
Share on other sites

  • 0

Thank you!  Unfortunately I tried you code, but I could not get it to work.  With a little pruning I was able to get it to reject everything though, so at least I know I am putting the code in the proper place.

============================================

<SCRIPT LANGUAGE="JavaScript">
 function check()
 {
var first = "";
var second = "";
  
if (( first.length  === 0) &  ( second.length  === 0))
{
window.alert("Please enter a value in a field");
return false;
}
}
 document.getElementById("searchID").onclick=check;
</SCRIPT>
============================================
 

It does not seem to like how you were getting the Values of the Search Fields since if I use:

var first= document.getElementById("Value1_1").value;

var second = document.getElementById("Value2_1").value;

It just sails on past without a problem.  Or an error interestingly enough.  Was I supposed to leave those as written did I need to replace them with something specific to my Search Page?

 

Any thoughts?

 

Link to comment
Share on other sites

  • 0

I was looking at this more and wondering if I needed to change the "Value1_1" and "Value2_1" strings to be specific to the Name or ID of the Controls on my Search Page, but I did not see anything which looked like what thus might be.  Then I found this in the "JavaScript TipsCaspio Form Elements" help article. 

Search Forms

  • Text Field/Text Area/Dropdown/Checkbox/Listbox/Hidden:

          -   ValueX_Y
          -   X is the form element order, which starts at 1 and increments based on the order of the element in the form.
          -   Y is the criteria. It starts at 1 and increments based on criteria order. It is always 1 if the field has no extra criteria.

So this suggests to me I should not call my Search Fields "Controls"... I should call them "Elements".  Further they are not referenced by specific name, but rather by their position in the Form.  So I have two Search Fields, both which are Text Fields... "Registration" and "Name".  In this order a call to:

  document.getElementById("Value1_1").value;

...should theoretically get me the data entered for "Registration" although I do not se how to tell what the available "criteria" are.  I have yet to get this to work though.  As far as I can tell though the moment I add the "document.getElementById(..." call to the code it appears to short-circuit.

If I have the following...

 function check()
 {
   window.alert("1");
   window.alert("2");
 }
 document.getElementById("searchID").onclick=check;
 
Then when I click "Search" I get a pop-up box with "1", then another with "2", and it goes to the Results Page.
 

If I have this instead...

 function check()
 {
   window.alert("1");
   window.alert(document.getElementById('Value1_1').value);
   window.alert("2");
   window.alert(document.getElementById('Value2_1').value);
 }
 document.getElementById("searchID").onclick=check;
 
Then I get a pop-box with "1" and then it goes to the Results Page.   It hits the "document..." code and goes *POOF*
 
Guessing this is just bone-head simple and I am just a little to ignorant of things to understand what I am doing wrong.
 
Russ
 
Link to comment
Share on other sites

  • 0
On 8/1/2017 at 7:54 AM, RussellParker said:

I was looking at this more and wondering if I needed to change the "Value1_1" and "Value2_1" strings to be specific to the Name or ID of the Controls on my Search Page, but I did not see anything which looked like what thus might be.  Then I found this in the "JavaScript TipsCaspio Form Elements" help article. 

Search Forms

  • Text Field/Text Area/Dropdown/Checkbox/Listbox/Hidden:

          -   ValueX_Y
          -   X is the form element order, which starts at 1 and increments based on the order of the element in the form.
          -   Y is the criteria. It starts at 1 and increments based on criteria order. It is always 1 if the field has no extra criteria.

So this suggests to me I should not call my Search Fields "Controls"... I should call them "Elements".  Further they are not referenced by specific name, but rather by their position in the Form.  So I have two Search Fields, both which are Text Fields... "Registration" and "Name".  In this order a call to:

  document.getElementById("Value1_1").value;

...should theoretically get me the data entered for "Registration" although I do not se how to tell what the available "criteria" are.  I have yet to get this to work though.  As far as I can tell though the moment I add the "document.getElementById(..." call to the code it appears to short-circuit.

If I have the following...

 function check()
 {
   window.alert("1");
   window.alert("2");
 }
 document.getElementById("searchID").onclick=check;
 
Then when I click "Search" I get a pop-up box with "1", then another with "2", and it goes to the Results Page.
 

If I have this instead...

 function check()
 {
   window.alert("1");
   window.alert(document.getElementById('Value1_1').value);
   window.alert("2");
   window.alert(document.getElementById('Value2_1').value);
 }
 document.getElementById("searchID").onclick=check;
 
Then I get a pop-box with "1" and then it goes to the Results Page.   It hits the "document..." code and goes *POOF*
 
Guessing this is just bone-head simple and I am just a little to ignorant of things to understand what I am doing wrong.
 
Russ
 

Hi Russ, I can check your datapage if you don't have sensitive info there.

Please paste my script without any changes into the footer  and send me the URL of the datapage.

Link to comment
Share on other sites

  • 0

Well.... As I suspected I was being a dolt.  I had an HTML box above the Search Fields giving instructions.  It was Value1 and "getElementById()" was not working against it and returing Null.  Executing the ".value" method against a Null object was causing the Script to short circuit.  Instead of using "Value1_1" and "Value2_1" I changed it to "Value2_1" and "Value3_1" and your script works perfectly.

Link to comment
Share on other sites

  • 0
On 7/31/2017 at 6:54 AM, Mathilda said:

Hi Russ, welcome to forum!

Try inserting the following code in the footer of your search form:


<SCRIPT LANGUAGE="JavaScript">

 function check()
 {
var first= document.getElementById("Value1_1").value;
var second = document.getElementById("Value2_1").value;
   
if (( first.length  === 0) &  ( second.length  === 0))
{
window.alert("Please enter a value in a field");
return false;
}
}
 document.getElementById("searchID").onclick=check;

</SCRIPT>

 

I tried this code and it seems to be working if the ajax loading is disabled. 

Sharing with you is the modified script that is working even if the Ajax loading in the DataPage is enabled.

<script>

document.addEventListener('BeforeFormSubmit', function() {

var first= document.getElementById("Value1_1").value;
var second = document.getElementById("Value2_1").value;
var third = document.getElementById("Value3_1").value;

if (( first === 0) & ( second === 0) & ( third === 0))
{
event.preventDefault();
window.alert("Please enter a value in a field");
}
});

</script>

You can also refer to this link: https://howto.caspio.com/datapages/ajax-loading/

 

Hope this helps!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...