473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1456
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
4
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
7
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
5
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
2
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
6
by: Jon Paal | last post by:
validation doesn't fire what's missing ????? /////// ---- code -----/////////////////////////// Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.