Hi I am using below code for auto hyphenating credit card txtfield, it it working in IE and Chrome but not in Firefox and probably won't work in safari, I haven't tried though.
it is hyphenating in Firefox but when I want to erase the numbers it only erases numbers upto the last hyphen and not beyond in firefox, and working perfectly in Chrome and IE erasing all the hyphens and numbers.
This is occuring because when you are deleting items within your textbox your event is being fired on key press and it will hit your function and add a hyphen after it reaches one of the points that it should add one.
You could detect if a backspace was pressed and ignore adding the hyphen (you just need to add in an event parameter in your OnKeyPress event) :
well thanks for the help bro I indeed fixed it as you said, and yeah I guessed the same thing but I read somewhere that onkeypress is fired for printable characters only
mamoorkhan
Member
64 Points
52 Posts
How to Auto Hyphenate Credit Card Number??
Jan 30, 2013 06:28 PM|LINK
Hi I am using below code for auto hyphenating credit card txtfield, it it working in IE and Chrome but not in Firefox and probably won't work in safari, I haven't tried though.
HTML is as follows
<asp:TextBox ID="txtCardNumber" runat="server" CssClass="inputField" Style="width: 240px;" MaxLength="19" onblur="javascript:return masking(this.value,this);" onKeyPress="javascript:return masking(this.value,this);"></asp:TextBox>Javascript is as follows
function masking(input, textbox) { if (input.length == 4 || input.length == 9 || input.length == 14) { input = input + '-'; textbox.value = input; } }it is hyphenating in Firefox but when I want to erase the numbers it only erases numbers upto the last hyphen and not beyond in firefox, and working perfectly in Chrome and IE erasing all the hyphens and numbers.
Rion William...
All-Star
26960 Points
4465 Posts
Re: How to Auto Hyphenate Credit Card Number??
Jan 30, 2013 08:20 PM|LINK
This is occuring because when you are deleting items within your textbox your event is being fired on key press and it will hit your function and add a hyphen after it reaches one of the points that it should add one.
You could detect if a backspace was pressed and ignore adding the hyphen (you just need to add in an event parameter in your OnKeyPress event) :
function masking(input, textbox,e) { if (input.length == 4 || input.length == 9 || input.length == 14) { if(e.keyCode != 8){ input = input + '-'; } textbox.value = input; } }using :
Example
raju dasa
Star
14378 Points
2445 Posts
Re: How to Auto Hyphenate Credit Card Number??
Jan 31, 2013 04:58 AM|LINK
Hi,
Instead of onkeypress use onkeyup:
check this thread: http://forums.asp.net/t/1877248.aspx/1?processing+shortcuts+and+Paste+the+selected+text
rajudasa.blogspot.com || blog@opera
mamoorkhan
Member
64 Points
52 Posts
Re: How to Auto Hyphenate Credit Card Number??
Jan 31, 2013 06:38 AM|LINK
well thanks for the help bro I indeed fixed it as you said, and yeah I guessed the same thing but I read somewhere that onkeypress is fired for printable characters only
Rion William...
All-Star
26960 Points
4465 Posts
Re: How to Auto Hyphenate Credit Card Number??
Jan 31, 2013 11:35 AM|LINK
You may want to consider using jQuery to handle key press events as it is a bit more cross-browser compatible.