my HTML input field should allow only numbers between 2 and 50 and my html code is below and it's still allowing me to enter unlimited digits. Any help would be appreciated.
<input type="number" min="2" max="50" pattern="[0-9]*">
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}">
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
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
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]