Jump to content

Need Help with Auto phone formatting AND Validation


LSW

Recommended Posts

Finally on the requesting help side of things again... smile.gif

 

I am updating a classic ASP app & one of the requests was for auto formatting of the phone number. Now the first upgrade was to add validation which I did with Regular Expressions and it worked fine:

 

 

 .Pattern = "^\([1-9]\d{2}\)\s?\d{3}\-\d{4}{:content:}quot;

 

Now they want auto formatting which is used the JavaScript from Kodyaz. This has validation in it, so I had commented out the regular Expression.

 

But what I end up with is the formatting working fine, but not the validation, so if no area code is given, the first 3 numbers land in the area code, then 3 more, the dash and the last number: (465) 114-3

 

I have been messing around with both trying to get the JS to require all 10 digits or the regular expression to flag the error and neither work.

 

So does anyone have a suggestion on how to tweak the formatting section to require 10 numbers? Full code is at the above link, the validation part is:

 

var maxphonelength = 14;

function ValidatePhone(object){

 var p = phonevalue1

 p = p.replace(/[^\d]*/gi,"")

 if (p.length < 3) {
   object.value=p
 } else if(p.length==3){
   pp=p;
   d4=p.indexOf('(')
   d5=p.indexOf(')')
   if(d4==-1){
     pp="("+pp;
   }
   if(d5==-1){
     pp=pp+")";
   }
   object.value = pp;
 } else if(p.length>3 && p.length < 7){
   p ="(" + p; 
   l30=p.length;
   p30=p.substring(0,4);
   p30=p30+") " 

   p31=p.substring(4,l30);
   pp=p30+p31;

   object.value = pp; 

 } else if(p.length >= 7){
   p ="(" + p; 
   l30=p.length;
   p30=p.substring(0,4);
   p30=p30+") " 

   p31=p.substring(4,l30);
   pp=p30+p31;

   l40 = pp.length;
   p40 = pp.substring(0,9);
   p40 = p40 + "-"

   p41 = pp.substring(9,l40);
   ppp = p40 + p41;

   object.value = ppp.substring(0, maxphonelength);
 }

 

I have tried to cut it down as I really do not see the use of if / else. either it is (XXX) 465-1143 or an error message asking for the area code.

 

PS - I am on the clock with this as I discovered it when I moved it to the test server and we have to get people testing it this week, so any help is appreciated.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...