blonde question...

T

Terry Olson

I need to do a if statement on only textfields that have a number in them
other then zero. What is the best way to filter out all field values other
then numbers other then zero? I haven't had any luck, I know this should be
a simple thing, but I'm having a blonde day.
 
J

John Bokma

Terry said:
I need to do a if statement on only textfields that have a number in
them other then zero. What is the best way to filter out all field
values other then numbers other then zero? I haven't had any luck, I
know this should be a simple thing, but I'm having a blonde day.

Paint your hair :-D

Get all textfields, check if they have a number, and check if it's zero or
not.
 
T

Terry Olson

Thank you for turning my question into a statement, and completely avoiding
answering it. Anyone out there want to give me SPECIFICS?
 
J

John Bokma

Terry Olson wrote:

Don't top post
Thank you for turning my question into a statement, and completely
avoiding answering it. Anyone out there want to give me SPECIFICS?

You asked how, my answer is one way. But you probably want a complete
spelled out solution?
 
M

Michael Winter

I need to do a if statement on only textfields that have a number in
them other then zero. What is the best way to filter out all field
values other then numbers other then zero? I haven't had any luck, I
know this should be a simple thing, but I'm having a blonde day.

It would be simple if you were more specific.

When are you checking the text fields? Are you checking *every* text
input, or just a subset?

When these fields contain numbers, should they only contain numbers, and
what do you mean by "filter out"?

About the only thing I can say with certainty is that your answer lies
with regular expressions.

Mike
 
D

Dr John Stockton

JRS: In article <GmBad.74338$a41.62715@pd7tw2no>, dated Mon, 11 Oct
2004 19:42:30, seen in Terry Olson
I need to do a if statement on only textfields that have a number in them
other then zero. What is the best way to filter out all field values other
then numbers other then zero? I haven't had any luck, I know this should be
a simple thing, but I'm having a blonde day.

Firstly, you have to define zero.

Is 0.0e6 zero, or is it not?

Do you actually mean number, or do you mean digit? Many posters do not
discriminate.

Is there a number in "Daddy flew a B-36", and if so is it negative?

x = /[1-9]/.test(S) // Does S contain a digit other than zero?

Does "Pi11" contain a number?

Must the number be the only non-white-space? Does leading/trailing
white-space matter?

If you can say that the only characters to be allowed in the fields are
decimal digits, the question becomes much easier.
 
M

MikeB

Michael Winter said:
It would be simple if you were more specific.

When are you checking the text fields? Are you checking *every* text
input, or just a subset?

If a group of textfields within a larger group is to be treated differently than
the others, I prefix the name of the field with a couple of meaningful identical
characters, like if the field is "Required", a field name might be
"reqFirstName" and the like, then looping through the fields I can address just
those fields to a test, rather than every field. This logic could be employed
here.
 
F

Fred Oz

mscir wrote:
[snip]
What about something like this to check every textbox and return every
non-zero value?
[snip]

Whilst your function neatly cycles through all the text boxes, it
doesn't test very rigorously.

Much more validation is required - your function will return every
value that parseFloat() can turn into a number. e.g. "5ABC" will
return "5" and be accepted by your function as a non-zero number.

The quickest (and dirtiest perhaps...) test I know of is:

if (x == parseFloat(x)) alert(x + ' is a float');

It will accept any number format, including scientific notation. Of
course, it doesn't test the format or suitability of the number for
whatever purpose you want it for (maybe you want an integer).

If you want more rigour or format validation, regular expression tests
are the go.


Cheers, Fred.
 
L

Lee

Terry Olson said:
I need to do a if statement on only textfields that have a number in them
other then zero. What is the best way to filter out all field values other
then numbers other then zero? I haven't had any luck, I know this should be
a simple thing, but I'm having a blonde day.

Coding is easier once you've learned to state the problem clearly
and concisely. If you're thinking of the problem as to "filter out
all fields values other than numbers other than zero" you're bound
to be confused.

If you restate it as to "<take some action> on all text fields that
contain numbers that are not zero", it's much simpler:

<html>
<head>
<title>demo</title>
<script type="text/javascript">
function demo(f){
for (var i=0;i<f.elements.length;i++) {
if(f.elements.type=="text" &&
!isNaN(f.elements.value) &&
0 != +f.elements.value) {
// do something with f.elements
alert(f.elements.value);
}
}
}
</script>
</head>
<body>
<form>
<input value="">
<input value="3">
<input value="0">
<input value="apple">
<br>
<input type="button" value="test" onclick="demo(this.form)">
</form>
</body>
</html>
 
L

Lee

mscir said:
var elements = document.getElementsByTagName('input');

I would avoid using getElementsByTagName(), since there is a much
older mechanism that works just as well, assuming that you're not
interested in accumulating elements from multiple forms.

var elements = thisForm.elements;

Your method will filter out the non-input elements, but you still
have to check the "type" attribute, and the older method will work
in more clients.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top