aspose file tools*
The moose likes JSF and the fly likes empty space validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "empty space validation" Watch "empty space validation" New topic
Author

empty space validation

naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129

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.....................

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

What kind of custom validator did you try? Can you share what you did?
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
<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

Joined: Apr 30, 2003
Posts: 820

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

Joined: Apr 12, 2006
Posts: 129
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

Joined: Apr 30, 2003
Posts: 820

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

Joined: Apr 12, 2006
Posts: 129
one more quick point

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

I have it as Integer
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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

Joined: Apr 30, 2003
Posts: 820

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

Joined: Apr 12, 2006
Posts: 129
Yeah custom converter should solve this problem I think
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: empty space validation