Don't Forget The Caret^ and the $tick
When using a regular expression for validation.... don't forget the caret^ and the $tick
Let's say, you want to make sure the user types in a 3 digit number.
Simple! Can't be simpler:
Let's test if this suits the following:
"500" -- Yep!
"999" -- Yep!
"123" -- Yep!
All good so far. Let's check that it fails on the following:
"5" -- Yep!
"99" -- Yep!
"ABC" -- Yep!
"" -- Yep!
That's good too. I guess we're done, right?
Uh-oh! It looks like we forget the caret^ and the $tick!
Therefore the following would return a match!
"HEY I like the film 2001 a space odyssey!" -- Oops! matches our expression!
"But 2010 was just junk right?" -- Oops! another match!
What if we use a caret "^" and a dollar sign "$" to anchor the expression to the start and end...
Now we'll get the result we want!
So when using a regular expression for validation, don't forget to anchor them to the front and the back, by using... you guessed it...
the caret^ and the $tick.
(donkey image lifted from an article about Team Building)Next → ← Previous
I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.