0

Add custom textbox validation from java

asked 2016-06-07 22:34:58 +0800

jvilcayp gravatar image jvilcayp
3 1

updated 2016-06-08 19:23:22 +0800

Hello, i am trying to add a validation in java depending on the values from the back end.

For example, if the field's "type" value is 1, then i want the field "identity" to accept numbers only, otherwise, ot can hold any letter. I've tried to add an attribute to the field in my backend like:

((Textbox) identity).setAttribute("onKeyDown", "/*some js here to allow numbers only*/");

but it does not work.

By the way, this event fires on a "onChange" method from a combobox.

Thanks.

[Edit] Added some sample code

public void onChangeSelect (@ContextParam(ContextType.VIEW) Component view) {
    Component txtNumCI = view.query("#id");
    if (param == 1) {
        ((Textbox) txtNumCI).setMaxlength(8);
    } else {
        ((Textbox) txtNumCI).setMaxlength(11);
    }
}

So, when combobox "type" changes and sets the param value to 1 or 0 (simplified), then maxlength is changed as in sample code, but it should also validate if the value is numbers only (when param = 1) or alphanumeric otherwise.

delete flag offensive retag edit

Comments

I can not comment on @chillworld answer, but the idea is to use dynamic client validation so that i don't have to say "it will validate it later", i don't know how to accomplish this with the visible attribute?

jvilcayp ( 2016-06-08 14:46:21 +0800 )edit

Mvvm pattern? Otherwise show small part of your zul and Java code how you build up your screen

chillworld ( 2016-06-08 17:29:56 +0800 )edit

@chillworld, added some simplified sample code of how it is currently handled (as of today, the validation of numbers only or alphanum is handled on form submit)

jvilcayp ( 2016-06-08 19:25:06 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-06-08 06:06:06 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

updated 2016-06-09 05:57:52 +0800

Why don't you just check for your "backend" and add a textbox or intbox depending the case.
With MVVM this could be done with the visible attribute, or zk 8 the if tag.

Edit:

Looks like you use MVVM =>

So what about this :

<intbox value="@bind(vm.value)" if="${vm.param eq 1}" maxlength="8" />
<textbox value="@bind(vm.value)" if="${vm.param ne 1}"maxlength="11" />

Here is a fiddle.

Greetz chill.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2016-06-07 22:34:58 +0800

Seen: 57 times

Last updated: Jun 09 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More