like-gold
like-gold16mo ago

valid phone number

Hi, I recently saw an excellent tutorial by @Conner where only valid emails are entered. Am wondering apart from the VF phone number entity is there another way to ensure valid phone numbers are entered? TIA.
4 Replies
W. Williams (SFT)
Sure. You can use a regex in a javascript step to validate a phone number. Something like this:
// NOTE: This is for use with US numbers and will format is in 888-888-8888 format and sets it back to last_utterance.

const validateAndFormatPhoneNumber = phoneNumber => {
let cleaned = phoneNumber.replace(/\D/g, '')

if (cleaned[0] === '1') {
cleaned = cleaned.slice(1)
}

if (cleaned.length === 10) {
return cleaned.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3')
}

return ''
}

last_utterance = validateAndFormatPhoneNumber('{last_utterance}')

// You need to create a path called error within the javascript path to handle the invalid number.

if (last_utterance.length === 0) return 'error'
// NOTE: This is for use with US numbers and will format is in 888-888-8888 format and sets it back to last_utterance.

const validateAndFormatPhoneNumber = phoneNumber => {
let cleaned = phoneNumber.replace(/\D/g, '')

if (cleaned[0] === '1') {
cleaned = cleaned.slice(1)
}

if (cleaned.length === 10) {
return cleaned.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3')
}

return ''
}

last_utterance = validateAndFormatPhoneNumber('{last_utterance}')

// You need to create a path called error within the javascript path to handle the invalid number.

if (last_utterance.length === 0) return 'error'
like-gold
like-goldOP16mo ago
Thanks Williams, very helpful, will test it out for my country...🔥
W. Williams (SFT)
This should work with all contries, just change the regex and the number of numbers. Is this correct (I just asked ChatGPT)?
In Australia, phone numbers typically follow these formats:

1. **Landline numbers**:
- Local: (0x) xxxx xxxx or (0x)xxxx xxxx
- International: +61 x xxxx xxxx
- Example: (02) 1234 5678 or +61 2 1234 5678

2. **Mobile numbers**:
- Local: 04xx xxx xxx
- International: +61 4xx xxx xxx
- Example: 0412 345 678 or +61 412 345 678

Here's how you might format Australian phone numbers:

- **Landline**: `(0x) xxxx xxxx`
- **Mobile**: `04xx xxx xxx`

For validation and formatting, you would remove any non-numeric characters and ensure the correct length and prefix.
In Australia, phone numbers typically follow these formats:

1. **Landline numbers**:
- Local: (0x) xxxx xxxx or (0x)xxxx xxxx
- International: +61 x xxxx xxxx
- Example: (02) 1234 5678 or +61 2 1234 5678

2. **Mobile numbers**:
- Local: 04xx xxx xxx
- International: +61 4xx xxx xxx
- Example: 0412 345 678 or +61 412 345 678

Here's how you might format Australian phone numbers:

- **Landline**: `(0x) xxxx xxxx`
- **Mobile**: `04xx xxx xxx`

For validation and formatting, you would remove any non-numeric characters and ensure the correct length and prefix.
Happy to help modify the code.
like-gold
like-goldOP16mo ago
yep this is correct,.. i just had a haiku to first extract the number from the user input then saved that extracted and gave to the next haiku to verify using the similar format you have provided. afterwards, added some conditions in the 2nd haiku to output if is valid or not. then the path continues.

Did you find this page helpful?