Regular Expression in field property: format

Hi

I (seem to) remember we can use regular expressions in the format property of a field. For example I would like to format a social security number. Is this possible. I would need an example to see how this is done if it’s possible to format a field this way.

Regards,

this is not possible by using the format.
You could use a column converter for that.

I get it halfway to work, but not the way it should be.

I have a integer field and the content for example is this number: 7562669685040
I would like to have it formatted this way: 756.2669.6850.40

My trial code looks like:

function handleAHVN13Display(value, type) {
	application.output('Value: ' + value + ', Datatype: ' + type);
	var regex = /(^\d{3})(\d{4})(\d{4})(\d{2})/;
	return utils.numberFormat(value, regex);
}

but I don’t know how to add the formatting point. Any hints?

Regards,

jcompagner:
this is not possible by using the format.
You could use a column converter for that.

Hi Robert,

utils.numberFormat() doesn’t take regular expressions.
I think you need to use the following code:

function handleAHVN13Display(value, type) {
   application.output('Value: ' + value + ', Datatype: ' + type);
   var regex = /(^\d{3})(\d{4})(\d{4})(\d{2})/;
   return value.replace(regex, '$1.$2.$3.$4');
}

Hope this helps.

Also can’t you just use utils.numberFormat(value,‘###.###.###.##’)?

it doesn’t work. I get following error message:

Value: 7562669685040, Datatype: INTEGER
Could not sort records, try ShowAll records
Wrapped java.lang.IllegalArgumentException: Multiple decimal separators in pattern "###.####.####.##" (/Users/robert/servoy_workspace/HadesPersons/globals.js#67)
	at /Users/robert/servoy_workspace/HadesPersons/globals.js:67 (handleAHVN13Display)

ROCLASI:
Also can’t you just use utils.numberFormat(value,‘###.###.###.##’)?

Regards,

I guess that makes sense since it it a number formatting.
And what about the regex replace?

Sorry, nearly missed it :-(

Unfortunatly, this is what I get:

Value: 7562669685040, Datatype: INTEGER
TypeError: Cannot find function replace. (/Users/robert/servoy_workspace/HadesPersons/globals.js#68)
TypeError: Cannot find function replace. (/Users/robert/servoy_workspace/HadesPersons/globals.js#68)
	at /Users/robert/servoy_workspace/HadesPersons/globals.js:68 (handleAHVN13Display)

ROCLASI:
And what about the regex replace?

Any idea?

Regards,

Just cast it to a string first:

function handleAHVN13Display(value, type) {
   application.output('Value: ' + value + ', Datatype: ' + type);
   var regex = /(^\d{3})(\d{4})(\d{4})(\d{2})/;
   return String(value).replace(regex, '$1.$2.$3.$4');
}

Hope this helps.

Robert, it helped, the display is as wished :D

Now, I only have to find out how to “reverse” it when inserting data into the same field instead of displaying it formatted.

Regards,

Hi Robert,

You can use the utils.stringReplace() function for this.
Or if you still want to go the RegExp route you can use the following code:

function handleAHVN13Store(value, type) {

    return Number(value.replace(/\./g, '')));
    // return Number(utils.stringReplace(value, '.', '')); // gives same result

}

Hope this helps.

Hi Robert

Thanks for the help. The Problem is now solved! We took your line of code and it works fine!
But the big problem was that we did not define the converted object type! So everytime a wrong number was returned! But if we define the converted object type to text it all works.

Thanks to all for the help.

Regards, Stef