GeeCON Prague 2014*
The moose likes JSF and the fly likes How do I validate if an input box contains alphanumeric characters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "How do I validate if an input box contains alphanumeric characters?" Watch "How do I validate if an input box contains alphanumeric characters?" New topic
Author

How do I validate if an input box contains alphanumeric characters?

Dan Acuff
Ranch Hand

Joined: Jul 13, 2009
Posts: 62
How do I also validate if the h:inputText is a non-numeric character we display a validation error message as well?

<h:inputText id="quantity"
rendered="#{!((customerSession.priorityProcessingItem != null) and (_cartItem.item.id eq customerSession.priorityProcessingItem.id))}"
value="#{_cartItem.quantity}" required="true" size="2"
maxlength="2">
<f:converter converterId="QuantityToDefaultValueConverter" />
<f:validateLongRange minimum="1" maximum="99" />
<a:support event="onchange" ajaxSingle="true"
actionListener="#{customerSession.calculateCartTotal}"
reRender="minicartAjax, shoppingCartAjax, orderTotalAjax"></a:support>
</h:inputText>


I imagine there must be regular expression usage. But what JSF would I use to validate. I am new to JSF and much help is much appreciated.
Kavita Tipnis
Ranch Hand

Joined: Sep 21, 2008
Posts: 177
You can try using a custom validator
Dan Acuff
Ranch Hand

Joined: Jul 13, 2009
Posts: 62
Hi, thanks for your reply. I think my first attempt was a backing bean attempt.
Can you correct me where I am wrong. I am very new to this but I figure I need to jump in with both feet. deleteFromCart is a fully functional method.

The method itself:

public void isNumber(Integer number, ShoppingCartItem cartId) {
if (number == 0){
deleteFromCart(cartId);
}
}

The interface tie-in:

public void isNumber(Integer number, ShoppingCartItem cartId);


And the calling code again:

<h:inputText id="quantity"
rendered="#{!((customerSession.priorityProcessingItem != null) and (_cartItem.item.id eq customerSession.priorityProcessingItem.id))}"
value="#{_cartItem.quantity}" required="true" size="2"
maxlength="2" validator="#{customerSession.isNumber(_cartItem.item.id)}">
<f:converter converterId="QuantityToDefaultValueConverter" />
<f:validateLongRange minimum="1" maximum="99" />
<a:support event="onchange" ajaxSingle="true"
actionListener="#{customerSession.calculateCartTotal}"
reRender="minicartAjax, shoppingCartAjax, orderTotalAjax"></a:support>
</h:inputText>

The error message I am getting:

/store/webshop/shoppingcart.xhtml @154,82 validator="#{customerSession.isNumber(_cartItem.item.id)}": Method not found: CustomerSession:a2661-s6bxxb-g2f22ox8-1-g2f6y45r-21.isNumber(java.lang.Long)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

You can't define a validation expression as an EL method call, and in fact, EL doesn't support method calls like that anyway. Validators are special classes with a specific organization (they contain a validate() method that throws an exception when validation fails). There are a number of built-in validators, but not many. And a few add-on ones, including a Regular Expression validator that's good for validating things like mixed alpha/numeric stuff.

The "validator" attribute is supposed to reference the name of a validator, either built-in or defined as a validator in faces-config where you map a logical validator name to a validator class and use the logical validator name in the "validator=" attribute declaration. Like I said, it's not an EL expression, just a name.


Customer surveys are for companies who didn't pay proper attention to begin with.
Dan Acuff
Ranch Hand

Joined: Jul 13, 2009
Posts: 62
I totally get what your saying ..I tried the same thing with the valueChangeListener. does not seem to be many options to pass methods with parameters to a backing bean method.

What exactly DO I need to do, just for starters even, to get me on the correct path.

All I want to do is remove the item from the cart if they change the quantity to 0.

<h:inputText id="quantity"
rendered="#{!((customerSession.priorityProcessingItem != null) and (_cartItem.item.id eq customerSession.priorityProcessingItem.id))}"
value="#{_cartItem.quantity}" required="true" size="2"
maxlength="2">
<f:converter converterId="QuantityToDefaultValueConverter" />
<f:validateLongRange minimum="1" maximum="99" />

<a:support event="onchange" ajaxSingle="true"
actionListener="#{customerSession.calculateCartTotal}"
reRender="minicartAjax, shoppingCartAjax, orderTotalAjax"></a:support>
</h:inputText>

Thanks,
Dan
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

I recommend attaching an AJAX action to the onblur event. "onchange" has problems when you do stuff like this, even without JSF.

If your action processor invoked by the onblur action simply removes all cart items from the backing bean's model that have 0 items counts, the reRender will update the display minus the deleted items.

Assuming your cart display is a datatable, the easy way out is to rerender the entire datatable when you update the model. That's usually quick enough to make the user happy.

You do run the risk of annoying people, however. If they attempt to type something like "10" and various GUI hiccups cause the "1" to get lost, there's the possibility of something important being removed and the user being irritated enough not to replace it. I prefer an explicit "delete" button and a confirmation pop-up myself. An explicit button also makes it easier to support people who have JavaScript turned off a la the latest Adobe security bulletin.
 
GeeCON Prague 2014
 
subject: How do I validate if an input box contains alphanumeric characters?