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

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

P: n/a
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;
}
Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"KathyB" <Ka**********@attbi.com> schreef in bericht
news:75*************************@posting.google.co m...
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

Jul 20 '05 #2

P: n/a
JRS: In article <75*************************@posting.google.com> , seen
in news:comp.lang.javascript, KathyB <Ka**********@attbi.com> posted at
Fri, 19 Dec 2003 16:22:53 :-
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>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #3

P: n/a
@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;
}
}
}
Jul 20 '05 #4

P: n/a
JRS: In article <3F***************@wanadoo.fr>, seen in
news:comp.lang.javascript, @SM <st**************@wanadoo.fr> posted at
Sun, 21 Dec 2003 05:34:12 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #5

P: n/a
John, used strictly with IE5.5 on an intranet.

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6

P: n/a
JRS: In article <3f*********************@news.frii.net>, seen in
news:comp.lang.javascript, Kathy Burke <ka**********@attbi.com> posted
at Mon, 22 Dec 2003 00:59:55 :-
John, used strictly with IE5.5 on an intranet.


That seems irrelevant to the question that I was asking about @SM's
testing.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #7

P: n/a
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 <sp**@merlyn.demon.co.uk> wrote in message news:<1w**************@merlyn.demon.co.uk>...
JRS: In article <75*************************@posting.google.com> , seen
in news:comp.lang.javascript, KathyB <Ka**********@attbi.com> posted at
Fri, 19 Dec 2003 16:22:53 :-
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>.

Jul 20 '05 #8

P: n/a
JRS: In article <75**************************@posting.google.com >, seen
in news:comp.lang.javascript, KathyB <Ka**********@attbi.com> posted at
Mon, 22 Dec 2003 16:35:01 :-
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.
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<1wZxAIAoHJ5$Ew
59@merlyn.demon.co.uk>...


Responses should go AFTER trimmed quotes - see newsgroup FAQ.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #9

P: n/a
@SM
Dr John Stockton a ecrit :

JRS: In article <3F***************@wanadoo.fr>, seen in
news:comp.lang.javascript, @SM <st**************@wanadoo.fr> posted at
Sun, 21 Dec 2003 05:34:12 :-
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>
</html>
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
Jul 20 '05 #10

P: n/a
JRS: In article <3F***************@wanadoo.fr>, seen in
news:comp.lang.javascript, @SM <st**************@wanadoo.fr> posted at
Wed, 24 Dec 2003 00:51:04 :-
Dr John Stockton a ecrit :

JRS: In article <3F***************@wanadoo.fr>, seen in
news:comp.lang.javascript, @SM <st**************@wanadoo.fr> posted at
Sun, 21 Dec 2003 05:34:12 :-
> if(value.parseFloat()!=value || value<min || value>max)
On which system did you test that?
Never where !
In MSIE4, strings appear not to have
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

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.

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


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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.