Formatting User Form Data

Often when you have a form, the user inputs data that either doesn’t match the format you desire, or it isn’t as nice looking as it could be. A good example is the phone number. The typical 7 digit phone number looks like 555-1212. Quite often, people entering phone numbers on web forms omit the dashes and enter 5551212 instead. While there isn’t anything wrong with this, there is a chance that an error is harder to spot by the user.
 
What you could do is check the value of that phone number after it has been entered. If the dashes have been left out, you could simply add them. You could do the same thing for a 10 digit phone number or even an international phone number. Of course the more data you handle the more difficult it’s going to be.
 
In this article we’ll take a look at 7 and 10 digit phone numbers and how we can format (or mask) them to look nicer should the user decide not to enter dashes. While we’re doing this we can introduce some handy string methods and talk about a very basic regular expression.
$2.89
- OR -

Overview

Table of Content:

  • A foundation
  • What's a blur and why is it on?
  • How the script will work
  • Manipulating strings: The substring() method
    • Syntax
  • Setting up
  • Trouble on the second ride
  • Don't fall over just yet!
  • RegEx syntax
  • Replace()
  • Calling the regex function
  • Putting things back where they belong
  • Take a look
  • Conclusion
  • Call for ideas!

Reviews

Be the first to write a review

You must me logged in to write a review.