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

我需要制作只能接受数字的 vaadin 14 文本字段。文本字段的标准如下

1.文本字段只能接受数字,不能接受其他任何内容,因为我想将该文本字段用作手机号码字段。2.以这样的方式进行验证:如果用户尝试输入字母,则文本字段中不得反映任何内容。文本字段中只能输入数字。3.任何警告或错误不得显示在用户界面中,因为我们专门为手机号码制作了文本字段。

我尝试过的事情是活页夹,但允许在焦点丢失事件之后输入字母,它们会验证并提供错误消息我不希望出现这种行为。

还尝试了 vaadin 数字字段,但允许字符“e”

只是简单而直接,我正在寻找仅输入数字的文本字段。如果用户尝试输入字母,则文本字段中不得反映任何内容。


DIEA
浏览 131回答 3
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组件。

临摹微笑

我找到了答案的解决方案我在 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> {&nbsp; &nbsp; private static final SerializableFunction<String, BigInteger> PARSER = valueFormClient -> {&nbsp; &nbsp; &nbsp; &nbsp; if (valueFormClient == null || valueFormClient.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new BigInteger(valueFormClient);&nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; private static final SerializableFunction<BigInteger, String> FORMATTER = valueFromModel -> valueFromModel == null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : valueFromModel.toString();&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs an empty {@code IntegerField}.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(PARSER, FORMATTER, Double.MIN_VALUE, Double.MAX_VALUE);&nbsp; //&nbsp; &nbsp; &nbsp; super(PARSER, FORMATTER, new BigInteger(String.valueOf(Integer.MIN_VALUE)), new BigInteger(String.valueOf(Integer.MAX_VALUE)));&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs an empty {@code IntegerField} with the given label.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param label&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the text to set as the label&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField(String label) {&nbsp; &nbsp; &nbsp; &nbsp; this();&nbsp; &nbsp; &nbsp; &nbsp; setLabel(label);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs an empty {@code IntegerField} with the given label and&nbsp; &nbsp; &nbsp;* placeholder text.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param label&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the text to set as the label&nbsp; &nbsp; &nbsp;* @param placeholder&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the placeholder text to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField(String label, String placeholder) {&nbsp; &nbsp; &nbsp; &nbsp; this(label);&nbsp; &nbsp; &nbsp; &nbsp; setPlaceholder(placeholder);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs an empty {@code IntegerField} with a value change listener.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the value change listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @see #addValueChangeListener(ValueChangeListener)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {&nbsp; &nbsp; &nbsp; &nbsp; this();&nbsp; &nbsp; &nbsp; &nbsp; addValueChangeListener(listener);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs an empty {@code IntegerField} with a value change listener and&nbsp; &nbsp; &nbsp;* a label.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param label&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the text to set as the label&nbsp; &nbsp; &nbsp;* @param listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the value change listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @see #setLabel(String)&nbsp; &nbsp; &nbsp;* @see #addValueChangeListener(ValueChangeListener)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField(String label,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {&nbsp; &nbsp; &nbsp; &nbsp; this(label);&nbsp; &nbsp; &nbsp; &nbsp; addValueChangeListener(listener);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructs a {@code IntegerField} with a value change listener, a label&nbsp; &nbsp; &nbsp;* and an initial value.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param label&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the text to set as the label&nbsp; &nbsp; &nbsp;* @param initialValue&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the initial value&nbsp; &nbsp; &nbsp;* @param listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the value change listener&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @see #setLabel(String)&nbsp; &nbsp; &nbsp;* @see #setValue(Object)&nbsp; &nbsp; &nbsp;* @see #addValueChangeListener(ValueChangeListener)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public BigIntegerField(String label, BigInteger initialValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ValueChangeListener<? super ComponentValueChangeEvent<BigIntegerField, BigInteger>> listener) {&nbsp; &nbsp; &nbsp; &nbsp; this(label);&nbsp; &nbsp; &nbsp; &nbsp; setValue(initialValue);&nbsp; &nbsp; &nbsp; &nbsp; addValueChangeListener(listener);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Sets the minimum value of the field. Entering a value which is smaller&nbsp; &nbsp; &nbsp;* than {@code min} invalidates the field.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param min&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the min value to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setMin(int min) {&nbsp; &nbsp; &nbsp; &nbsp; super.setMin(min);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Gets the minimum allowed value of the field.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return the min property of the field&nbsp; &nbsp; &nbsp;* @see #setMin(int)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public int getMin() {&nbsp; &nbsp; &nbsp; &nbsp; return (int) getMinDouble();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Sets the maximum value of the field. Entering a value which is greater&nbsp; &nbsp; &nbsp;* than {@code max} invalidates the field.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param max&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the max value to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setMax(int max) {&nbsp; &nbsp; &nbsp; &nbsp; super.setMax(max);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Gets the maximum allowed value of the field.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return the max property of the field&nbsp; &nbsp; &nbsp;* @see #setMax(int)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public int getMax() {&nbsp; &nbsp; &nbsp; &nbsp; return (int) getMaxDouble();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Sets the allowed number intervals of the field. This specifies how much&nbsp; &nbsp; &nbsp;* the value will be increased/decreased when clicking on the&nbsp; &nbsp; &nbsp;* {@link #setHasControls(boolean) control buttons}. It is also used to&nbsp; &nbsp; &nbsp;* invalidate the field, if the value doesn't align with the specified step&nbsp; &nbsp; &nbsp;* and {@link #setMin(int) min} (if specified by user).&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param step&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the new step to set&nbsp; &nbsp; &nbsp;* @throws IllegalArgumentException&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if the argument is less or equal to zero.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setStep(int step) {&nbsp; &nbsp; &nbsp; &nbsp; if (step <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("The step cannot be less or equal to zero.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; super.setStep(step);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Gets the allowed number intervals of the field.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return the step property of the field&nbsp; &nbsp; &nbsp;* @see #setStep(int)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public int getStep() {&nbsp; &nbsp; &nbsp; &nbsp; return (int) getStepDouble();&nbsp; &nbsp; }}这实际上解决了我有关手机号码输入的问题。

慕妹3242003

您可以使用活页夹在现场进行验证,例如binder.forField(textFieldForNumber) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.withValidator(new&nbsp;RegexpValidator("Only&nbsp;1-9&nbsp;allowed","\\d*")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.bind(YourEntity::getNo,&nbsp;YourEntity::setNo);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java