Pregunta

Could you guys please give me suggestions how to do client-side validation of fields on the fly in GWT application?

I'm talking about checking value of a field after it loses focus so I can inform user that needs to be fixed.

I'm confused because I'm using RequestFactory so there are no real beans on client side which JSR 303 refers to.

Plus I'd like to go beyond @NotNull or @Size constraints. For example I'd like to be able to check if email was entered correctly, or post address contains building number and etc.

Links to libraries and code samples would be very very helpful.

¿Fue útil?

Solución

for on the fly validation - Regex and ChangeListener are your friends.

Register change listeners for your text box fields.

Perform a regex on the contents for acceptable email address.

http://www.regular-expressions.info/email.html

myTextBox.addChangeListener( new ChangeListener() {
   void onChange(Widget sender) {
      if(!sender.getValue().matches("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$")) {
         sender.setFocus();
   }
}

Regex works for JSR 303 validation as well.

@Pattern("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$")
private String sender;

Lastly, I've found this excellent blog post that describes how to do JSR 303 validation directly from within the GWT client (no server required) by extending and customizing AbstractGwtValidatorFactory.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top