469,352 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Validation, .5 vs 0.5

Hi all,

I am (have) wrote an intranet industrial app that requires the user to
enter a number which is compared to a high and low limit. If the low
or high limits have a 0 in front of the decmial the entered value must
also.

The actual comparison is done in javascript as pasted below. If a
return true is given then the data is written to the database.

ex

low limit 0.5
high limit 0.6
entered value .55 is returned as OUT OF TOLERANCE CONDITION

whereas entered value of 0.55 is returned as GOOD!

For the time being I have consoled my users to be sure to enter a
leading 0 when dealing with decimals.

How do I correct this, such that .5 and 0.5 are considered valid
entries?

if ((document.testform.actual_value.value <
document.testform.upper_limit.value) &&
(document.testform.actual_value.value >
document.testform.lower_limit.value))
{
alert("Data Good\n\nWriting the Data\n to the Database")
document.testform.pass_fail.value="P"
return true
}
else
{
input_box=confirm("Click OK to record a NC or Cancel to
Re-enter");
if (input_box==true)
{
// Output when OK is clicked
alert ("You clicked NC, TAG the item as REJECT (RED)");
return true
}
else
{
// Output when Cancel is clicked
alert ("You clicked cancel");
return false
}
}

Sep 16 '05 #1
2 1400
rj***********@gmail.com wrote:
[...]
low limit 0.5
high limit 0.6
entered value .55 is returned as OUT OF TOLERANCE CONDITION

whereas entered value of 0.55 is returned as GOOD! [...]

function isInRange(min,max,number){
return !isNaN(min+max+number) && min<= number && number <= max;
}
// Not optimum, but you get the idea

// Coerce the form values into Numbers:
function rjames(form){
if (isInRange(
+form.lower_limit.value,
+form.upper_limit.value,
+form.actual_value.value)){
alert("Data Good\n\nWriting the Data\nto the Database");
form.pass_fail.value="P";
return true;
}
if(confirm("Click OK to record a NC or Cancel to Re-enter"){
alert ("You clicked NC, TAG the item as REJECT (RED)");
return true;
}
alert ("You clicked cancel");
return false;
}
<form ... onsubmit="return rjames(this)">

Mick

if ((document.testform.actual_value.value <
document.testform.upper_limit.value) &&
(document.testform.actual_value.value >
document.testform.lower_limit.value))
{
alert("Data Good\n\nWriting the Data\n to the Database")
document.testform.pass_fail.value="P"
return true
}
else
{
input_box=confirm("Click OK to record a NC or Cancel to
Re-enter");
if (input_box==true)
{
// Output when OK is clicked
alert ("You clicked NC, TAG the item as REJECT (RED)");
return true
}
else
{
// Output when Cancel is clicked
alert ("You clicked cancel");
return false
}
}

Sep 16 '05 #2
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Fri, 16 Sep 2005 14:45:41, seen in news:comp.lang.javascript,
rj***********@gmail.com posted :
I am (have) wrote an intranet industrial app that requires the user to
enter a number which is compared to a high and low limit. If the low
or high limits have a 0 in front of the decmial the entered value must
also.

The actual comparison is done in javascript as pasted below. If a
return true is given then the data is written to the database.

ex

low limit 0.5
high limit 0.6
entered value .55 is returned as OUT OF TOLERANCE CONDITION

whereas entered value of 0.55 is returned as GOOD!

For the time being I have consoled my users to be sure to enter a
leading 0 when dealing with decimals.

How do I correct this, such that .5 and 0.5 are considered valid
entries?

if ((document.testform.actual_value.value <
document.testform.upper_limit.value) &&
(document.testform.actual_value.value >
document.testform.lower_limit.value))
...


An answer posted earlier was to a different question.
You are doing string comparisons; a .value of a control is always (?) a
string. Fix it by using unary +, for which see newsgroup FAQ and
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#UP>.

Then the writing could be simplified using "with" :-

with (document.testform)
if ( (+actual_value.value < +upper_limit.value) &&
(=actual_value.value > +lower_limit.value) )

or a temporary T :-

T = document.testform
if ( (+T.actual_value.value < +T.upper_limit.value) &&
(+T.actual_value.value > +T.lower_limit.value) )

and a temporary Q :-

T = document.testform
Q = +T.actual_value.value
if ( (Q < +T.upper_limit.value) && (Q > +T.lower_limit.value) )

and if the limits are used repeatedly they should be assigned to
temporaries too :-

T = document.testform
Q = +T.actual_value.value
L = +T.lower_value.value
U = +T.upper_value.value
if ( (Q < U) && (Q > L) )

See also <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; for robust
operation, you should be pattern-testing the editable fields, possibly
for 1-M decimal digits optionally followed by a decimal point
(localisable) and 1-N digits.

Note that it is bad practice to allow a decimal point that has no
preceding digit; points are not always easy to see.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Stefan Richter | last post: by
2 posts views Thread by wumingshi | last post: by
4 posts views Thread by Tim Meagher | last post: by
6 posts views Thread by Stephen | last post: by
7 posts views Thread by Ryan Ternier | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Jon Paal | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.