/    Sign up×
Community /Pin to ProfileBookmark

How to find the pressed key cursor index in a textbox

Hi this is my first message
I want to create an iban textbox. I do everything for it like
[__ ____ ____ ____ ____ ____ ____] masked like this.
I begin to write and the cursor automaticly goes the next index and I mask it again every single key press.
But when I masked it goes the last index when I write a letter in the middle of the iban. I want to get the current index and after the mask go back the current index. My code is like below.
According to code below how can revize the scripts to go to the current index.

[code]

<script>

function MaskIban(str, loc, delim) {
var locs = loc.split(‘,’);
for (var i = 0; i <= locs.length; i++) {
for (var k = 0; k <= str.length; k++) {
if (k == locs[i]) {
if (str.substring(k, k + 1) != delim) {
if (!IsSpecialKeyCode())
str = str.substring(0, k) + delim + str.substring(k, str.length);
}
}
}
}
return str;
}

function RemoveNonIbanChars(str) {
var newStr = ”;
if (str != null) {
for (var i = 0; i <= str.length; i++) {
var s = str.charAt(i);
if ((s >= 0 && s <= 9) || (s >= ‘a’ && s <= ‘z’ )
|| (s >= ‘A’ && s <= ‘Z’)) {
newStr += s;
}
}
}

return newStr;
}

function SetCursor(textbox, index) {
if (textbox.value != null) {
if (textbox.createTextRange) {
var FieldRange = textbox.createTextRange();
//alert(index + “-” + textbox);
FieldRange.moveStart(‘character’, index);
FieldRange.collapse();
FieldRange.select();
}
}
}

function IsSpecialKeyCode() {
if (event.keyCode == 8) //backspace,delete..
return true;
else
return false;
}

function IsTurkishIbanChar(event) {
//alert(event.keyCode);
if (event.keyCode == 9 || event.keyCode == 46 || event.keyCode == 8) return true; //tab,delete,backspace
if (event.ctrlKey && event.keyCode == 86) return true; //CTRL+V
if (event.ctrlKey && event.keyCode == 67) return true; //CTRL+C
if (event.keyCode >= 48 && event.keyCode <= 57) return true;

if (event.keyCode >= 65 && event.keyCode <= 90) return true;
if (event.keyCode >= 96 && event.keyCode <= 122) return true;

return false;
}

function MASK() {
var ibanTextBox = document.getElementById(‘<&#37;=txtIbanTextBox.ClientID %>’);
var str2 = ibanTextBox.value.replace(/_/gi, ”).replace(/ /gi, ”);
var str = ibanTextBox.value.replace(/_/gi, ”).replace(/ /gi, ”);
str = RemoveNonIbanChars(str);
str2 = RemoveNonIbanChars(str2);

str = str.substring(0, 24);

var startIndex = str.length;

if (str2.length != 0)
for (var i = startIndex; i < 24; i++) {
str = str + ‘_’;
}

str = MaskIban(str, ‘2,7,12,17,22,27’, ‘ ‘);
ibanTextBox.value = str.toUpperCase();

var index = str.indexOf(‘_’);
if (str2.length >= 24) index = ibanTextBox.value.length;
SetCursor(ibanTextBox, index);
return true;
}

</script>

<table>
<tr>
<td>
<span id=”IbanHeaderText” runat=”server”><strong>TR</strong>&nbsp;</span>
</td>
<td>
<asp:TextBox ID=”txtIbanTextBox” runat=”server” Width=”165px” MaxLength=”31″
onKeyUp=”javascript:return MASK();” onChange=”javascript:return MASK();”
onKeyDown=”javascript:return IsTurkishIbanChar(event);”
Text=””></asp:TextBox>
</td>
</tr>
</table>
[/code]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @kodanaliz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...