为了账号安全,请及时绑定邮箱和手机立即绑定

构造 vaadin 14 文本字段,仅接受数字,不接受其他内容

构造 vaadin 14 文本字段,仅接受数字,不接受其他内容

DIEA 2024-01-25 21:37:35
我需要制作只能接受数字的 vaadin 14 文本字段。文本字段的标准如下1.文本字段只能接受数字,不能接受其他任何内容,因为我想将该文本字段用作手机号码字段。2.以这样的方式进行验证:如果用户尝试输入字母,则文本字段中不得反映任何内容。文本字段中只能输入数字。3.任何警告或错误不得显示在用户界面中,因为我们专门为手机号码制作了文本字段。我尝试过的事情是活页夹,但允许在焦点丢失事件之后输入字母,它们会验证并提供错误消息我不希望出现这种行为。还尝试了 vaadin 数字字段,但允许字符“e”只是简单而直接,我正在寻找仅输入数字的文本字段。如果用户尝试输入字母,则文本字段中不得反映任何内容。
查看完整描述

3 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

服务器端

您可以执行多种选项,

binder.forField(textFieldForNumber)
      .withValidator(new RegexpValidator("Only 1-9 allowed","\\d*"))
      .bind(YourEntity::getNo, YourEntity::setNo);

客户端

还可以使用textField.setPattern(..)方法在客户端执行相同的检查和输入过滤,例如:

textFieldForNumber.setPattern("\\d*");

此外,可以通过以下方式防止输入与模式完全不匹配

textFieldForNumber.setPreventInvalidInput(true);

备用小部件:NumberField

第三种选择是使用NumberField组件。


查看完整回答
反对 回复 2024-01-25
?
临摹微笑

TA贡献1982条经验 获得超2个赞

我找到了答案的解决方案我在 Vaadin 新测试版中提取了整数文本字段的源代码


代码如下


@Tag("vaadin-integer-field")

@HtmlImport("frontend://bower_components/vaadin-text-field/src/vaadin-integer-field.html")

@JsModule("@vaadin/vaadin-text-field/src/vaadin-integer-field.js")

public class BigIntegerField extends AbstractNumberField<BigIntegerField, BigInteger> {


    private static final SerializableFunction<String, BigInteger> PARSER = valueFormClient -> {

        if (valueFormClient == null || valueFormClient.isEmpty()) {

            return null;

        }

        try {

            return new BigInteger(valueFormClient);

        } catch (NumberFormatException e) {

            return null;

        }

    };


    private static final SerializableFunction<BigInteger, String> FORMATTER = valueFromModel -> valueFromModel == null

            ? ""

            : valueFromModel.toString();


    /**

     * Constructs an empty {@code IntegerField}.

     */

    public BigIntegerField() {


          super(PARSER, FORMATTER, Double.MIN_VALUE, Double.MAX_VALUE);

  //      super(PARSER, FORMATTER, new BigInteger(String.valueOf(Integer.MIN_VALUE)), new BigInteger(String.valueOf(Integer.MAX_VALUE)));

    }


    /**

     * Constructs an empty {@code IntegerField} with the given label.

     *

     * @param label

     *            the text to set as the label

     */

    public BigIntegerField(String label) {

        this();

        setLabel(label);

    }


    /**

     * Constructs an empty {@code IntegerField} with the given label and

     * placeholder text.

     *

     * @param label

     *            the text to set as the label

     * @param placeholder

     *            the placeholder text to set

     */

    public BigIntegerField(String label, String placeholder) {

        this(label);

        setPlaceholder(placeholder);

    }


    /**

     * Constructs an empty {@code IntegerField} with a value change listener.

     *

     * @param listener

     *            the value change listener

     *

     * @see #addValueChangeListener(ValueChangeListener)

     */

    public BigIntegerField(

            ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {

        this();

        addValueChangeListener(listener);

    }


    /**

     * Constructs an empty {@code IntegerField} with a value change listener and

     * a label.

     *

     * @param label

     *            the text to set as the label

     * @param listener

     *            the value change listener

     *

     * @see #setLabel(String)

     * @see #addValueChangeListener(ValueChangeListener)

     */

    public BigIntegerField(String label,

            ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {

        this(label);

        addValueChangeListener(listener);

    }


    /**

     * Constructs a {@code IntegerField} with a value change listener, a label

     * and an initial value.

     *

     * @param label

     *            the text to set as the label

     * @param initialValue

     *            the initial value

     * @param listener

     *            the value change listener

     *

     * @see #setLabel(String)

     * @see #setValue(Object)

     * @see #addValueChangeListener(ValueChangeListener)

     */

    public BigIntegerField(String label, BigInteger initialValue,

            ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {

        this(label);

        setValue(initialValue);

        addValueChangeListener(listener);

    }


    /**

     * Sets the minimum value of the field. Entering a value which is smaller

     * than {@code min} invalidates the field.

     * 

     * @param min

     *            the min value to set

     */

    public void setMin(int min) {

        super.setMin(min);

    }


    /**

     * Gets the minimum allowed value of the field.

     *

     * @return the min property of the field

     * @see #setMin(int)

     */

    public int getMin() {

        return (int) getMinDouble();

    }


    /**

     * Sets the maximum value of the field. Entering a value which is greater

     * than {@code max} invalidates the field.

     *

     * @param max

     *            the max value to set

     */

    public void setMax(int max) {

        super.setMax(max);

    }


    /**

     * Gets the maximum allowed value of the field.

     *

     * @return the max property of the field

     * @see #setMax(int)

     */

    public int getMax() {

        return (int) getMaxDouble();

    }


    /**

     * Sets the allowed number intervals of the field. This specifies how much

     * the value will be increased/decreased when clicking on the

     * {@link #setHasControls(boolean) control buttons}. It is also used to

     * invalidate the field, if the value doesn't align with the specified step

     * and {@link #setMin(int) min} (if specified by user).

     * 

     * @param step

     *            the new step to set

     * @throws IllegalArgumentException

     *             if the argument is less or equal to zero.

     */

    public void setStep(int step) {

        if (step <= 0) {

            throw new IllegalArgumentException("The step cannot be less or equal to zero.");

        }

        super.setStep(step);

    }


    /**

     * Gets the allowed number intervals of the field.

     *

     * @return the step property of the field

     * @see #setStep(int)

     */

    public int getStep() {

        return (int) getStepDouble();

    }


}

这实际上解决了我有关手机号码输入的问题。


查看完整回答
反对 回复 2024-01-25
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

您可以使用活页夹在现场进行验证,例如

binder.forField(textFieldForNumber)
      .withValidator(new RegexpValidator("Only 1-9 allowed","\\d*"))
      .bind(YourEntity::getNo, YourEntity::setNo);


查看完整回答
反对 回复 2024-01-25
  • 3 回答
  • 0 关注
  • 37 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信