...

View Full Version : Getting a simple error in simple code.



jentybhullar
09-24-2005, 05:52 PM
Hi, im currently using the following code to swap text between text boxes. I'm suppose to enter only numeric values, and its suppose to swap text only if numeric value in first text box is greater than second one. however in my code, some times it works, some times it doesnt. sometimes it even swaps even if its smaller than second value.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<!-- Minus AutoDato -->
<TITLE>Sort Text</TITLE>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<script>
function swap()
{
var form=document.switchtext;
var t1=form.text1;
var t2=form.text2;
if (form.text1.value > form.text2.value) {
var temp = t1.value;
t1.value = t2.value;
t2.value=temp;
}
}
</script>

</HEAD>
<BODY>

<b><i> Please insert only numeric values. </i><b>
<form name="switchtext">
<input type="text" name="text1"/> <br />
<input type="text" name="text2"/> <br />
<input type="button" name="switch" value="Switch Text" onclick=swap()>


</BODY>
</HTML>

Philip M
09-25-2005, 10:20 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<!-- Minus AutoDato -->
<TITLE>Sort Text</TITLE>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<script>
function swap()
{
var t1=document.switchtext.text1.value;
var t2=document.switchtext.text2.value;
if (t1 < t2) {
var temp = t1;
switchtext.text1.value = t2;
switchtext.text2.value = temp;
}
}
</script>

</HEAD>
<BODY>

<b><i> Please insert only numeric values. </i><b>
<form name="switchtext">
<input type="text" name="text1"> <br>
<input type="text" name="text2"> <br >
<input type="button" name="switch" value="SwitchText" onclick="swap()">


</BODY>
</HTML>

You will need to include a check to ensure that only numeric values are entered by the user, otherwise the script will throw an error.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum