Dashboards & Visualizations

Restrict users from using asterisks(*) in form inputs

kranthimutyala
Path Finder

Hi Guys,

I have 2 text inputs and want to restrict an user from entering (asterix *) in both the input fields at the same time.If they enter Asterix in any one of the field ,it is accepted but both inputs as ( * ) at the same time is not accepted. If both were entered as aestrisk ( * ) we need to show a pop-up like Please enter at least one input,

Please help.
Thank you in advance.

0 Karma
1 Solution

gaurav_maniar
Builder

Hi @kranthimutyala ,

Please try the following code,
- it will give a javascript alert-popup in case of both inputs are "*" or empty.
- the 3 panels will only be shown once, input values are given as per the requirement.
- if any panel result is zero, the panel will be hidden.

Please Accept & Up-vote the answer if it helps.

happy splunking......!!!!

<form script="default_token.js">
  <label>Default Token</label>
  <fieldset submitButton="true">
    <input type="text" token="token1">
      <label>Field</label>
    </input>
    <input type="text" token="token2">
      <label>Field2</label>
    </input>
  </fieldset>
  <row depends="$panel1$">
    <panel>
      <title>Test1</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel1"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel1">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
  <row depends="$panel2$">
    <panel>
      <title>Test2</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel2"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel2">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
  <row depends="$panel3$">
    <panel>
      <title>Test3</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel3"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel3">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
</form>

Javascript - default_token.js
require([
'underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/tableview',
'splunkjs/mvc/searchmanager',
'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView, SearchManager) {

    var tokens = mvc.Components.get("default");

    $(document).on("click", "#submit", function(e){
        var tok1 = tokens.get("token1");

        var tok2 = tokens.get("token2");

        if ((tok1 == undefined || tok1 == "" || tok1 == "*") && (tok2 == "*" || tok2 == undefined || tok2 == "")){
            tokens.set("js_token", undefined);
            alert("Please enter atleast one input.")
        } else {
            if (tok1 == undefined || tok1 == ""){
                tokens.set("token1", "*");
            }

            if (tok2 == undefined || tok2 == ""){
                tokens.set("token2", "*");
            }

            tokens.set("js_token", "1");
        }
    });
});

View solution in original post

gaurav_maniar
Builder

Hi @kranthimutyala ,

Please try the following code,
- it will give a javascript alert-popup in case of both inputs are "*" or empty.
- the 3 panels will only be shown once, input values are given as per the requirement.
- if any panel result is zero, the panel will be hidden.

Please Accept & Up-vote the answer if it helps.

happy splunking......!!!!

<form script="default_token.js">
  <label>Default Token</label>
  <fieldset submitButton="true">
    <input type="text" token="token1">
      <label>Field</label>
    </input>
    <input type="text" token="token2">
      <label>Field2</label>
    </input>
  </fieldset>
  <row depends="$panel1$">
    <panel>
      <title>Test1</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel1"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel1">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
  <row depends="$panel2$">
    <panel>
      <title>Test2</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel2"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel2">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
  <row depends="$panel3$">
    <panel>
      <title>Test3</title>
      <table>
        <search>
          <query>index=_internal sourcetype=$token1$ source=$token2$ | eval js_token=$js_token$ | head 1 | table _time, _raw</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <done>
            <condition match="'job.resultCount' == 0">
              <unset token="panel3"></unset>
            </condition>
            <condition match="'job.resultCount' > 0">
              <set token="panel3">true</set>
            </condition>
          </done>
        </search>
      </table>
    </panel>
  </row>
</form>

Javascript - default_token.js
require([
'underscore',
'jquery',
'splunkjs/mvc',
'splunkjs/mvc/tableview',
'splunkjs/mvc/searchmanager',
'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView, SearchManager) {

    var tokens = mvc.Components.get("default");

    $(document).on("click", "#submit", function(e){
        var tok1 = tokens.get("token1");

        var tok2 = tokens.get("token2");

        if ((tok1 == undefined || tok1 == "" || tok1 == "*") && (tok2 == "*" || tok2 == undefined || tok2 == "")){
            tokens.set("js_token", undefined);
            alert("Please enter atleast one input.")
        } else {
            if (tok1 == undefined || tok1 == ""){
                tokens.set("token1", "*");
            }

            if (tok2 == undefined || tok2 == ""){
                tokens.set("token2", "*");
            }

            tokens.set("js_token", "1");
        }
    });
});

gaurav_maniar
Builder

you can change the query, just add eval js_token = $js_token$ in your query.

0 Karma

niketn
Legend

@kranthimutyala please try the following run anywhere example based on Splunk's _internal index which runs the SPL only if both values in the text boxes are not asterisk. If they are you get a panel with error message and you would need to reset the values in both text boxes afterwards to make they SPL run again.

Please try out and confirm! Let me know if you need any help understanding components of the the Simple XML run any where dashboard code.

<form>
  <label>Both Text input should not allow asterisk</label>
  <fieldset submitButton="false">
    <input type="text" token="txtLogLevel">
      <label>Provide Log Level</label>
      <change>
        <condition match="$value$==&quot;*&quot; AND $txtSourceType$==&quot;*&quot;">
          <unset token="tokLogLevel"></unset>
        </condition>
        <condition>
          <set token="tokLogLevel">$value$</set>
        </condition>
      </change>
      <default>INFO</default>
    </input>
    <input type="text" token="txtSourceType">
      <label>Provide Log Level</label>
      <change>
        <condition match="$value$==&quot;*&quot; AND $txtLogLevel$==&quot;*&quot;">
          <unset token="tokSourceType"></unset>
        </condition>
        <condition>
          <set token="tokSourceType">$value$</set>
        </condition>
      </change>
      <default>splunkd</default>
    </input>
  </fieldset>
  <row>
    <panel>
      <table depends="$tokLogLevel$,$tokSourceType$">
        <title>Table with Results in case both Text Boxes DO NOT have asterisk</title>
        <search>
          <query>index=_internal sourcetype="$tokSourceType$" log_level="$tokLogLevel$"
 |stats count</query>
          <earliest>-15m</earliest>
          <latest>now</latest>
          <sampleRatio>1</sampleRatio>
        </search>
      </table>
      <html rejects="$logLevelTok$,$tokSourceType$">
         <div>
           <p style="color:red;font-weight:bold;font-size:150%;text-align:left;">
           Asterix * not allowed in both Text Boxes.
           </p>
         </div>
       </html>
    </panel>
  </row>
</form>
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

kranthimutyala
Path Finder

Hi Niket,

Thanks for your answer, I tested it and working well , but i want to include something like whenever the input value is NULL/undefined it should get changed to asterisk * internally (not using default value setting) and search should run on a submit button click.
Whenever both inputs were not mentioned OR both were marked as asterisk() , then please specify at-least one input pop should come up OR Both values are not allowed to use asterisk() at the same time
and lastly if the results of the panel are zero, we need to hide that panel assuming we have more than one panel and these tokens are used across different panels.

If you need any additional info please let me know

Thank you

0 Karma

woodcock
Esteemed Legend

You can reset for inputs in the change area of the fields section by using this syntax:

<unset token="YourFieldNameHere"></unset>
<unset token="form.YourFieldNameHere"></unset>
0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...