By visiting our site, you agree to our privacy policy regarding cookies, tracking statistics, etc.

proper noun logo blue
  • Content Marketing
    • SEO Articles
    • Pillar Content
    • Website Content
    • Content Briefs
  • Off-Page
    • Managed Outreach
  • Optimization
    • Content Optimization
    • Technical SEO
  • Verticals
    • Ecommerce SEO Services
    • SaaS SEO Services
    • Enterprise Business SEO
    • Local SEO Services
  • Case Studies
  • Support
The Roadmap
Book A Demo
Validating Phone Numbers With A Regular Expression
August 17, 2021by adamUncategorized

Validating Phone Numbers With A Regular Expression

Often times when you’re looking to embed a form in your WordPress website or WooCommerce store, you’re going to be using one of 2 different plugins: Gravity Forms or Fast Secure Contact forms. FS Contact Forms is an excellent plugin, it is very customizable, and allows for you to utilize a lot of really great features, but does rely on you to provide any Regex you may need to validate your forms.

For many WordPress website owners, this just doesn’t quite cut it considering they’ve likely never used regex before and most of the copy/paste solutions won’t be formatted for exactly what you may need. In most cases, this is really only an issue though when it comes to phone number validation, so we are going to focus on validating a phone number with regex in this post.

First thing you’ll want to do is open up the form you’re looking to modify, find the Phone field, and then click Show Details to view the full options for the field in question. Once you’re looking at all of the available fields, you will see a field titled “Validation Regex”. Put your cursor in that field and then paste in the following regular expression:

[code] /(?([0-9]{3}))?([s .-]?)([0-9]{3})?([s .-]?)([0-9]{4})/
[/code]

Now, move your cursor in to the input that says “Regex Failed Message” and paste in: Please Enter A Valid Phone Number, or simply create a failure message of your own.

This regular expression will allow for the following number formats to be accepted

– 1234567890
– 123-456-7890
– 123 456-8900
– (123)4567890
– (123) 456 7890
– (123) 456-7890
– (123)456-7890

So how does this regular expression work? Let’s break it down in to it’s pieces:

– The first part ^ means the “start of the line” which will force it to account for the whole string.

– The next block (?([0-9]{3})) is saying that any 3 numbers between 0 and 9 may be used here.

– The next block ?([s .-]?) is saying that after the 3 numbers there can be either a whitespace, no whitespace, a dash, or a period.

– The next block (?([0-9]{3})) is to validate another grouping of 3 numbers between 0 and 9

– The next block ?([s .-]?) is another block that validates either a whitespace, no whitespace, a dash, or a period after the middle 3 numbers

– The final block ([0-9]{4})/ validates a grouping of four numbers between 0 and 9 and then the expression is closed with the slash.

All in all, you should be able to copy and paste the regular expression above in to your FS Contact Form fields and have no issues, but if you’d like to make any modifications or updates, the breakdown above should assist with that as well.

Read More
Share
proper noun logo blue

Proper Noun is an SEO, digital growth, and content marketing agency located in Fort Lauderdale, FL.

Twitter Facebook-f Google-plus-g Pinterest-p Linkedin-in

Contacts

Our Offices
300 SW 1st Ave #155
Fort Lauderdale, FL 33301
Give Us A Call
(954)674-1258

Our Services

  • Content Marketing
  • Ecommerce SEO Services
  • Enterprise Business SEO
  • Managed Outreach
  • Technical SEO
  • Website Optimization

Our Company

  • Blog
  • Contact Us
  • Book A Demo
  • Case Studies
  • Article Writing
  • Website Directory

© 2022 Proper Noun. All Rights Reserved.

Proper Noun is a content marketing agency located in Fort Lauderdale, FL. Proper Noun provides managed content marketing campaigns and managed outreach campaigns for ecommerce businesses, saas products, law firms, real estate, enterprise businesses and much more. We also offer a la carte content creation services.

Markets We Serve

  • Ecommerce SEO
  • Enterprise SEO
  • International SEO
  • National SEO
  • Local SEO
  • B2B SEO
  • Whitelabel SEO
  • SEO Consulting
  • SEO Reseller

Contacts



8 800 2563 123

  emil@yoursite.com

 27 Division St, New York, NY 10002, United States