Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Smith
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic