how to do this simple check in JSTL(without extending any java classes and additional JSP functions). I need it like this:
<c:if test="${fn:isNumeric()}">
... do something ...
</c:if>
Thanks in advance.
how to do this simple check in JSTL(without extending any java classes and additional JSP functions). I need it like this:
<c:if test="${fn:isNumeric()}">
... do something ...
</c:if>
Thanks in advance.
If your environment supports the new EL 2.2 feature of invoking non-getter methods on EL objects (which is available in all Servlet 3.0 compatible containers, such as Tomcat 7, Glassfish 3, etc), then you could just use the
String#matches()
method directly in EL.(I'll leave the minus
-
and the thousands and fraction separators,
and.
outside consideration as possible characters which may appear in a technically valid number)Note that the
<c:set>
with the expression in its body implicitly converts any type toString
usingString#valueOf()
. Otherwise thematches()
call in<c:if>
would fail for non-String
types.You can create a custom function as explained in the following tutorials:
Steps to create a function from the above links:
Create a
.tld
file under/WEB-INF
:Create a class with the method (the logic of the method is taken from here, it uses Regular Expression)
And then use it in the JSP as:
If you really insist on
then you could use the following hack.