Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Regular expression for numeric values

Avatar

Level 2

Hi:

Due to limitations within our environment, I have to use text fields to enter in numeric data only.  The client would need to be prompted upon entering a letter that this field excepts only numeric characters.

I am using the code below on the change event for the field and it works fine except that the client can not enter decimals or commas.  Does anyone know how to escape the period and commas with this expression?

if (xfa.event.change.match(/[0-9\-]/) == null)
{
    xfa.event.change = "";
    xfa.host.messageBox("Only numbers are permitted for this field.");
}

2 Replies

Avatar

Level 10

Hi,

This script in the change event will give the warning. I am just thinking if script could also delete the offending character:

var vPattern = /[a-z]/;

var result = vPattern.test(xfa.event.newText);


if (result == true)

{

    xfa.host.messageBox("Only numbers are permitted for this field.");

}

Good luck,
Niall

Avatar

Level 2

Thanks!

I never thought about going from the letter side.  DOH!

The way to clear the offending character is to add the follow code to your solution

{

xfa.event.change="";

messageBox...

Thanks again,