Edit
Contact us
Salocin Group Leaders in data and AI-enabled connected customer experiences
Edit Engineers of connected customer experiences
Join the Dots Independent, data-led media thinking for sustainable growth
Wood for Trees Optimisers of future fundraising performance
  • Home
  • Our services
    • Cloud solutions
    • Data science
    • Modern Data Platform
    • Privacy and AI compliance
  • Our partners
    • Microsoft
    • Apteco
    • Salesforce
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Salocin Group
    • Careers
  • Contact Salocin Group
  • Home
  • Who we are
    • B Corp
    • Careers
  • Our work
  • What we do
    • Intelligent data
    • Marketing technology
    • Transformational CRM
    • Our technology partners
    • Privacy review
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • Contact Edit
  • Home
  • Broadcast media
  • Digital media
  • Print
    • Direct mail
  • Data
    • Our work with Herdify
    • EPiC
  • Media agency
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Join the Dots
    • Careers
  • Contact Join the Dots
  • Home
  • Services
    • Actionable insight
    • Data discovery
    • Data engineering
    • Data hygiene
    • Privacy review
  • Products
    • InsightHub
    • Apteco
    • Microsoft
    • Data management
    • Consent and preference management
  • Our insights
    • Blog
    • Case studies
    • Reports
    • Webinars
    • Whitepapers
  • About Wood for Trees
    • Operating principles
    • Careers
  • Contact Wood for Trees
Blog

How to use RegEx for error checking

By Edit | 6 Oct 2015

We published this a long time ago…

Some of the content in this post might be out of date, and some images and links may no longer work.

Discover who we are and how we may be able to help you today:

Learn more

“Some people, when confronted with a problem, think
‘I know, I’ll use regular expressions.’ Now they have two problems.” – Jamie Zawinski, Netscape Engineer.

Regular Expressions (RegEx) are a pretty nice way of filtering data in a complex manner. However, they can be a bit tricky to get working. This post will go through how you can use RegEx for error checking – for example, getting rid of rubbish inputs from telephone number or e-mail address fields.

How do I use RegEx for error checking?

Firstly, decide what you want to check for errors:

  • Should I only include a certain kind of term? (Are you allowed letters, numbers, spaces or a combination?)
  • Is there a minimum or maximum character length? (Phone numbers must not exceed 15 characters, for example)
  • Are there any terms I need to exclude? (Do you allow calls from 070 numbers?)
  • Does the field need to be in a specific format? (Email address must contain “@”, for example)

Including character types

To include just a certain type of character, firstly, you need to decide what kind of character you want to allow without your set and find the relevant RegEx:

  • Numbers: 0-9
  • lowercase alphabetical: a-z
  • Uppercase alphabetical: A-Z
  • Spaces: \s
  • Words:\w

Regex defines a set using [] and, to include more than one type of item within a set, they can be separated by commas.

For example, I want to include telephone numbers but allow people to type spaces, so I’ll be using the set [0-9,\s].

Because I want to match a string rather than just a character, I need to write a rule that says “all characters within the string must be within this format”. That can be done using the following RegEx:

^[0-9,\s]{1,}$

To break this down:

  • ^ means “must begin with”
  • [0-9,\s] is the set we’re using to just include numbers and spaces
  • {1,} says there must be at least one character of the string within that set and all the following characters must be within that set
  • $ means “must end with”

So, now we have a Regular Expression that says “return strings with only numbers and spaces”. To modify for a different set, you need to change the set being used: ^[0-9,\s]{1,}$

Using character limits

We have almost done this already and it all comes down to is using the curly brackets: {}. Using them allows you to set a limit on the minimum or maximum length your input can be. For example, telephone numbers must be at least 10 digits long and, at the most, contain 15 characters. However, to allow for people to put spaces in, this could be extended to 20 characters.

The Regular Expression is modified to this: ^[0-9,\s][0-9,\s]{10,20}$

Where {10,20} is, it says that “the string must be between 10 and 20 characters”. This can, however, be modified if you require it to be.

Excluding poor inputs

In this example, I do not want any leads from 070 numbers as, in the past, they have yielded poor results. I always wish to exclude 0123456789 as a telephone number as it is a rubbish number only used for testing purposes.

^(?!070)(?!0123456789)[0-9,\s][0-9,\s]{10,15}$

This makes use of the following RegEx modifiers:

  • () groups things and acts as an “and” operation
  • (?!) turns it into “do not include” (known as a “negative lookahead”)

To modify for your own “do not include”, you have to change the characters within the set: ^(?!070)(?!0123456789)[0-9,\s][0-9,\s]{10,15}$

If you have any questions, feel free to get in touch.

Resources

Regexr – For testing RegEx (My personal favourite)

Regex101 – For testing RegEx (But you might prefer this)

Regular-Expressions.info – For reference purposes

We published this a long time ago…

Some of the content in this post might be out of date, and some images and links may no longer work.

Discover who we are and how we may be able to help you today:

Learn more

Share this

  • Email
  • WhatsApp
  • LinkedIn
  • Facebook
  • X (Twitter)

More insights

AI isn’t going to take your job (unless you really want it to) 
Blog

AI isn’t going to take your job (unless you really want it to) 

By Edit | 18 Jun 2024
Customer relationship marketing: How generative AI is revolutionising engagement  
Blog

Customer relationship marketing: How generative AI is revolutionising engagement  

By Edit | 4 Apr 2024
Personalisation as a process
Blog

Personalisation as a process

By Edit | 8 Mar 2024
  • Privacy policy
  • Cookie policy
  • Ts&Cs
  • Report a concern

© 2025 Edit, part of Salocin Group Ltd. All rights reserved. Company no.: 0362​4881. VAT no.: 4208​34911.

Salocin Group Certified B Corporation | Cyber Essentials Certified | British Assessment Bureau, ISO 27001 Information Security Management
Salocin Group
Your cookie preferences

We use cookies to ensure this website functions properly, to analyse website traffic and for marketing purposes.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}