Dashboards & Visualizations

Need help with Regex

zacksoft_wf
Contributor

I have a regex in my dashboard that performs the validation whether the input value in the field is Numeric or Not.  The code/regex works perfectly well. 

I  need some help in modifying the particular regex so that it should also check if the input field contains any special character such as " <double quote> or <space> at the starting or ending or middle of the string (Basically Padding, Spacing or quotes), That would really  help. 

The  regex that I have which only checks numeric is as follows. It is under the if(match(value ...section  , Highlighted in Bold

=================================================
<input type="text" token="selText">
<label>Enter Only Digits</label>
<change>
<eval token="validationResult">if(match(value, &quot;^[0-9]+$&quot;), &quot;Numeric&quot;, &quot;Not Numeric&quot;)</eval>
</change>
</input>
</fieldset>
================================================================================

Labels (1)
Tags (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

It looks like classes aren't recognised here try it this way

<eval token="validationResult">if(match(value, &quot;[^a-zA-Z0-9]&quot;), &quot;Punctuation or space&quot;, &quot;Not Punctuation or space&quot;</eval>

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

<eval token="validationResult">if(match(value, &quot;^[0-9]+$&quot;), &quot;Numeric&quot;, if(match(value, &quot;[[:punct:] ]&quot;), &quot;Punctuation or space&quot;, &quot;Not Numeric&quot;))</eval>
0 Karma

zacksoft_wf
Contributor

I tried to test it , whether the input value in the text box has " <quotation Mark> or space padded anywhere in the beginning or end , But it's not working.
It's rather giving me "malformed expression in eval".



<input type="text" token="selText">
<label>Don't enter any Splunk Un-escapable characters</label>
<change>
<eval token="validationResult">if(match(value, &quot;[[:punct:] ]&quot;), &quot;Punctuation or space&quot;, &quot;Not Punctuation or space&quot;</eval>
</change>
</input>
</fieldset>

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

When I tried to input this 22"455"
Instead of getting the validation command  , I see this "Error in 'eval' command: The expression is malformed."

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It looks like classes aren't recognised here try it this way

<eval token="validationResult">if(match(value, &quot;[^a-zA-Z0-9]&quot;), &quot;Punctuation or space&quot;, &quot;Not Punctuation or space&quot;</eval>

zacksoft_wf
Contributor

Thank you @ITWhisperer , It's  almost perfect.  Just a minor issue. 
Issue : When I input any string or number with padded spaces in front or back it gives that warning, also it gives warning when I enter slash (/) , which is just perfect. The problem occurs, when I input a quotation sign, example NewYork"    or  "New"York , then it throws error and says "Unbalanced quotes". Also, is there a way we can only validate if the space is in the beginning or end ? currently if the space is between strings it's showing validation message too. example: New<space>Yoork
Thank you so much for your help.  I'm pasting the entire dashboard code snippet for your reference.

===============================================================
<form>
<label>TextBox Validation</label>
<fieldset submitButton="false">
<input type="text" token="selText">
<label>Enter your Input (Do not add splunk special charachters that cannot be escaped)</label>
<change>
<eval token="validationResult">if(match(value, "[^a-zA-Z0-9]"), "Punctuation or space", "Not Punctuation or space"</eval>
</change>
</input>
</fieldset>
<row>
<panel>
<table>
<search>
<query>| makeresults
| eval Text="$selText$ is $validationResult$"
| table Text</query>
<sampleRatio>1</sampleRatio>
</search>
<option name="count">20</option>
<option name="dataOverlayMode">none</option>
<option name="drilldown">cell</option>
<option name="percentagesRow">false</option>
<option name="rowNumbers">false</option>
<option name="totalsRow">false</option>
<option name="wrap">true</option>
</table>
</panel>
</row>
</form>
===============================================================

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

You could use a token modifier

| eval Text="$selText|s$ is $validationResult$"

You could include space as a valid character and then trim the value before using it

0 Karma

zacksoft_wf
Contributor

any suggestion about identifying " <double quotes> using regex ?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Which version of splunk are you using because I am not seeing that issue?

0 Karma

zacksoft_wf
Contributor

Version: 8.1.4 When I am inputting the string say, gfgfgf"hh.
It says 'Unbalanced Quotes' instead of the custom message from the else statement.

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

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

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...