Click to See Complete Forum and Search --> : Help with input field logiv
smudge618
02-07-2008, 05:50 AM
Hi have four input fields. Onkeydown does the calculation automatically.
I am trying to create onfocus function that makes the form only allow two inputs out of the four. On the second input into teh form it disables the other two input boxes that have not had any input.
Any advice please...
Thanks in advance
can we see the HTML section? We need to see how you grouped the inputs
smudge618
02-07-2008, 06:06 AM
<form name="f1" id="theForm" onreset="clear()" />
<!-- Title of form -->
<table cellpadding="5" border="1" cellspacing="0" bgcolor="#ffedcf">
<tr>
<td colspan="4" align="center">Enter two known values and press <b>Solve</b> to calculate unknowns.</td>
</tr>
<!--Volts row -->
<tr>
<td align="left" width="200">Volts(Volts)</td>
<td align="center"><input type="Text" name="volts" size="20" onkeyup="validNumeric(this)" /></td>
<td align="center" width="200px"><div id="volts"></div></td>
</tr>
<tr>
<td align="left" width="90">Resistance(Ohms)</td>
<td align="center"><input type="Text" name="ohms" size="20" onkeyup="validNumeric(this)" /></td>
<td align="center" width="200px"><div id="ohms"></div></td>
</tr>
<tr>
<td align="left" width="90">Current(Amps)</td>
<td align="center"><input type="Text" name="amps" size="20" onkeyup="validNumeric(this)" /></td>
<td align="center" width="200px"><div id="amps"></div></td>
</tr>
<tr>
<td align="left" width="90">Power(Watts)</td>
<td align="center"><input type="Text" name="watts" size="20" onkeyup="validNumeric(this)" /></td>
<td align="center" width="200px" ><div id="watts"></div></td>
</tr>
<tr>
<td></td>
<td align="center" colspan="2">
<input type="reset" value=" Reset " name="reset" onreset="clear()" /></td>
</tr>
</table>
</form>
smudge618
02-07-2008, 06:08 AM
onblur= "inputBoxes()" for each input box that checks once two inputs are in it needs to disable the other two inputs?
<script type="text/javascript">
function inputBoxes(){
var names=['volts','ohms','amps','watts'], n, i=0, k=0, el;
while(n=names[i++]){
document.getElementsByName(n)[0].value.length>0?k++:null;
}
i=0;
while(n=names[i++]){
el=document.getElementsByName(n)[0];
el.removeAttribute('disabled');
k==2&&el.value.length==0?el.setAttribute('disabled','disabled'):null;
}
}
</script>
smudge618
02-07-2008, 06:35 AM
This my code code for inputBoxes... logic is wrong :(
function inputBoxes()
{
var inputs = document.getElementById('theForm').getElementsByType('input');//Get all input elements
nm = textInputs.length; //number of inputs to cycle through
var nmNotEmpty = 0; // number of inputs not empty
//Cycle hthrough the loop of all input type ="text"
for (i=0; i<nm; i++)
{
// check if it's empty or not
if (textInputs[i].value = '')
// not empty? increment the not empty variable
nmNotEmpty++;
}
// Now check how many aren't empty and if it's more than 2 disable the other two inputs
if (nmNotEmpty >= 2)
document.getElementByType('input').disabled = false;
else
document.getElementById('input').disabled = true;
}
smudge618
02-07-2008, 06:56 AM
I though input box was boolean?
I though input box was boolean?
According to the new W3C recommendations, all the HTML (and specially XHTML) attributes must have string values. For instance:
<input disabled="disabled" readonly="readonly">
Thus the DOM way is to set the attribute (setAttribute(attribute,value)) and to remove the attribute (removeAttribute(attribute))
smudge618
02-07-2008, 07:22 AM
hmmm tried this not working...not too sure what is going on!
It works for me in all the browsers I have:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function inputBoxes(){
var names=['volts','ohms','amps','watts'], n, i=0, k=0, el;
while(n=names[i++]){
document.getElementsByName(n)[0].value.length>0?k++:null;
}
i=0;
while(n=names[i++]){
el=document.getElementsByName(n)[0];
el.removeAttribute('disabled');
k==2&&el.value.length==0?el.setAttribute('disabled','disabled'):null;
}
}
</script>
</head>
<body>
<form>
<input type="text" name="volts" onblur="inputBoxes()">
<input type="text" name="ohms" onblur="inputBoxes()">
<input type="text" name="amps" onblur="inputBoxes()">
<input type="text" name="watts" onblur="inputBoxes()">
</form>
</body>
</html>
smudge618
02-07-2008, 10:05 AM
Hi
The problem I have is with IE7....the attribue does not work in it? Also I gettingan undefined error that I do not understand..
Any help?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ohm's Law Calculator</title>
<script language="javascript">
function solveform(){ //called by the on onkeypevent
var d=document.f1;
var v1=d.volts.value;
var o1=d.ohms.value;
var a1=d.amps.value;
var w1=d.watts.value;
if (a1 && o1){w1=Math.pow(a1, 2)*o1; v1=o1*a1;}
else if (a1 && v1){w1=a1*v1; o1=v1/a1;}
else if (v1 && o1){w1=Math.pow(v1, 2)/o1; a1=v1/o1;}
else if (w1 && a1){v1=w1/a1; o1=w1/Math.pow(a1,2);}
else if (w1 && v1){a1=w1/v1; o1=Math.pow(v1,2)/w1;}
else if (a1 && v1){w1=v1*a1; o1=v1/a1;}
else if (w1 && o1){v1=Math.sqrt(w1*o1); a1=Math.sqrt(w1/o1);}
document.getElementById('volts').innerHTML=v1;
document.getElementById('ohms').innerHTML =o1;
document.getElementById('amps').innerHTML =a1;
document.getElementById('watts').innerHTML=wl;
}
function validNumeric(f) { //Function that strips out any letters and displays error message
if (!/^\d*$/.test(f.value)) {
var t1 =setTimeout("document.getElementById('error').innerHTML = 'Please Only enter letters'",1000);
var t2 =setTimeout("document.getElementById('error').innerHTML = ''",5000);
f.value = f.value.replace(/[^\d]/g,"");
}
solveform()
}
function clear() {
var divs = document.getElementById('theForm').getElementsByTagName('div');// Create variable for the divs in the form
for (var i=0; i<divs.length; i++) {
var div = divs[i];
while (div.hasChildNodes()) {
div.removeChild(div.firstChild);
}
}
}
</script>
</head>
<body>
<form name="f1" id="theForm" onreset="clear()" />
<!-- Title of form -->
<table cellpadding="5" border="1" cellspacing="0" bgcolor="#ffedcf">
<tr>
<td colspan="4" align="center">Enter two known values and press <b>Solve</b> to calculate unknowns.</td>
</tr>
<!--Volts row -->
<tr>
<td align="left" width="90" height="70px">Volts(Volts)</td>
<td align="center"><input type="Text" name="volts" size="20" onkeyup="validNumeric(this)" onclick= "inputBoxes()" /></td>
<td align="center" width="200px"><div id="volts"></div>V</td>
</tr>
<tr>
<td align="left" width="90" height="70px">Resistance(Ohms)</td>
<td align="center"><input type="Text" name="ohms" size="20" onkeyup="validNumeric(this)" onclick="inputBoxes()" /></td>
<td align="center" width="200px"><div id="ohms"></div>Ω</td>
</tr>
<tr>
<td align="left" width="90" height="70px">Current(Amps)</td>
<td align="center"><input type="Text" name="amps" size="20" onkeyup="validNumeric(this)" onclick="inputBoxes()" /></td>
<td align="center" width="200px"><div id="amps"></div>A</td>
</tr>
<tr>
<td align="left" width="90" height="70px">Power(Watts)</td>
<td align="center"><input type="Text" name="watts" size="20" onkeyup="validNumeric(this)" onclick="inputBoxes()" /></td>
<td align="center" width="200px" ><div id="watts"></div>W</td>
</tr>
<tr>
<td></td>
<td align="center" colspan="2">
<input type="reset" value=" Reset " name="reset" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function inputBoxes(){
var names=['volts','ohms','amps','watts'], n, i=0, k=0, el;
while(n=names[i++]){
document.getElementsByName(n)[0].value.length>0?k++:null;
}
i=0;
while(n=names[i++]){
el=document.getElementsByName(n)[0];
el.removeAttribute('disabled');
k==2&&el.value.length==0?el.setAttribute('disabled','disabled'):null;
}
}
</script>
<p><b id='error'></b> </p>
</body>
</html
IE confounds name with id if same value, thus try to change the id's of the divs, say "Volts", a.s.o.