Yet another validate number ??? Allow negative and period (-.5) etc.

K

KathyB

Hi,

I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

function ValidateSave(formRef,fieldName,min,max) {
var formField = formRef.elements[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField.value)) {
alert('Invalid entry...please try again!');
formField.focus();
formField.select();
return false;
}
 
J

Janwillem Borleffs

KathyB said:
Hi,

I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

You cand do this without regular expressions with the isFinite function:

var num = -.5;
alert(isFinite(num)); // Alerts `true`


JW
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

function ValidateSave(formRef,fieldName,min,max) {
var formField = formRef.elements[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField.value)) {
alert('Invalid entry...please try again!');
formField.focus();
formField.select();
return false;
}


You should indent your code to show its structure. Hard-to-read code is
likely to be ignored.

You should not allow a decimal point with no digit before it; nor with
no digit after. Such constructions are liable to be mistakes. Your
words allow it, but not your code. If a - sign is allowed, a + sign
should be allowed and might be mandated.

You have: Bad = !/^\d+(\.\d+)?$/.test(formField.value)
You need: Bad = !/^(\+|-)?\d+(\.\d+)?$/.test(formField.value)

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
@

@SM

KathyB a ecrit :
Hi,

I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

function ValidateSave(formRef,fieldName,min,max) {
with(formRef[fielName]){
if(value.parseFloat()!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
KathyB a ecrit :
I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

function ValidateSave(formRef,fieldName,min,max) {
with(formRef[fielName]){
if(value.parseFloat()!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}


On which system did you test that? In MSIE4, strings appear not to have
a parseFloat method, and parseFloat is a global function.

When answering, please leave a gap, and do not start on a line which
already has a quote mark.
 
K

KathyB

John,

Using what you kindly provided:

You have: Bad = !/^\d+(\.\d+)?$/.test(formField.value)
You need: Bad = !/^(\+|-)?\d+(\.\d+)?$/.test(formField.value)

....validates ok for negative whole numbers but not for -.5 etc.
(invalid entry message). This works great if the user remembers to put
-0.5, but not if they leave off the zero and enter -.5 -- any way
around this? Silly, I know, but I have quite the user community!

Thanks again, Kathy

p.s. thought you're other question was directed at me...thought it was
strange :) but I answered it anyway!



Dr John Stockton said:
JRS: In article <[email protected]>, seen
I just can't get this quite right. I use the following function to
validate a user entry. I need to allow negative numbers including
those with decimals (e.g., -.5). The following allows the decimals but
not the negative. Any help appreciated...as always. Thanks -- Kathy

function ValidateSave(formRef,fieldName,min,max) {
var formField = formRef.elements[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField.value)) {
alert('Invalid entry...please try again!');
formField.focus();
formField.select();
return false;
}


You should indent your code to show its structure. Hard-to-read code is
likely to be ignored.

You should not allow a decimal point with no digit before it; nor with
no digit after. Such constructions are liable to be mistakes. Your
words allow it, but not your code. If a - sign is allowed, a + sign
should be allowed and might be mandated.

You have: Bad = !/^\d+(\.\d+)?$/.test(formField.value)
You need: Bad = !/^(\+|-)?\d+(\.\d+)?$/.test(formField.value)

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
You have: Bad = !/^\d+(\.\d+)?$/.test(formField.value)
You need: Bad = !/^(\+|-)?\d+(\.\d+)?$/.test(formField.value)

...validates ok for negative whole numbers but not for -.5 etc.
(invalid entry message). This works great if the user remembers to put
-0.5, but not if they leave off the zero and enter -.5 -- any way
around this? Silly, I know, but I have quite the user community!

Having a decimal point without a digit on each side is strongly
deprecated, because allowing it is too likely to lead to un-noticed
error. See, for example, SUNAMCO 87-1 (IUPAP-25), 1987 revision, by
Cohen & Giacomo, Section 1.3.2.; either it, or a successor, ought to be
on the Web somewhere. I decline to support breach of its
recommendation.
(e-mail address removed)>...

Responses should go AFTER trimmed quotes - see newsgroup FAQ.
 
@

@SM

Dr John Stockton a ecrit :
JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
function ValidateSave(formRef,fieldName,min,max) {
with(formRef[fielName]){
if(value.parseFloat()!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}

On which system did you test that?

Never where !
In MSIE4, strings appear not to have

I've no more IE4 installed.
But ...
just don't tell him 'min' and 'max' are strings ! ! !
that's to say calling the function this way :
ValidateSave(this,'myFieldName',-.95,2.74)
a parseFloat method, and parseFloat is a global function.

Don't understand what you mean,
here, parseFloat is only used to test if the entry is a number

I've seen there was a little error in my code ! :-(
Here is something working fine (even with Mac IE5.0)

<html>
<script type="text/javascript"><!--
function ValidateSave(formRef,fieldName,min,max) {
with(formRef[fieldName]){
value=value.replace(',','.');
if(parseFloat(value)!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}
onload=setTimeout('document.forms[0][0].focus()',10);
// --></script>
<FORM ACTION="00.htm" METHOD=get TARGET="_blank"
onSubmit="return ValidateSave(this,'TxtField',-.75,1.1)">
<INPUT TYPE=text NAME="TxtField" VALUE="">
<INPUT TYPE=submit VALUE="Submit">
<input type=button value="refresh" onclick="location=self.location;">
</FORM>
When answering, please leave a gap, and do not start on a line which
already has a quote mark.

That's not very important ...

Note only that some systems use ',' isnteed of '.' as separator
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Dr John Stockton a ecrit :

Never where !


Don't understand what you mean,
here, parseFloat is only used to test if the entry is a number
if(parseFloat(value)!=value || value<min || value>max)


Now, you are calling parseFloat as a global function with a parameter,
rather than a method of String; the important difference is that it
works that way.

That's not very important ...

Indeed it is, if you are to be readily understood. Usenet conventions
should be obeyed, even if you cannot yourself see the importance on
whatever system you use.

Note only that some systems use ',' isnteed of '.' as separator

Some countries do, certainly. AFAICS, ECMA-262 does not allow it in
code, where the comma has an alternate meaning.

If a comma is to be allowed in numeric input, its meaning must be
predefined, since otherwise 1,111.1 would be dangerously near ambiguous
(if thousands separators are allowed, a user might try thousandths
separators).

Programmers wishing to allow for I/O of forms other than ECMA-standard
will have to do at least some of the processing in their own code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top