Leading plus in number textfields

What would be the easiest way to make a number textfield accepting a leading plus?
It throws an “Alert input error” if the number starts with a plus (e.g. “+123” for 123).

I tried to remove the “+” by using a text change listener, but it takes a little while to fire while the number validation jumps in immediately when lost focus.

I use an electronic scale (with a keyboard emulation driver) to input values in some textfields and unfortunately the leading plus sign cannot be disabled.


You can do it by overriding the default Datatype provided by the framework. For attributes of BigDecimal type it would be as follows.

  • Create a class extending BigDecimalDatatype and override its parse() method:

    package com.company.sales;
    import com.haulmont.chile.core.datatypes.impl.BigDecimalDatatype;
    import org.dom4j.Element;
    import java.math.BigDecimal;
    import java.text.ParseException;
    import java.util.Locale;
    public class MyBigDecimalDatatype extends BigDecimalDatatype {
        public MyBigDecimalDatatype(Element element) {
        public BigDecimal parse(String value, Locale locale) throws ParseException {
            if (value != null && value.startsWith("+")) {
                value = value.substring(1);
            return super.parse(value, locale);
  • Register the class in your metadata.xml with id="decimal":

    <metadata xmlns="http://schemas.haulmont.com/cuba/metadata.xsd">
            <datatype id="decimal" class="com.company.sales.MyBigDecimalDatatype"
                    format="0.####" decimalSeparator="." groupingSeparator=""/>
        <metadata-model root-package="com.company.sales" namespace="sales"/>
  • That’s it. Leading “+” will be discarded in all fields bound to BigDecimal attributes right after the field lost focus.

1 Like

I’m not exactly sure how all this works in background, but it works… thank you!