phoneNumber Validation

I used a text field to enter phone numbers. I used a change script, right click on text property add change script.

This is my script, it’s not the best and could probably be better, but did what I wanted:

import phonenumbers
import re

x = currentValue.value
regex = "^\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$"
	
if re.search(regex,x):
	z = phonenumbers.parse(x, "IN")
	if phonenumbers.is_valid_number(z):
		x = phonenumbers.format_number(phonenumbers.parse(self.props.text, 'US'), phonenumbers.PhoneNumberFormat.NATIONAL)
		self.props.text = x
		self.custom.OK = 1
	else:
		self.custom.OK = 0
else:
	self.custom.OK = 0

Format is (222) 333-3456. You will need to import phononumbers, I think I got it here:

You will need to do your homework to make this work. I can’t help much more than what I wrote up in this post.

Edit, it should be noted, I don’t prevent characters, I only make sure what was entered is correct, hence the custom property “OK”.