By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,620 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,620 IT Pros & Developers. It's quick & easy.

Testing for blank input...

P: n/a
Me
I need to test for blank inputs and run a routine "if" the input is
blank. This will be used internally on a LAN to calculate selling price
if the "sell" input is blank and a margin is supplied, and to calculate
a margin if the margin input is blank and the sell price is supplied.
The "cost" is set and will be doled out by the CGI. I've tried checking
for a zero variable length ".length" and couldn't get it to work. It
always fires if you check for -1 even if there is something in the
input, and it never fires if you set it any higher. I'm sure I'm just
doing it wrong. Here's where I'm at:

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--HIDE
function doMath( ) {

// var one will be set by the CGI when in use

var one = eval(document.theForm.
elements[0].value)
var two = eval(document.theForm.
elements[1].value)
var three = eval(document.theForm.
elements[2].value)
// Run only if sell input is blank

var margin = ((one / three) * 100)
document.theForm.elements[1].value = margin

// Run only if sell input is blank

var sell = (one / ((100 - two) / 100))
document.theForm.elements[2].value = sell

}
//STOP HIDING-->
</SCRIPT>
<BODY><FORM NAME="theForm">
Cost <INPUT TYPE="text"> Margin <INPUT TYPE="text"> Sell <INPUT TYPE="text">
<INPUT TYPE="button" VALUE="Calculate"
onClick="doMath( )">
</FORM>
</body>
</html>
Suggestions appreciated. :-)
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Me
Me wrote:
I need to test for blank inputs and run a routine "if" the input is
blank. This will be used internally on a LAN to calculate selling price
if the "sell" input is blank and a margin is supplied, and to calculate
a margin if the margin input is blank and the sell price is supplied.
The "cost" is set and will be doled out by the CGI. I've tried checking
for a zero variable length ".length" and couldn't get it to work. It
always fires if you check for -1 even if there is something in the
input, and it never fires if you set it any higher. I'm sure I'm just
doing it wrong. Here's where I'm at:


Got it. Dumb syntax problem. Here's what worked.

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

function doMath( ) {
var one = eval(document.theForm.elements[0].value)
var two = eval(document.theForm.elements[1].value)
var three = eval(document.theForm.elements[2].value)
// Run only if margin input is blank

if (document.theForm.elements[1].value.length==0){
var margin = (100 - ((one / three) * 100));
document.theForm.elements[1].value = margin
}

// Run only if sell input is blank

if (document.theForm.elements[2].value.length==0){
var sell = (one / ((100 - two) / 100));
document.theForm.elements[2].value = sell
}

}

</SCRIPT>
</head>

<BODY>

<FORM NAME="theForm">
Cost <INPUT TYPE="text"> Margin <INPUT TYPE="text"> Sell <INPUT TYPE="text">
<INPUT TYPE="button" VALUE="Calculate" onClick="doMath( )">
</FORM>

</body>
</html>
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.