Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Adding a string to a phone number.
- This topic has 8 replies, 3 voices, and was last updated 3 years, 7 months ago by Sam Moffatt.
-
AuthorPosts
-
April 8, 2021 at 12:53 PM #44087
Andrew DrapperParticipantHmmm… this is not easy to explain, but…
I use an email to SMS service that nicely sends an SMS text to my customers using their phone number and the string “@textmagic.com”
So I want to take the field with their mobile (cell) phone number and remove any spaces that I might have mistakenly added. Then add the string “@textmagic.com” at the end, then put it into an email field.
Alternatively, I could jump into the API!!!
April 8, 2021 at 4:44 PM #44088
Daniel LeuParticipantYeah, you need a script using the Javascript API for that.
April 9, 2021 at 7:48 PM #44101
Sam MoffattParticipantSomething like this should do it:
record.getFieldValue('fld-fieldid').replace(/[^0-9]/g, '') + "@textmagic.com";
It’ll replace anything that isn’t a number in the field and also add the email suffix. Replace
fld-fieldid
with your field ID.April 12, 2021 at 12:50 AM #44128
Andrew DrapperParticipantWith some fiddling, I got this to work
function SMS() { var mobilephone = record.getFieldValue('fld-314b7a6810eb44828ab4cd717f987b7e').replace(/[^0-9]/g, ''); var SMS = mobilephone + "@textmagic.com"; console.log(SMS); return SMS; } SMS();
But I now have this number in a script field and therefore can not click on the send email symbol.
Can I now past this value into an email field?
April 12, 2021 at 9:19 AM #44130
Daniel LeuParticipantYou just need to store it in an email field:
const email_id = 'fld-xxxx'; record.setFieldValue(email_id, SMS()); document.saveAllChanges();
April 12, 2021 at 9:54 AM #44132
Andrew DrapperParticipantSorry,
I probably need to do a bit more reading into the manual but I now have
function SMS() { var mobilephone = record.getFieldValue('fld-314b7a6810eb44828ab4cd717f987b7e').replace(/[^0-9]/g, ''); var SMS = mobilephone + "@textmagic.com"; //console.log(SMS); return SMS; const email_id = 'fld-d52d76de4eed468f9119a49b50a47f0d'; record.setFieldValue(email_id, SMS()); document.saveAllChanges(); } SMS();
But though I do get a nice “07***726@textmagic.com” in my SMS Script field, it does not place anything in the email field.
April 12, 2021 at 11:00 AM #44134
Daniel LeuParticipantSorry, I wasn’t clear. My code was supposed to be outside of your SMS function.
Here is your modified code with everything put together:
function SMS() { var mobilephone = record.getFieldValue('fld-314b7a6810eb44828ab4cd717f987b7e').replace(/[^0-9]/g, ''); var SMS = mobilephone + "@textmagic.com"; //console.log(SMS); const email_id = 'fld-d52d76de4eed468f9119a49b50a47f0d'; record.setFieldValue(email_id, SMS); document.saveAllChanges(); return SMS; } SMS();
April 12, 2021 at 11:41 PM #44138
Andrew DrapperParticipantThank you so much.
Works like a dream. I could fight on trying to parse the off +44 that some records have, but I think I will just hand modify as they come up.
Presumably, I now hide the script field and let it do its stuff in the background?
Andrew
April 13, 2021 at 12:16 AM #44139
Sam MoffattParticipantYou can hide the script field and it’ll keep working when ever it’s dependent fields are updated. Something simple to take out the +44 assuming it’s at the start of the field:
record.getFieldValue('fld-314b7a6810eb44828ab4cd717f987b7e').replace(/^\+44/, '').replace(/[^0-9]/g, '');
The
^\+44
basically says match the start of the string (the^
character), match a plus sign (the\+
piece,+
has special meaning normally so needs to be escaped) and then match “44”. If the+44
isn’t at the start then remove the^
character (e.g./\+44/
) to just replace that string.I chain this as two
replace
statements just because it makes things a little clearer what is going on (e.g. you’re getting the field value, then you’re replacing the country code and then you’re making everything a number).Regex 101 link: https://regex101.com/r/hPF4kb/1
Regular expressions can be a bit of a challenge to learn but if you’re doing string manipulation they’re an invaluable tool in many languages, including Javascript!
-
AuthorPosts
You must be logged in to reply to this topic.