473,626 Members | 3,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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...a s always. Thanks -- Kathy

function ValidateSave(fo rmRef,fieldName ,min,max) {
var formField = formRef.element s[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField .value)) {
alert('Invalid entry...please try again!');
formField.focus ();
formField.selec t();
return false;
}
Jul 20 '05 #1
10 2994

"KathyB" <Ka**********@a ttbi.com> schreef in bericht
news:75******** *************** **@posting.goog le.com...
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...a s 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
JRS: In article <75************ *************@p osting.google.c om>, seen
in news:comp.lang. javascript, KathyB <Ka**********@a ttbi.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...a s always. Thanks -- Kathy

function ValidateSave(fo rmRef,fieldName ,min,max) {
var formField = formRef.element s[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField .value)) {
alert('Inval id entry...please try again!');
formField.focu s();
formField.sele ct();
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.demo n.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #3
@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...a s always. Thanks -- Kathy

function ValidateSave(fo rmRef,fieldName ,min,max) {

with(formRef[fielName]){
if(value.parseF loat()!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}
Jul 20 '05 #4
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...a s always. Thanks -- Kathy

function ValidateSave(fo rmRef,fieldName ,min,max) {

with(formRef[fielName]){
if(value.parseF loat()!=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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5
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
JRS: In article <3f************ *********@news. frii.net>, seen in
news:comp.lang. javascript, Kathy Burke <ka**********@a ttbi.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7
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.de mon.co.uk> wrote in message news:<1w******* *******@merlyn. demon.co.uk>...
JRS: In article <75************ *************@p osting.google.c om>, seen
in news:comp.lang. javascript, KathyB <Ka**********@a ttbi.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...a s always. Thanks -- Kathy

function ValidateSave(fo rmRef,fieldName ,min,max) {
var formField = formRef.element s[fieldName];
if (!/^\d+(\.\d+)?$/.test(formField .value)) {
alert('Inval id entry...please try again!');
formField.focu s();
formField.sele ct();
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.demo n.co.uk/js-valid.htm>.

Jul 20 '05 #8
JRS: In article <75************ **************@ posting.google. com>, seen
in news:comp.lang. javascript, KathyB <Ka**********@a ttbi.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.de mon.co.uk> wrote in message news:<1wZxAIAoH J5$Ew
59@merlyn.demo n.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #9
@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(fo rmRef,fieldName ,min,max) {with(formRef[fielName]){
if(value.parseF loat()!=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(th is,'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(fo rmRef,fieldName ,min,max) {
with(formRef[fieldName]){
value=value.rep lace(',','.');
if(parseFloat(v alue)!=value || value<min || value>max)
{
alert('Invalid entry...please try again!');
focus();
select();
return false;
}
}
}
onload=setTimeo ut('document.fo rms[0][0].focus()',10);
// --></script>
<FORM ACTION="00.htm" METHOD=get TARGET="_blank"
onSubmit="retur n ValidateSave(th is,'TxtField',-.75,1.1)">
<INPUT TYPE=text NAME="TxtField" VALUE="">
<INPUT TYPE=submit VALUE="Submit">
<input type=button value="refresh" onclick="locati on=self.locatio n;">
</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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
41791
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
13
6523
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a number will be missing, which is not a problem for my purposes I don't think, but how would it be negative? Can randomly created PK autonumber fields be negative? Any way for a PK, autonumbered, *incremented* to be negative? TIA
7
7095
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
15
10982
by: dee | last post by:
Hi, What is the maximum number of minutes for Session timeout that I can specify in web.config? Thanks. Dee
23
2865
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which is not valid. Here's my attempt, neither of my regexps work quite how I want: import os import re
6
8366
by: =?Utf-8?B?ZGlhdG9tQG5ld3Nncm91cC5ub3NwYW0=?= | last post by:
Hello, I have a data entry windows form. One of the text boxes allows the user to enter a string. I need this text box to only allow users to type in a negative integer value (e.g. -1, -2, -3). Current approach: I want to validate the user's entry during the TextChanged() event. During the validation, I need a function that will parse the string and see if it is a negative integer. Does anyone have suggestions on the best way to...
5
7137
by: phpCodeHead | last post by:
I am needing to determine how to go about validating that a field in my form contains only a positive integer. I know that this is fairly simple if the form contains only one element to be validated; but, a much bigger challenge ( to me anyway, that's why I'm coming to the pros! ) when I don't know exactly how many fields may appear on the form as it is dynamically generated based upon the number of line items to be received on a purchase...
2
1937
by: Mick Walker | last post by:
Hi, I have a problem that I have been trying to figure for a couple of days, and am wondering if anyone out there would be so kind as to give me a solution. (Deadline time) I am trying to validate a form. Its quite a simple form, but I am a wee bit stuck. Baically it consists of 9 text input fields and a select element. All elements on the form, have to have a value in them (manditory), BUT 2 items (txtEmail, and txtDOB) have to match...
1
3119
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric numbers. Now i use this script for validate the email address. But it allows the cpital letters otherwise its working correctly. SCRIPT FUNCTION ************************************************
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.