Problem with mask. It only works for keys along top of keyboard, not in keypad.

Hi,

I got this solution earlier. Thought everything was working until I started using the numeric keypad to enter the numbers instead of the keys along the toop of the keyboard. I'm not sure why that would make a difference. The mask was basically limiting the input to numerics and then formatting it for a phone number or a credit card. Can someone help?

<html>
<head>
<title>Untitled</title>
<script>
//Format Credit Card number
function formatCC(t) {
t = t.replace(/[^0-9]/g,"")
var n = t
if (t.length>4) n = t.substr(0,4)+"-"+t.substr(4,4)
if (t.length>8) n = t.substr(0,4)+"-"+t.substr(4,4)+"-"+t.substr(8,4)
if (t.length>12) n = t.substr(0,4)+"-"+t.substr(4,4)+"-"+t.substr(8,4)+"-"+t.substr(12,4)
return n
}
//End
//Validate Credit Cards
function validateCreditCard(s) {
var v = "0123456789";
var w = "";
for (var i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
var j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
var k = Math.floor(j);
var m = Math.ceil(j) - k;
var c = 0;
for (var i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (var i=0; i<k+m; i++)
c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}

function validateCC(n){
if(n.value.length>0){
if (!validateCreditCard(n.value)) {
  alert('Please enter a valid credit card number');
     n.focus()
  return false;
     }
}
}
//End

//Format telephone number
function formatTel(t) {
t = t.replace(/[^0-9]/g,"")
var n = t
//if (t.length<3) n = "("+t.substr(0,3)
if (t.length>3) n = "("+t.substr(0,3)+")"+t.substr(3,3)
if (t.length>6) n+= "-"+t.substr(6,4)
return n
}
//End
//Validate Phone number
function validTel(t) {
var x = t.value
if (x.length>0){    
     if(x.length<13){
     alert("Invalid telephone number.\r\nPlease enter 10 digit phone number starting with area code")
     t.value = "";
     t.focus()    
     }
}
}
//End
</script>
</head>
<body>
<p>Enter Phone#:<input type='text' name='txtphone' maxlength="13" onkeyup="this.value=formatTel(this.value);" onblur="validTel(this)" />
</p>
<p>Enter Credit Card#:<input type='text' name='ccnum' maxlength="19" onkeyup="this.value=formatCC(this.value);" onblur="validateCC(this)" />
</p>
</body>
</html>
tonyrodriguezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ivo StoykovCommented:
Hi tonyrodriguez,

what is the problem. it seems to work fine...

HTH

I
tonyrodriguezAuthor Commented:
When I run this page and use my keyboard keypad the formatting doesn't work. It only works when I use the numbers along the top. I don't know what it could be. Maybe I should try another machine.
Ivo StoykovCommented:
Hi tonyrodriguez,

is your keypad on? on mine PC it works fine...

regards

I
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

hujiCommented:
ivostoykov is noting a good point. Check if the NumLock light is on, or otherwise press the NumLock key and check if it makes your code work.
Ivo StoykovCommented:
Hi huji,

Please reade the messages above yours before answering... 10x


regards

I
hujiCommented:
My message reads:
"ivostoykov is noting a good point......."
which means I read your comment before posting mine! I didn't mean to duplicate your comment, instead, I wanted to clarify it by directly naming the NumLock button.
In any case, if my answer is accepted by the author (by mistake) you can object to CS, and I will agree with you in such a situation that no points should be given to my comment.
Hope this helps
Huji
Ivo StoykovCommented:
Hi huji,

Hapy Hollidays

regards

I
raj3060Commented:
@tonyrodriguez:
You did use my answer, and dear you did not give me any points.
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21676656.html

I am assuming that by mistake you accepted another answer. You can ask moderators to change the answer on that question.

Also, answer I gave has been tested and works fine. Please make sure that you have numeric keys unlocked.

Cheers..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tonyrodriguezAuthor Commented:
Yeah I guess I accidently accepted the wrong answer.  Who do I ask to correct this?

 Why do I want the numeric keys unlocked? I am able to enter numbers using my number keypad but they don't get formatted. The numbers show up in the field, they just don't get formmated. If I use the numbers along the top of the keys, everything works fine.
tonyrodriguezAuthor Commented:
The problem must be with my keyboard. I try it at work on a different keyboard and enter the numbers using the keypad and everything works fine.
raj3060Commented:
You can ask CS moderator to change the answer.
Thanks
hujiCommented:
I suggest you to ask CS to open this thread, and move it to Hardware topic area.
Huji
raj3060Commented:
@Huji,
We are talking about this question. Follow the link:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21676656.html

@tonyrodriguez accepted wrong answer by mistake.
hujiCommented:
@raj: Pardon me if I seem misinterpretating. What I meant was that "this" question has a wrong answer accepted, so it should be reopened, and then, the author has the option to ask CS to move it to hardware area as well (or put a link quesiton there.)
Best regards,
Huji
raj3060Commented:
@Huji,
My bad.
Happy New Year.
hujiCommented:
Happy new year. :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.