aspose file tools*
The moose likes JSF and the fly likes how to prevent ice:commandButton from submitting if there is a validation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "how to prevent ice:commandButton from submitting if there is a validation error" Watch "how to prevent ice:commandButton from submitting if there is a validation error" New topic
Author

how to prevent ice:commandButton from submitting if there is a validation error

John Smith
Ranch Hand

Joined: Aug 21, 2004
Posts: 48
Hi All:

I have the following ice:command code. When it calls the javascript function validate() and if it fails validation I don't want it to submit but unforutnately it submits anywhere.

JSPX CODE:
<ice:commandButton id="newCustomerSave"
value="#{msgs['airfreight.customers.button.submit']}"
action="#{customerBean.saveNewCustomer}"
styleClass="buttonpurple" onclick="validate();" />

JavaScript Code:
function validate() {

mNv=document.getElementById('iceForm:_id47:0:companyName').value;
if (mNv=='') {
alert('Company/Customer name is a required field. Please try again.');
event.returnValue=false;
return false;
}
mNv=document.getElementById('iceForm:_id47:0:iataNumber').value;
if (mNv=='') {
alert('IATA number is a required field. Please try again.');
event.returnValue=false;
return false;
}

mNv=document.getElementById('iceForm:_id47:0:postalCode').value;
if (mNv!='') {
var regEx = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/;
if(regEx.test(mNv))
{

}
else
{
alert('Invalid Postal Code');
event.returnValue=false;
return false;
}

}
return true;
}

Any hint or help would be greatly appreciated it!!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

Hint: use the "Code" button. It makes sample code and XML easier to read.

This is a standard behavior that extends beyond IceFaces and even beyond JSF all the way back to raw HTTP itself.

The "onclick" handler expects a boolean response. If that response is false, the submit will be suppressed. For example:


Customer surveys are for companies who didn't pay proper attention to begin with.
 
wood burning stoves
 
subject: how to prevent ice:commandButton from submitting if there is a validation error