Skip to main content
Piotr Smolen
Inspiring
May 23, 2022
Answered

Field format for telephone number

  • May 23, 2022
  • 1 reply
  • 8904 views

Hi, I would like to ask for a little help with formatting the field. It's probably simple, but after many tries, I got stuck ...
I have a field to enter the phone number. I don't want to format it any special. I want it to be either the only numbers or the numbers with the "+" sign at the beginning for phones with an international number. Nothing more. Numbers with or without a plus at the beginning.

This topic has been closed for replies.
Correct answer bebarth

Hi,

Isn't it better to manage the keystroke than to check the value at the end?
It's exaggerated, but if you have 200 digits to type, isn't it better to indicate the error immediately while typing rather than checking the 200 characters???

Here is a custom keystroke script which allows that:

var modeleRegEx=/^[+]?\d*$/;
if(!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	if (!modeleRegEx.test(testeChaine)) {
		event.rc=false;
		app.alert("Nieprawidłowy format numeru telefonu.",3);
	}
} else {
	if (!(event.value=="" || modeleRegEx.test(event.value))) {
		event.rc=false;
		app.alert("Nieprawidłowy format numeru telefonu.",3);
	}
}

@+

1 reply

try67
Community Expert
Community Expert
May 23, 2022

This is not really formatting, then. It sounds like what you want is a Validation script, to only allow numbers, with or without a "+" before them. Is that correct?

Piotr Smolen
Inspiring
May 23, 2022

Exactly.

try67
Community Expert
Community Expert
May 23, 2022

You can use this code as the field's custom Validation script, then:

 

if (event.value) {
	event.rc = /^\+?\d+$/.test(event.value);
	if (!event.rc) app.alert("Invalid telephone number format.");
}