Eddie Barna wrote:
I thought it would be fun to use JSTL for some form validation instead of Javascript. I am trying to make sure a user only enters numbers in a field called AcctNum. Also i decided to use <fmt:parseNumber /> to test my field. Here's the code:
<c:catch var="parsingError">
<fmt:parseNumber var="Gotcha" value="${param.AcctNum}"/>
</c:catch>
<c:if test="${Gotcha != param.AcctNum}" var="noMatch"/>
Now here's where i run into problems. if i input a string followed by numbers such as iu789 then my catch statement catches the error and stores the following statement in in my variable:
javax.servlet.jsp.JspException: In <parseNumber>, value attribute can not be parsed: "iu789"
This good. Thats what i am trying to do.
Now if i input something like this: 45678uy78 or number followed by a string such as this: 89087yht then the whole page craps out on me and i get this error from the server:
org.apache.jasper.JasperException: javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "test" with value "${Gotcha == param.AcctNum}": An exception occured trying to convert String "45678uy78" to type "java.lang.Long" (null) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
My understanding is that my catch statement is supposed to catch this also but it doesn't. Am I missing something here guys? Please be gentle since i am a novice. TIA
Here is what happens:
- The parsing of "45678uy78" yields the number 45678.
I agree this is weird, but that's the behavior of NumberFormat.parse().
[Sorry haven't had the time to dig further in the javadocs to explain why it would not
throw an exception if you have digits followed by non-digits, but that's
the way it is if no pattern is specified :-(]
- The expression 45678 != "45678uy78" triggers the conversion of "45678uy78" to a Long and fails (see section A.3.6.2 of the spec).
You'd therefore want to insert your conditional tag inside the c:catch.
<c:catch var="parsingError">
<fmt:parseNumber var="Gotcha" value="${param.AcctNum}"/>
<c:if test="${Gotcha != param.AcctNum}" var="noMatch"/>
</c:catch><c:if test="!empty parsingError"> invalid input ... </c:if>
<!-- if I get here, input is valid --> ...
-- Pierre
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
