[valid_nanp]

Description

Link: [valid_nanp]
Author: Steve Piercy
Category: String
Version: 8.5.x
License: Public Domain
Posted: Apr. 22, 2010
Updated: Aug. 13, 2010
More by this author...
Determines whether a string contains a valid phone number according to the North American Numbering Plan . Validation ignores all non-numeric characters. Additionally the number must have at least 10 digits and does not begin with "+1". Phone numbers with extensions are permitted, where any digit beyond the first 10 digits forms the extension. Optionally formats the output, substituting "#" for digits, and inserting any other arbitrary character.

Parameters

-number string, required the phone number to test (and optionally format)
-format string, optional the format string

Sample Usage

[valid_nanp('401-285-0696',-format='(###) ###-#### x######')]
[valid_nanp('401-285-0696 x1234',-format='(###) ###-#### x######')]
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
define_tag('valid_nanp', -description='Determines whether a string contains a valid phone number according to the North American Numbering Plan <http://en.wikipedia.org/wiki/North_American_Numbering_Plan>.  Validation ignores all non-numeric characters.  Additionally the number must have at least 10 digits and does not begin with "+1".  Phone numbers with extensions are permitted, where any digit beyond the first 10 digits forms the extension.  Optionally formats the output, substituting "#" for digits, and inserting any other arbitrary character.',
	-required='number', -type='string', -copy,	// the phone number to test (and optionally format)
	-optional='format', -type='string',	// the format string
	-priority='replace');

	local('v') = false;	// var for storing whether a number is valid
	#number = string_replaceregexp(#number,-find='\\D',-replace='');

	// validation requirements according to NANP, with optional extension
	if(integer(#number->substring(1,1)) >= 2
		&& integer(#number->substring(2,1)) <= 8
		&& integer(#number->substring(4,1)) >= 2
		&& #number->size >= 10);
		#v = true;
	/if;

	if(local_defined('format') && #v);
		// format the number only if requested and is a valid phone number
		local('f') = string;	// formatted output
		local('d') = 1;			// digit index position
		iterate(#format->split(''),local('i'));
			if(#number->size > 10 || #d <= 10);
				if(#i=='#');
					#f+=#number->substring(#d,1);
					#d+=1;
				else;
					#f+=#i;
				/if;
			/if;
		/iterate;
		return(#f);
	else;
		// else just return whether the number is valid
		return(#v);
	/if;
/define_tag;

 

Related Tags



Comments

05/01/2010, Steve Piercy
Added -copy to the number parameter
Added -copy to -number parameter so that the tag would not modify the original value of the string.
Email:


Password:



Newest

Most Popular

Support tagSwap.net