Thymeleaf Lists Contains - list

I am using Thymeleaf #lists.contains but cannot get this scenario to work.
I have an ArrayList in Java as such:
List<String> data = new ArrayList<String>();
The list conatins numbers: [1,2,3]. Now in Thymeleaf I want to check if a number is in a list then print my checkbox as checked, I am trying this:
<input type="checkbox" th:if="${#lists.contains(data,1)}" name="checklist" checked="true" />
<input type="checkbox" th:unless="${#lists.contains(data,1)}" name="checklist" />
This does not work. None of the checkboxes are checked. I would have expected for the 1 in the list and the 1 in the if to match and check the checkbox.
For some reason Thymeleaf is not working like this. If I append all the values with something like a c, example ['c1','c2','c3'] and test for that, then it works perfectly. So is it an number/string testing problem and how do I get it to work without appending a character to the number?
If I print the variables out, I get this:
${#lists.contains([1],1)} = false
${#lists.contains([1],'1')} = true
So if I had to use variable on both side, how would I add the quotes?
I tried this but it does not work:
${#lists.contains(data,"numvar")}

If you have an array of Strings, you have to search using a string:
<input type="checkbox" th:if="${#lists.contains(data, '1')}" name="checklist" checked="true" />
Just like the java:
List<String> strings = new ArrayList<>(Arrays.asList("1","2","3"));
System.out.println(strings.contains(1)); // returns false
System.out.println(strings.contains("1")); // returns true
If you have an array of Integers, you have to search using an Integer:
<input type="checkbox" th:if="${#lists.contains(data, 1)}" name="checklist" checked="true" />
Just like the java:
List<Integer> integers = new ArrayList<>(Arrays.asList(1,2,3));
System.out.println(integers.contains(1)); // returns true
System.out.println(integers.contains("1")); // returns false

So the way I fixed this was to do this:
${#lists.contains(data, '' + numvar + '')}
Just to recap, the data variable is an ArrayList of Strings. The numvar variable is an int number. To compare the two you have to wrap the numvar around single quotes, and this is how you have to do it. Thanks Metroids for pointing it out.

to find the value in array, this worked for me:
<input type="checkbox" th:name="plates[]" th:checked="${#arrays.contains(array, 1)} ? 'checked'" th:value="${plate.id}">

Related

Regex pattern should allow only integers from 0 to 100 not allow decimal points

I have an input box and what I need is it should only allow values from 0 to 100 and It should not allow to enter decimal precision should allow only integers like 10, 25,...etc and not 10.5,0.5... etc.
What I tried is as follows:
<input id="discount" name="discount" placeholder="Discount" autocomplete="off" type="number" inputmode="numeric" pattern="[0-9]*" value="0" maxlength="2">
FIDDLE
Need to modify the pattern="[0-9]*".
Please guide me.
If you're trying to restrict a numeric range, the best way to do so is to supply the min and max attributes. Also, if you wish to allow 100, you'll need to increase maxlength.
input#discount:invalid {
background-color: red;
}
<input id="discount" name="discount" placeholder="Discount"
autocomplete="off" type="number" inputmode="numeric"
min="0" max="100" maxlength="3"
pattern="^([0-9]|[1-9][0-9]|100)$">
it should be "^[0-9]*$", add start and end indicator for your pattern

Coldfusion: How to split a string into a set of variables

I'm trying to teach myself ColdFusion.
I have a string coming in from the database in this format:
domain.com
<br/>
www.facebook.com/facebookpage
<br/>
http://instagram.com/instagrampage
It is all coming from #getRacer.txtDescription#. The format of this text will always be the same.
I need to split it into 3 variables. I tried this (derived from the example on the adobe website)
<h3>ListToArray Example</h3>
<cfset myList = ValueList(getRacer.txtDescription)>
<p>My list is a list with <cfoutput>#ListLen(myList)#</cfoutput> elements.
<cfset myArrayList = ListToArray(myList,'<br/>')>
<p>My array list is an array with
<cfoutput>#ArrayLen(myArrayList)#</cfoutput> elements.
I somehow ended up with 11 items in the array.
Thank you
This should work.
<cfset TestSTring = "domain.com<br/>www.facebook.com/facebookpage<br/>http://instagram.com/instagrampage">
<cfset a = TestString.Split("<br/>")>
The reason ListtoArray is displaying 11 items is because ColdFusion treats each character in the delimiter string (<br/>) as a separate delimiter
Based on #Leigh's comment updating my answer to ensure people should learn the Coldfusion APIs rather than fiddling with Java functions, <cfset a = ListToArray(TestString, "<br/>", false, true)> will also work. Thanks Leigh.
Note: The false at the end is for the includeEmptyFields flag and the true is for the multiCharacterDelimiter flag. See the docs.
<cfset myList = ReplaceNoCase(getRacer.txtDescription,'<br/>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
I chose a pipe character because it is unlikely to already exist in your string. If you wanted to account for the possibility that your BR tag may or may not use XML syntax then you could you regex:
<cfset myList = ReReplaceNoCase(str,'<br/?>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>

Regex not working in numeric input field

I am trying this html code to limit the input field to nine-digit rule, with a maximum value possible of 999999999.
<input name="field" type="number" max="999999999" pattern=".{9,9}">
However this code will check the maximum but will allow things like:
9
999
9999
99999
When what should be allowed is:
123456789
111111111
789531156
What is wrong?
I am only interested in html5 solution.
The pattern attribute doesn't seem to work for inputs of type "number" (at least in Chrome). However, you can set the min value like this:
<input name="field" type="number" max="999999999" min="100000000">
Or use a simple "text" input like this:
<input name="field" type="text" pattern="\d{9}">
Demonstration

Convert form input's to [MyId,Form] using regex

I have some large forms & instead of converting these forms to emails by hand I thought it would be faster if I could just search & replace using Dreamweavers RegEx functions -http://www.adobe.com/devnet/dreamweaver/articles/regular_expressions_pt1.html
Basically, I have some input values that I need to pull the input id's from & make into a value like [MyId,Form]
EXAMPLE INPUT:
<input name="PreviousReading100" id="PreviousReading100" type="number" style="width:200px; color:#666;" class="clear-default ForceNumber html5" value="[|PreviousReading100]" />
RESULT REQUIRED:
[PreviousReading100,Form]
--HAVE TRIED--
FIND:
<input\b(?=((?!(/>|id="?[0-9a-zA-Z]*")).)*id="?[0-9a-zA-Z]*").*?/>
REPLACE:
[$2,Form]
RESULT:
[id="PreviousReading100",Form]
Try
Replace <input(\s+\w+="(.*?)")*\s+id="(.+?)"(\s+\w+="(.*?)")* />
With [$3,Form]

REGEX to link terms within displayed text

I have an application that I have inherited that dynamically builds HREF links within the displayed text. All seemed to be working well until we recently did a database change and our list of terms to link started to get returned from the query in a different order. This exposed a bug within the existing REGEX where it tries to place an HREF link within a preexisting HREF. Simply forcing a new ordering on the terms list is not an option. A term could be just one word, could be multiple words and even could be words formatted using HTML.
What would I need to adjust within the REGEX so that it ignores terms within the HREF attribute of an A element? Here is an example of what I am referring to:
<cfset Output = "This is some sample text to show the problem when we have term1 term2." />
<cfloop index="w" list="sample;term1 term2;term1" delimiters=";">
<cfset Output = "." & Variables.Output & "." />
<cfset Output = REReplaceNoCase(Variables.Output, "(?![</]#w#>)(\W)(#w#)(\W)", "\1\2\3", "one") />
<cfset Output = Mid(Variables.Output, 2, Len(Variables.Output)-2) />
</cfloop>
<cfoutput>#Variables.Output#</cfoutput>
Change the first (\W) in the regex to ([^=\w>]) and the second to ([^=\w<])
<cfset Output = REReplaceNoCase(Variables.Output, "(?![</]#w#>)([^=\w>])(#w#)([^=\w<])", "\1\2\3", "one") />