If you've ever wanted to validate form input such as email addresses and phone numbers, but have found the built-in validations of CFFORM to be lacking, note that CF5 has added new functionality to perform regular expression validation within CFINPUT. All you need is the right expression. If you're not familiar with them, no worries. Here's how to validate both a phone number and email address. The key is the new VALIDATE="regular_expression" and associated PATTERN attributes.

<CFFORM ACTION="">
Phone: <CFINPUT TYPE="Text" NAME="phone" VALIDATE="regular_expression" PATTERN="^(\(?\d{3}\)?)?\s?\d{3}[\s\-]?\d{4}$" MESSAGE="Phone is improperly formatted">

Email: <cfinput type="text" name="email" message="Email address is impoperly formatted." validate="regular_expression" pattern="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$">

<INPUT TYPE="Submit">
</CFFORM>

You can actually run that example, leaving the ACTION="". It will just try to submit to itself. If you give it a valid email and phone, it will submit. If you don't, it won't and will offer an error mesage. Of course, you can change the MESSAGE to suit your taste and would want to change the ACTION and NAME values to those appropriate to your application.

For more information, see my article in the Dec 2001 CDFJ, "Validating Input with Regular Expressions: Little Known Features of CF 5 ". You can read all 30+ of my CFDJ articles online from the "articles" link on my site, www.systemanage.com. Note, however, that the expressions offered above differ slightly from those offered in the article. I have offered these corrected versions in the "comments" area at the front page of the online article.
About This Tutorial
Author: Charles Arehart
Skill Level: Advanced 
 
 
 
Platforms Tested: CF5
Total Views: 56,093
Submission Date: August 24, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • Nadeem, are you asking about ColdFusion? Use CFINPUT type="required" on the fields that should be filled in. By default, that will validate them on submit. And if you're running CF7 or above and wanted to validate when they leave the field, use Validate="onblur" on the CFINPUT. You can even say validate="onblur,onsubmit" to validate at both places.

  • What if the user wants to enter their phone number as 555-555-5555?

  • what if i had a form where i wanted to make sure all the data in the form fields are filled in when they click on submit button

  • ^(http:+\/\/)?(www.)?([a-zA-Z0-9_.-])+.(com|net|org|info|biz|tv|us|cc|name|bz|edu|de|be|co.nz|at|tk)$ that should work for what you are wanting, making it more optional.

  • I'll also add that since writing this tip some years ago, a couple of useful tools have come out to help with building regular expressions. First, for a great little book, see good ol' Ben Forta's "Teach Yourself Regular Expressions in 10 Minutes. Wonderful little intro. As for tools, I frankly can't say enough about a tool that is first and foremost a file search tool (for Windows). But as for regular expressions, it has a great little feature for building them through the most effective and simple expression builder I've seen. I often call it up just to build regular expressions! For more, see a blog entry I did, http://bluedragon.blog-city.com/read/975073.htm.

  • Thanks for posting that, Aaron. One dilemma to keep in mind with doing URL validation is that many URLs don't fit a common pattern. For instance, what if someone put in just http://google.com, above? That's a valid URL, but it would fail that validation. Indeed, some URLs really don't start with www at all, such as http://maps.google.com. Still, people have tried many variations. I'd recommend doing a google search on "validate url expression". You'll see many fine examples. Just be sure to not outsmart your users. :-)

  • ^http:+\/\/www.([a-zA-Z0-9_.-])+.(com|net|org|info|biz|tv|us|cc|name|bz|edu|de|be|co.nz|at|tk)$ I think you are wanting something like this maybe?

  • I have set up a links page where people can submitt their web site address for inclusion on our links page. I want to validate that it is a URL (not necessilly exists just the format is correct) with a regular expression. I have having problems with this. this is the url pattern I am using: ^([a-zA-Z0-9\-]{3,}\.)?([a-zA-Z0-9\-]{3,}\.)([a-zA-Z]{3}|[a-zA-Z]{2}\.[a-zA-Z]{2})!is but it is not working. Any ideas?

  • absolutely beautifull!

Advertisement

Sponsored By...
Powered By...