Quantcast
Viewing all articles
Browse latest Browse all 5

Regex Newbie – First Lesson

Just a quick post before I leave. During the holiday season, I managed to study some regular expression and come up with few lessons learned. A good start for a regex newbie like me.

Creating code name from text

I’m creating a simple store application and I want to create automatically an item code based on the given item name (only when the user does not enter a code). Here is the regex:

preg_replace('/[^-a-zA-Z0-9]/', '', $name);

Simple amount validation

I need to validate the entered price which needs to be numeric or with two decimal places via JavaScript. Here us the regex:

^[0-9]+(\.[0-9][0-9])?$
# which will match dddd.dd or dddd

That’s it for now.


Viewing all articles
Browse latest Browse all 5

Trending Articles