Xpages phone number

738 views Asked by At

In my application I have a field which have to store a phone number. The field must not allow characters but only numbers. The problem is that the format is something like:

0752344234

and if I add something like this, Xpages will save the field as: 752344234 without the 0.

Question: What method can I use to add the 0 in front of the numbers withouth using a string type field?

2

There are 2 answers

0
Knut Herrmann On BEST ANSWER

Use the validateConstraint validator:

<xp:inputText
    id="inputText1"
    value="..."
    disableClientSideValidation="true">
    <xp:this.validators>
        <xp:validateConstraint message="Please enter only digits for phone number">
            <xp:this.regex><![CDATA[^[0-9]*$]]></xp:this.regex>
        </xp:validateConstraint>
    </xp:this.validators>
</xp:inputText>

The regular expression ^[0-9]*$ accepts any number of digits and an empty string. If you want at least one digit then replace * by +.

You don't need to set the input text field as required.

You can decide where you'd like the validation message to appear. Set option disableClientSideValidation="false" to show message as a dialog box on client side or disableClientSideValidation="true" to put the message into xp:message/xp:messages control.

0
PoisonedYouth On

This is an easy example where I use an custom validator to check if the insert value contains only numeric values. You can also use regex expressions to check if the value has a specified form.

<?xml version="1.0" encoding="UTF-8"?>
    <xp:view xmlns:xp="http://www.ibm.com/xsp/core">
        <xp:inputText id="inputText1" required="true">
            <xp:this.validators>
                <xp:customValidator>
                    <xp:this.validate><![CDATA[#{javascript:for(var i=0; i< value.length; i++){
        if(isNaN(value.charAt(i))){
            return false
        }
    }}]]></xp:this.validate>
                </xp:customValidator>
            </xp:this.validators>
        </xp:inputText>
        <xp:button id="button1" value="Validate">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete" immediate="false" save="true">
                <xp:this.action><![CDATA[#{javascript://}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:message id="message1" for="inputText1"></xp:message>
    </xp:view>

To use a custom validator you also have to mark your edit box as required .