Field format for telephone number

Community Beginner ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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.

TOPICS
JavaScript , PDF forms

Views

89

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Adobe Community Professional , May 23, 2022 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.");
}

Likes

Translate

Translate
Adobe Community Professional , May 23, 2022 May 23, 2022

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 alre

...

Likes

Translate

Translate
Enthusiast , May 23, 2022 May 23, 2022

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 (!model
...

Likes

Translate

Translate
Adobe Community Professional ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

Exactly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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.");
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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.");

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

quote

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.");


By @try67

Yes, I use Notepad ++, the native code editor in DC is one big mistake for me, only the Debugger is surpassed by it ...

I have a lot of comments on DC Pro and every 2-3 weeks I am asked for an opinion and a question about the possibility of contact. At the beginning I patiently described my ills and waited for contact, now I don't care, since nobody reads it.

Coming back to the topic, in some forms I have a lot of code with messages for the user and nowhere else has there been a problem with Polish characters, only here. In the worst case, I will edit the message to use words only with Latin alafbet 🙂

Thank you very much for all the answers, they are what I expected !!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

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);
	}
}

@+

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

Sure, it's better, but can be much more complicated. Also, sometimes you need both.

For example. your code allows for just "+" to be entered. To avoid that you will need to (also) use the Validation code I provided.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

Correct! so:

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

@+

@+

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

LATEST

This solution suits me best 🙂
And as for Polish letters ... I deleted the messages, the script simply does not allow you to enter another plus, as well as other characters except numbers. And that was what it was about.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines