count a specific character in a string

M

Martin Nadoll

Hello,

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more than
one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as value
for that field?

i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

Thanks for any help,
Martin Nadoll
 
S

Silvio Bierman

Martin Nadoll said:
Hello,

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more than
one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as value
for that field?

i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

Thanks for any help,
Martin Nadoll

Probably the most efficient way is looping over the characters and count for
yourself. You could do a replaceAll with an empty string and compare
lengths.

Silvio Bierman
 
G

Geoff Tucker

Martin Nadoll said:
Hello,

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more than
one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as value
for that field?

i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

Thanks for any help,
Martin Nadoll

var yourInput=document.forms['myForm'].elements['myInputField'];

if (yourInput.value.split(".").length-1 > 1)
{
alert ("Please not more than one dot!!");
return false;
}

Works for me
Geoff
 
S

Silvio Bierman

Geoff Tucker said:
Martin Nadoll said:
Hello,

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more than
one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as value
for that field?

i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

Thanks for any help,
Martin Nadoll

var yourInput=document.forms['myForm'].elements['myInputField'];

if (yourInput.value.split(".").length-1 > 1)
{
alert ("Please not more than one dot!!");
return false;
}

Works for me
Geoff

Good idea but I think you should escape the "." because it has special
meaning in a regexp.

Silvio Bierman
 
M

Michael Winter

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more
than one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as
value for that field?

Yes. String validation is best performed with regular expressions. The
snipped below will check that the number in 'num' is either an integer, or
a (simple) floating point representation:

if( /^\d+(\.\d+)?$/.test( num )) {
// num is valid
} else {
// num contains letters, symbols, or more than one dot (.)
}

The expression permits the following:

- A string composed entirely of digits with a minimum of one digit (e.g.
512)
- A string that begins with at least one digit, followed by a single dot,
and ending with at least one digit (e.g. 4.2 or 16.333)

If users attempt to enter other valid number representations, such as 5e-2
(0.05), they will fail. Any value that contains a letter or symbol will
also fail.

Don't forget: you should always perform validation on the server.
Client-side validation is no substitute.

Hope that helps,
Mike
 
R

Randy Webb

Martin said:
Hello,

I am working on a form-validation script.
There is a input-field where you input a float or integer numper (maximum
price to output in database-query).

But my Cold-Fusion Query generates a >error-message, if there is more than
one dot in that value e.g. 15.60.50

Is it possible to validate, that there is only one or no dot given as value
for that field?

i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

myVar = document.myForm.myInputField.value.split('.');
if (myVar.length>2)
{
alert('You have entered more than one decimal point');
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Is it possible to validate, that there is only one or no dot given as value
for that field?

OK = !/\..*\./.test(F.X0.value)

I've not tested for speed, but a RegExp scan ought to be reasonably
efficient, and the method generates no additional objects.

Most validation can be done in a similar manner, with the conditions for
the test s supplied in an array of object literals - see <URL:http://www
..merlyn.demon.co.uk/js-valid.htm>.
 
M

Mick White

Martin said:
i tried with:
if (document.myForm.myInputField.value.indexOf(".")>1) {
alert ("Please not more than one dot!!")
return false
}

but that doesn't work

Thanks for any help,
Martin Nadoll


return !isNaN(document.myForm.myInputField.value)
Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Michael Winter <[email protected]
d> posted at Wed, 25 Feb 2004 00:04:48 :-
Don't forget: you should always perform validation on the server.
Client-side validation is no substitute.

Half true; it applies to cases in which a form is submitted to a server
for subsequent processing, and acceptance of incorrect entries would be
against the interests of the page owner. It is a rather common case,
but it is not the only case. It is perfectly possible to serve a page
that does processing at the client on client-provided information,
presenting results to the client; in that case, only client-side
validation is possible, and if the user suborns the code that's his
problem.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Not to mention that the amount of storage and bandwidth required by your
posts admonishing people for including "superfluid" information in their
attribution has probably far exceeded the amount of storage and bandwidth
required by extra information in their attributions.

Moreover, the "authority" that Lahn quotes is just someone's Web page,
translated from German.

The true authoritative documents, the RFCs and the like, clearly
envisage without disfavour using a full attribution, and discuss how
different parts of such may be helpful in different circumstances.

One merit of such would apply in the case of the Lahn rant itself, or
rather that of the rest of the article. The article which he cites is
not now present in my newsbase; but, without a dated attribution, I
cannot see whether it is an aged article, part of a discussion here in
which there is no longer any interest, or whether the article is in a
newsgroup that I either do not take or retain for a shorter period.

Perhaps it has not occurred to Thomas Lahn that one day, when he grows
up, he may be seeking employment; and an employer, particularly of one
from a computer-related search, may well try an Internet search. Most
employers want people who can interact well with others; but not
monomaniacal despots /in posse/.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top