...

View Full Version : restricting the user from entering the number >150



murali1983
01-06-2007, 02:29 PM
hi frnds

i want to restrict the user from entering the number greater than 150 in the textbox.

like if the user enters 1-9 in the first position its fine..., in the same way in the second position also. ex: 99..now i shoudnt allow him to type anyohter number coz it wil exceede 150.

i hope the question was clear..

thnx
Murali

Mr J
01-06-2007, 03:31 PM
Give this a try


<script type="text/javascript">

function chk150(){

re = /^[0-9]*$/
if (!re.test(document.f1.t1.value)){
document.f1.t1.value = document.f1.t1.value.replace(/[^0-9]/g,"")
}

if(document.f1.t1.value>150){
document.f1.t1.value=""
}

}

</script>

<form name="f1">
<input type="text" name="t1" onkeyup="chk150()">
</form>

david_kw
01-06-2007, 08:51 PM
You could try something like this. I think I've seen solutions that don't require you to see the incorrect value but I can't think what they are right now.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Junque</title>
<script type="text/javascript">
/* <![CDATA[ */
var oldValue = "";
function m(oThis) {
var s = oThis.value;
var n = parseInt(s);

if (n > 150) {
oThis.value = oldValue;
} else if (/[^0-9]/.test(s)) {
oThis.value = oldValue;
}
oldValue = oThis.value;
}
/* ]]> */
</script>
</head>
<body>
<div>
<form name="f" action="#" onsubmit="return(false);">
<input type="text" name="t" onkeypress="m(this);" onkeydown="m(this);" onkeyup="m(this);" />
</form>
<br />
<div id="adiv"> </div>
</div>
</body>
</html>


david_kw

Ancora
01-07-2007, 01:11 PM
murali1983:



<html>
<head>
<script type="text/javascript">

var max = 150;

function check(nField){

if (!/^\d+$/.test(nField.value)){nField.value = nField.value.substring(0,nField.value.length-1)}
if (Number(nField.value) > max){alert('Maximum is '+max);nField.value = ""}
}

</script>
</head>
<body>

<form>
<label>A: <input type='text' size='5' name='a' onkeyup="check(this)"></label><br>
<label>D: <input type='text' size='5' name='d' onkeyup="check(this)"></label>
</form>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum