Form Validation with JavaScript Regular Expressions (part 1)

There is no use collecting information that is clearly incorrect. Unless you're a corporate site administrator, there will probably be a limit to the amount of information you can hold in a database, and therefore it makes no sense to fill the database with incorrect information. Something is needed to ensure that users entering data into your forms are entering data that you can use and that meets your expectations of the data.

Regular Expressions can be used as a way of matching patterns in strings. By patterns, I mean specific occurrences of letters, numbers or punctuation marks. This is an extremely effective way of checking that the information visitors to your site have entered into your forms is correct. When I say correct I mean generally in the correct format; if a user spells their name incorrectly when entering their email address, there's not a lot you can do. But if they enter it and fail for some reason to include the @ character, for example, this at least can be detected. There is a home for regular expressions in any form, and almost any field that requires character input can be checked.

You define a regular expression that matches correctly entered data and then passes the form to the relevant CGI app, or you can define regular expressions that match incorrectly entered data and output an error message if this data is entered. A combination of these two approaches will ensure that you are given data in a format that you are able to work with.

Chris Charlton

Chris CharltonChris, Los Angeles' CSS & ActionScript guru, successfully cannonballed into web development in the late 90's. Always caught up with the latest in Flash, Dreamweaver, Fireworks, and XML, Chris authored premium articles for the largest Dreamweaver/Flash community (www.DMXzone.com) and produced WebDevDesign (iTunes featured), a popular Web Design & Development Podcast. Somewhere, Chris finds time to run an authorized Adobe user group focused around open source and Adobe technologies. Being a big community leader, Chris Charlton remains a resident faculty member of the Rich Media Insitute and lends himself to speak at large industry events, like JobStock, NAB, and FITC Hollywood.

Brain cycles from Chris are always Web Standards, Flash Platform, and accessibility.

See All Postings From Chris Charlton >>

Comments

Be the first to write a comment

You must me logged in to write a comment.