Regular Expression

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.

2 thoughts on “Regex Newbie – First Lesson”

  1. thanks for your post, very helpful for a n00b on regex here. I have applied your simple amount validation on my text field in my flash app. I’ll be a regex newbie forever until a project comes up that will use this extensively =P

  2. Thanks for dropping by gensourai23. Haven’t done any heavy regex yet so that’s just it for now.

Leave a reply

Your email address will not be published. Required fields are marked *