...

View Full Version : onChance-function for textinput to regulate characters



deafdigit
10-05-2011, 12:00 PM
Hi there,


I need a bit of javascript to solve a problem I'm having.

I have an HTML text-input field, and I would like - simply - that it be impossible to enter a period-character (.).

My best take at it so far is a javascript function called by the onChance-event attached to the <input>-tag, but to be honest, nothing about it really works, so I thought I'd let the pros get their take at it.

Can somebody help with this?


//deafdigit

Philip M
10-05-2011, 12:49 PM
<input type = "text" id = "myfield" onkeyup = "stopDot()" onblur = "stopDot()">

<script type = "text/javascript">
function stopDot(){
var val = document.getElementById("myfield").value;
val = val.replace(/\./g,"");
document.getElementById("myfield").value = val;
}
</script>

Note that you need to block the user pasting in a . character. (onblur)

Why do you want to do this? Surely not to force an integer number??

Surely you mean onchange - not onchance!!


"If you think education is expensive, try ignorance." - Derek Bok (1930-), Harvard University President

deafdigit
10-05-2011, 01:10 PM
Hi Philip,


First of all - yes onchange ofcourse. For some reason my fingers keep typing "onchance" instead.

I want this because I'm danish and I need my users to enter a currency-amount. In danish, we don't enter large numbers as: 1,234,234.97 but rather as: 1.234.234,97 which means my DB efs up everytime someone enters a number like 1.234,97 (it only handles so many decimals). Therefore I need my input field to ignore period-characters.

I stumpled upon this function:


<form name="InputLimiter">

<p>Numbers Only: <input type="text" id="NumbersOnly" onkeypress="return inputLimiter(event,'Numbers')" value="1234567890" /></p>

</form>

<script type="text/javascript" language="javascript">

function inputLimiter(e,allow) {
var AllowableCharacters = '';

if (allow == 'Numbers'){AllowableCharacters='1234567890,';}

var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
if (k!=13 && k!=8 && k!=0){
if ((e.ctrlKey==false) && (e.altKey==false)) {
return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
} else {
return true;
}
} else {
return true;
}
}
</script>

Which seems to do the job. Any thoughts?


//deafdigit

Philip M
10-05-2011, 02:54 PM
That is a very complicated and inefficient way to enter numbers only. If you had made this requirement clear I would have offered you


<input type = "text" id = "myfield" onkeyup = "stopDot()" onblur = "stopDot()">

<script type = "text/javascript">
function stopDot(){
var val = document.getElementById("myfield").value;
val = val.replace(/[^0-9\,]/g,""); // only numbers and comma allowed
document.getElementById("myfield").value = val;
}
</script>

deafdigit
10-10-2011, 12:23 PM
Thanks again, Philip. Your function did the trick. Very nice.

I'll be able to do this in the future.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum