Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

empty space validation

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if i enter empty space( click space bar couple of times ) in the field, i want to display error message saying invalid data. But i am not able to

i have tried standard JSF validations
and also custom validations

It works only when the field is a required field

it doesn't work if it's not a required field.

HELP.....................

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of custom validator did you try? Can you share what you did?
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<f:convertNumber integerOnly="true" type="number" pattern="##"/>
f:validateLength min=4 max=4/>

my field data type is Integer


Does JSF automatically trims empty spaces if there is no other data in the field??
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:
Does JSF automatically trims empty spaces if there is no other data in the field??



No. When I use the validate Length tag, it accepts spaces.

I did this with a Validator, where I used the .trim() method similar to the following:
here is tag:




here is entry in faces-config.xml:





There are other solutions:
http://stackoverflow.com/questions/2030086/jsf-trimming-white-spaces
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, you have made the inputText as REQUIRED

In my case if i make the field as Required then it will display error message

Don't make it as required field and TRY

and let me know what it says

To my understanding it will not display any error message
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:In your example, you have made the inputText as REQUIRED

In my case if i make the field as Required then it will display error message

Don't make it as required field and TRY

and let me know what it says

To my understanding it will not display any error message



I removed the required attribute and I still get my error message as expected.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more quick point

did you define "orgName" field as String or Integer type?

I have it as Integer
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:one more quick point

did you define "orgName" field as String or Integer type?

I have it as Integer



You mean defined in the backing bean? It is a String and YES, when I changed this to an Integer field, my trim() space validation stopped working :-(

So now:

123 = passes. Validation never reaches my custom validator
ou812 = fails with message " : 'ou812' is not a number pattern. " validation never reaches my custom validator.
" " (that should be 9 spaces ) = passes... Never reaches custom validator, shows up as null in backing bean...
" 0 " (zero surrounded by 4 spaces on each side) = fails and, for some reason, DOES reach my custom validator

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the reason is that Integer will trigger the default converter. The default converter attempts to convert the whitespace string you enter into an Integer object and it ends up with null, and since it is not required, it passes validation.

The solution is to write a custom converter. In this same app, I now have a simple custom converter (both methods return simple strings) that short circuits JSFs default Integer converter and forces my Validator to run and fail to pass a string composed only of spaces. The following page gives a good explanation of how to write custom converters.

http://www.ibm.com/developerworks/library/j-jsf3/
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah custom converter should solve this problem I think
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic