Sample01.zul
-------------------------
<?page id="page_listener_sample01" title="" contentType="text/html;charset=UTF-8"?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
<zk>
<window id="win_event_listener_sample01" title="EventListener SAMPLE" width="600px" border="normal" closable="true" onClose="self.detach();" apply="inisoft.mo.gui.Sample01">
<vbox>
<label value="文字驗證:"/>
<textbox id="tbox_input" />
<label value="數字驗證:"/>
<intbox id="ibox_input" />
</vbox>
</window>
</zk>
Sample01.java
-------------------------
package inisoft.mo.gui;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;
/**
* @author Van Chan
*/
public class Sample01 extends GenericForwardComposer {
Page page_listener_sample01;
Window win_event_listener_sample01;
EventListener textboxListener,intboxListener;
Textbox tbox_input;
Intbox ibox_input;
@Override
public void doAfterCompose(Component comp) throws Exception
{
super.doAfterCompose(comp);
tbox_input.setWidth("200px");
addValidationTextboxListener(tbox_input);
ibox_input.setWidth("100px");
addValidationIntboxListener(ibox_input);
}
public void addValidationTextboxListener(Textbox tbox){
textboxListener = new EventListener()
{
public void onEvent(Event event) throws Exception {
String regularExpression="\\s+"; //for check long whitespace character
Textbox textbox=(Textbox)event.getTarget();
String value = "";
value = textbox.getValue()+"";
if("".equals(value) || value.matches(regularExpression)){
String ErrorMsg="Can not be empty, and not only contain blank characters !";
throw new WrongValueException(textbox,ErrorMsg);
}
}
};
tbox.addEventListener(Events.ON_BLUR,textboxListener);
}
public void addValidationIntboxListener(Intbox ibox){
intboxListener = new EventListener()
{
public void onEvent(Event event) throws Exception {
Intbox intbox=(Intbox)event.getTarget();
String value = "";
value = intbox.getValue()+"";
if("null".equals(value) || value.matches("\\s+")){
throw new WrongValueException(intbox, "Can not be empty, and not only contain blank characters !");
}
}
};
ibox.addEventListener(Events.ON_BLUR, intboxListener);
}
}