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

Field format for telephone number

  • May 23, 2022
  • 1 reply
  • 8921 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

Ok, it works, although I cannot use Polish letters in the message because there are bushes instead of them.

By the way, an additional question in the same subiect. While trying to solve this problem, I wrote the following script myself.

var tekst=this.getField("Wn_Telefon").valueAsString;
var dl=this.getField("Wn_Telefon").valueAsString.length;
var tekst_wy="";
var znak="";

for (var i = 0; i <dl; i ++){
	znak=tekst.substr(i,1);

	if(i==0){
		tekst_wy=znak;
	}
	
	if(i>0 && znak!="+"){
		tekst_wy = tekst_wy + znak;
	}	
}
event.value=tekst_wy;

The principle of operation is simple. Only the first character can be "+". If "+" appears elsewhere it is ignored.
The script works but has one drawback. Namely, if I enter, for example, "++ 12345 + 678 +", then after pressing TAB and moving to another field, what is needed appears, that is "+12345678". But if I go back to editing this field again, I have "++ 12345 + 678 +" again. So something else is remembered and another displayed. Probably my misunderstanding of events and the order in which the scripts are called.
Of course, as an additional keystroke script is:

if (event.change) event.rc = /^[0-9\+]+$/.test(event.change);

 


A Format script does not change the field's actual value. It just formats it in a specific way after it was entered. When you return to the field the original value re-appears. If you want to change the field's actual value you can use the Validate event, but beware, this change will apply each time you edit it! So if you add a "+" sign to the start of the string, for example, it will continue adding it each time you edit the field, unless you add a condition not to do so if the "+" sign is already there.

 

As for the Polish characters in the error message, that should work. But make sure you're using a plain-text editor and setting the encoding to UTF-8. I recommend using something like Notepad++ for that.

This worked fine for me (I used Google Translate, so forgive any mistakes in the text):

app.alert("Nieprawidłowy format numeru telefonu.");