...

View Full Version : restricting the dynamicallly generated text boxes from accepting more than one dot.



murali1983
01-15-2007, 04:25 PM
I am generating the text boxes dynamically, and the generated text boxes can accept only numbers[0-9] and singele dot.
how can i achive this....? below is the function which accepts only numbers....but how can i restrict the text box from

accepting more than one dot.


<html>
<head>
<script language="javascript">


function numbersonly(eventKey)
{
var unicode=eventKey.keyCode;
if (unicode!=8)
{
if (unicode<48||unicode>57)
return false
}

}
</script>
</head>
<body>
<p>I am generating the text boxes dynamically, and the generated text boxes can accept only numbers[0-9] and singele dot.
how can i achive this....? below is the function which accepts only numbers....but how can i restrict the text box from

accepting more than one dot.
</p>
<input type="text" id="txtPlan1" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan2" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan3" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan4" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan5" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan6" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan7" name = "txtPlan" onkeypress="return numbersonly(event);"></br>
<input type="text" id="txtPlan8" name = "txtPlan" onkeypress="return numbersonly(event);"></br>

<p>
all the above text fields should accept only single dot.., in my program i am generating these text boxes dynamically</P>
</body>

TripperTreats
01-15-2007, 05:20 PM
This might work:


function numbersonly (num, eventKey) {
var unicode=eventKey.keyCode;
var inp = document.getElementById('txtPlan'+num);
if (unicode!=8) return (48<=unicode && unicode <=57);
if (unicode==8) {
for (var i=0; i++; i<inp.value.length) {
if (inp.value.charAt(i) == '.') return false
}
return true;
}
}Of course you would need to change your inputs to:


<input type="text" id="txtPlan1" name = "txtPlan" onkeypress="return numbersonly(1,event);"></br>
<input type="text" id="txtPlan2" name = "txtPlan" onkeypress="return numbersonly(2,event);"></br>
<input type="text" id="txtPlan3" name = "txtPlan" onkeypress="return numbersonly(3,event);"></br>
<input type="text" id="txtPlan4" name = "txtPlan" onkeypress="return numbersonly(4,event);"></br>
<input type="text" id="txtPlan5" name = "txtPlan" onkeypress="return numbersonly(5,event);"></br>
<input type="text" id="txtPlan6" name = "txtPlan" onkeypress="return numbersonly(6,event);"></br>
<input type="text" id="txtPlan7" name = "txtPlan" onkeypress="return numbersonly(7,event);"></br>
<input type="text" id="txtPlan8" name = "txtPlan" onkeypress="return numbersonly(8,event);"></br>

Philip M
01-15-2007, 05:28 PM
Try this:-

function numbersonly(lstr) {
if (/((\.+)\d*(\.+))|([^0-9\.])/gi.test(lstr.value)) {
alert ("Invalid format - only digits and one single dot are allowed!");
return false;
}
return true;
}

OR a simpler version:-
if (/(\.+.*\.+)|([^0-9\.])/gi.test(lstr.value)) {

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski. :D

Mr J
01-19-2007, 07:34 PM
This may do it as well


str="123.321"

re=/^\d*\.?\d*$/

alert(re.test(str))

Philip M
01-20-2007, 07:40 AM
This may do it as well


str="123.321"

re=/^\d*\.?\d*$/

alert(re.test(str))

Yes, that works as well! And shorter! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum