RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Regular Expression to validate US postal codes

Simple way to validate US postal codes using regular expression and optionally bash. US postall codes accept five digit ZIP number + optional 4 digit code. For example 32344-4444 and 32344 are valid codes but 323445-44 and 323445 are invalid.

#!/bin/bash

# regexp to get a valid US postal code
echo $1 | grep -qE '^[0-9]{5}(-[0-9]{4})?$' 

if [ $? -eq 0 ]; then
	echo "$1 is a valid US postal code."
else
	echo "$1 is an invalid US postal code."
fi

Execution:

$ chmod +x validate_us_postal_code.sh

$ ./validate_us_postal_code.sh 32344-4444
32344-4444 is a valid US postal code.

$ ./validate_us_postal_code.sh 32344
32344 is a valid US postal code.

$ ./validate_us_postal_code.sh 323445-44
323445-44 is an invalid US postal code.

$ ./validate_us_postal_code.sh 323445
323445 is an invalid US postal code.

Share this linux post:

Submit Regular Expression to validate US postal codes in Delicious Submit Regular Expression to validate US postal codes in Digg Submit Regular Expression to validate US postal codes in FaceBook Submit Regular Expression to validate US postal codes in Google Bookmarks Submit Regular Expression to validate US postal codes in Stumbleupon Submit Regular Expression to validate US postal codes in Technorati Submit Regular Expression to validate US postal codes in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download