Forum Moderators: open

Message Too Old, No Replies

Maxlength problem

Setting text box input limit to 4 dp

         

Asif

2:52 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



Hi guys,

I need to limit user input in my text boxes to 4 decimal places. Any ideas?

Thanks

SpaceFrog

3:12 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



maxLength=4?

Asif

3:16 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



4 "DP" not 4 characters.

SpaceFrog

3:23 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



on the onkeyup you will have to trigger a function that will search decimal separator and limit decimals...

if(document.getElementById('Myzone').value.split('.')[1]>4){alert("too many decimals')

Bernard Marx

3:34 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/* 
dummy objects to emulate form boxes
for this demo
*/
box0 = {value: '111.111178'};
box1 = {value: '111.111132'};

alert(round(box0.value,4));
alert(round(box1.value,4));

function round(val,dp)
{
var mult = Math.pow(10,dp);
return Math.round(val*mult)/mult; /*implicit type conversion*/
}

Asif

3:38 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



But I don't want an alert message to show up. The user shouldn't be able to enter further than the 4th decimal, without generating an error or alert message.

Asif

3:40 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



It's like you know when you use maxlength the user isn't allowed to enter any number of characters more than the maxlength? I need something like that.

SpaceFrog

3:51 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



if(document.getElementById('Myzone').value.split('.')[1]>4){
document.getElementById('Myzone').value=document.getElementById('Myzone').value.split('.')[0]+"."+document.getElementById('Myzone').value.split('.')[1].substr(0,4)}

SpaceFrog

3:56 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Nouvelle page 1</title>

<script type='text/javascript'>
function toolong(){
if(document.getElementById('Myzone').value.split('.')[1]){
if(document.getElementById('Myzone').value.split('.')[1].length>4){
document.getElementById('Myzone').value=document.getElementById('Myzone').value.split('.')[0]+"."+document.getElementById('Myzone').value.split('.')[1].substr(0,4)
}
}
}</script>
</head>

<body>
<input type="text" id="Myzone" onkeyup='toolong()'/>
</body>
</html>

Bernard Marx

4:35 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html>
<head>
<script type='text/javascript'>
function toolong(box){
box.value = box.value.replace(/^(.*\..{0,4}).*$/,'$1');
}</script>
</head>

<body>
<input type="text" id="Myzone" onkeyup='toolong(this)'/>
</body>
</html>

SpaceFrog

4:41 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



RegExp ...
Never could cope with them....

But you should double the triggering of function on onmouseup to avoid pasting with mouse ...

GaryK

5:12 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What will happen if I don't have JS enabled?

Why not take a simpler approach and use a separate text box for the decimal places? ;)

Asif

7:14 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



See - what I understand by looking at the functions is that whatever the users enter will be cut down to 4 dp by the function. However, what I need is a complete stop after the user enters the fourth dp. For example, if the user enters 56.7896, then even if he enters another dp, nothing will appear in the text box.

Bernard Marx

8:12 pm on Mar 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



NO. Apart from my first one, which doesn't actually take aby action, all the functions remove any characters after the fourth DP. They don't do any rounding, just remove any extra characters as they are put in. Try one.

Asif

9:43 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



guys...

sorry for being a smart butt...yes it works! thanks a ton...

SpaceFrog

9:34 am on Mar 8, 2005 (gmt 0)

10+ Year Member



you could even be more directive by sending the focus elsewhere or rnedering textbox readonly thus blocking any other tentative to enter decimals ;-)