473,387 Members | 1,515 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.

count a specific character in a string

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
Jul 20 '05 #1
9 19969

"Martin Nadoll" <ma****@nadoll.de> wrote in message
news:c1*************@news.t-online.com...
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
Jul 20 '05 #2

"Martin Nadoll" <ma****@nadoll.de> wrote in message
news:c1*************@news.t-online.com...
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
Jul 20 '05 #3

"Geoff Tucker" <ge***@tucker24.fsnet.co.uk> wrote in message
news:c1**********@newsg2.svr.pol.co.uk...

"Martin Nadoll" <ma****@nadoll.de> wrote in message
news:c1*************@news.t-online.com...
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
Jul 20 '05 #4
On Tue, 24 Feb 2004 19:35:51 +0100, Martin Nadoll <ma****@nadoll.de> wrote:
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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5
Martin Nadoll wrote:
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');
}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #6
Martin Nadoll wrote:
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
Jul 20 '05 #7
JRS: In article <c1*************@news.t-online.com>, seen in
news:comp.lang.javascript, Martin Nadoll <ma****@nadoll.de> posted at
Tue, 24 Feb 2004 19:35:51 :-
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>.

--
© 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 #8
JRS: In article <op**************@news-text.blueyonder.co.uk>, seen in
news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.invali
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.

--
© 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
JRS: In article <40**************@agricoreunited.com>, seen in
news:comp.lang.javascript, Grant Wagner <gw*****@agricoreunited.com>
posted at Wed, 31 Mar 2004 17:58:31 :

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/.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #10

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

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
2
by: Jerry | last post by:
Hi - Using XSL I need to count the number of times character appears in a string. My guess is I need to recurse through the value of x, but I'm having trouble getting my head around the...
7
by: news.hku.hk | last post by:
how to count the occurance of a character in a string ? e.g. #include <iostream> #include <string> using namespace std; int main (){ string text = "abc, def, ghi, jkl, mno"; // how to count...
24
by: Nalla | last post by:
Hi, I want a program. It should be a command line one. you can input the path of a folder(preferably) or a file...it should count the no. of lines between the compiler directives, ifdef win32 and...
4
by: thomaz | last post by:
Hi.... There is a string method to count the total number of a specified character in a string. EX: count the total of (*) in a string *** Test *** Thanks....
10
by: Jon | last post by:
I want to count the number of instances of a certain string(delimiter) in another string. I didn't see a function to do this in the framework (if there is, please point me to it). If not, could...
3
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
3
by: chrisperkins99 | last post by:
It seems to me that str.count is awfully slow. Is there some reason for this? Evidence: ######## str.count time test ######## import string import time import array s = string.printable *...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.