Home > Domino Tips > Developer > JavaScript > Phone number validation/formatting
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVASCRIPT

Phone number validation/formatting


Jim Bengtson
02.11.2002
Rating: -3.33- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


View member feedback to this tip.

Call this function to validate your phone number, and to format it in (###) ###-#### format. Use this code in the onBlur event of your field to call this function:


validatephone(this)

The returned formatted phone number can be changed (i.e., to ###-###-####, (###) ###.####, or any other format desired by changing the code in the switch statements.



Code

function stripphone(Phone) {
 var fon = "";
 if (Phone != "" ) {
  for (var i = 0; i < Phone.length; i++) {
   var Chars = "0123456789";
   if (Chars.indexOf(Phone.charAt(i)) != -1) {
    fon = fon + Phone.charAt(i);
   }
  }
 }
 return fon;
};

function validatephone(Phone) {
 var tele =  stripphone(Phone.value);
 var acode="";
 var prfx="";
 var sfx="";
 if (tele != "" ) {
  switch(tele.length) {
   case 7:
    prfx=tele.substring(0,3);
    sfx=tele.substring(3);
    return prfx + "-" + sfx;
    break;
   case 10:
    acode="(" + tele.substring(0,3) + ") ";
    prfx=tele.substring(3,6);
    sfx=tele.substring(6);
    return acode + " " + prfx + "-" + sfx;
    break;
   case 11:
    acode=tele.substring(0,1) +" (" + tele.substring(1,4) + ") ";
    prfx=tele.substring(4,7);
    sfx=tele.substring(7);
    return acode + " " + prfx + "-" + sfx;
    break;
   default:
    alert("Unable to interpret Phone Number");
    Phone.focus();
    return tele;
  }
 }
};


MEMBER FEEDBACK TO THIS TIP

It seems that the formatting for this code doesn't work. Has anyone else had this problem?

—Bridget O.


Do you have comments of your own? Let us know.


Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
JavaScript
Trap an attachment path via the Domino file upload control field
Converting Lotus Notes views to XML documents using JavaScript
Prevent errors on iFramed pages with JavaScript
How to add keyboard functionality for Lotus Notes documents
Validate Lotus Notes Domino fields using JavaScript
How to support Flash objects in any Web browser for a Lotus Notes Domino application
How to validate Lotus Notes forms on a Domino server without losing entered data
Ajax code equivalent of the @DBColumn formula for Lotus Notes
A bevy of Notes/Domino development tips
Loading XML from JavaScript

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts