newbie: how to compare form input to variable value?

C

Cessna

Hello,

In my form validation, I'm testing a form input field to see if it contains
the same number as a variable contains.

if the variable is called "match1", and my user text field is named "guess1"

I'm trying to create a statement in my form validation function something
like this:


if (document.form1.guess1.value) !== (match1)
{
alert("Please guess again ...");
form1.guess1.focus();
return false;
}

i think i'm not doing the == statement properly,
can anyone give me a hand with this?

thanks!

CN
 
L

-Lost

Cessna said:
Hello,

In my form validation, I'm testing a form input field to see if it contains
the same number as a variable contains.

if the variable is called "match1", and my user text field is named "guess1"

I'm trying to create a statement in my form validation function something
like this:


if (document.form1.guess1.value) !== (match1)
{
alert("Please guess again ...");
form1.guess1.focus();
return false;
}

i think i'm not doing the == statement properly,
can anyone give me a hand with this?

if (Number(document.forms['form1'].elements['guess1'].value) != match1)

Or, if you must:

if (Number(document.form1.guess1.value) != match1)

I am using the Number object keyword to make sure your form value is
treated as a Number.
 
E

Evertjan.

Cessna wrote on 06 mei 2007 in comp.lang.javascript:
In my form validation, I'm testing a form input field to see if it
contains the same number as a variable contains.

if the variable is called "match1", and my user text field is named
"guess1"

I'm trying to create a statement in my form validation function
something like this:


if (document.form1.guess1.value) !== (match1)

1 you need an outer ()
2 enclosing results in () is not useful
3 use != for simple "not equal"
4 use crosbrowser code:

if (document.forms['form1'].elements['guess1'].value != match1)
{
alert("Please guess again ...");
form1.guess1.focus();
document.forms['form1'].elements['guess1'].focus();

return false;
}

i think i'm not doing the == statement properly,

You did not use a ==, and == is not a statement but an operator

==========

Usually, when validating a form,
it is easier and cleaner to use "this":

<form onsubmit='return validate(this)' ...

var match1 = 'Blahblah';

function validate(f) {
if (f.elements['guess1'].value != match1) {
alert("Please guess again ...");
f.elements['guess1'].focus();
return false;
};
};
 
D

Dr J R Stockton

In comp.lang.javascript message <frWdnbk8dferFqDbnZ2dnUVZ_oKnnZ2d@comcas
I am using the Number object keyword to make sure your form value is
treated as a Number.

For the conversion, unary + is considerably faster than Number() - and
it's quicker to type, too.

Remember William : "entia non sunt multiplicanda praeter necessitatem".

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

-Lost

Dr said:
In comp.lang.javascript message <frWdnbk8dferFqDbnZ2dnUVZ_oKnnZ2d@comcas


For the conversion, unary + is considerably faster than Number() - and
it's quicker to type, too.

Really? Is this listed anywhere? These are the kinds of things I love
to keep track of. (Or would like to understand better.)

I will stockpile the tip nonetheless. Thanks.
Remember William : "entia non sunt multiplicanda praeter necessitatem".

Of course, of course. Had I only known of the speed difference.
 
L

-Lost

Ed said:
Dr J R Stockton scribed:

Si hoc legere scis nimium eruditionis habes!

:) I think this applies doubly in Doc J's case.

(Nevermind the fact I read it.)
 
D

Dr J R Stockton

In comp.lang.javascript message <9pKdnYvdksgJxKPbnZ2dnUVZ_urinZ2d@comcast
Really? Is this listed anywhere?

The newsgroup FAQ, which you should read, indicates the use of unary +.
It has been suggested here, more than once, including recently, that the
relevant section of the FAQ needs pruning of deprecated methods -0 & +1.

It's mentioned in my site, which cites <http://xkr.us/articles/javascript
/unary-add/>.

It's in ECMA-262 and ISO 16262.

It's often mentioned here.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

-Lost

Dr said:
In comp.lang.javascript message <9pKdnYvdksgJxKPbnZ2dnUVZ_urinZ2d@comcast


The newsgroup FAQ, which you should read, indicates the use of unary +.
It has been suggested here, more than once, including recently, that the
relevant section of the FAQ needs pruning of deprecated methods -0 & +1.

In the newsgroup FAQ, which you may not have read, also suggests the use
of Number(value).
It's mentioned in my site, which cites <http://xkr.us/articles/javascript
/unary-add/>.

Um... that site makes no mention of the unary + operator being faster
than "casting" to a Number.
It's in ECMA-262 and ISO 16262.

It says that it is faster than using Number? I have not seen that at all.
It's often mentioned here.

Can you provide me an example string to use to initiate a search in this
group then? I tried "+ faster", "unary + faster", "Number +", "Number
unary" et cetera and found nothing relevant to the unary + operator
being faster than Number.

Thanks.
 
A

ASM

-Lost a écrit :
found nothing relevant to the unary + operator
being faster than Number.

it is very very more faster because
+
is shorter than
Number()
in octets in the file to transmit

:)


Sériously,
I've read here and there that '+' was the best way to convert a string
in a number. (useness or reality ? stays open)
 
E

Evertjan.

ASM wrote on 08 mei 2007 in comp.lang.javascript:
Seriously,
I've read here and there that '+' was the best way to convert a string
in a number. (useness or reality ? stays open)

<script type='text/javascript'>

var t;
var s = '1.2345';
var r;

t = new Date();
for (var i=0;i<100000;i++)
{r = +s;r = +s;r = +s;};
alert(new Date()-t + ' miliseconds');

t = new Date();
for (var i=0;i<100000;i++)
{r = Number(s);r = Number(s);r = Number(s);};
alert(new Date()-t + ' miliseconds');


</script>
 
A

ASM

Evertjan. a écrit :
ASM wrote on 08 mei 2007 in comp.lang.javascript:


<script type='text/javascript'>

var t;
var s = '1.2345';
var r;

t = new Date();
for (var i=0;i<100000;i++)
{r = +s;r = +s;r = +s;};
alert(new Date()-t + ' miliseconds');

t = new Date();
for (var i=0;i<100000;i++)
{r = Number(s);r = Number(s);r = Number(s);};
alert(new Date()-t + ' miliseconds');


</script>

Boudiou !
FireFox 2 : approx 3800 and 5200
Safari 1.3 : approx 1000 and 1500
IE (Mac) : approx 500 and 600

Tremendous !
IE is very much faster than FF ? ! ? !
 
E

Evertjan.

ASM wrote on 08 mei 2007 in comp.lang.javascript:
Evertjan. a écrit :

Boudiou !
FireFox 2 : approx 3800 and 5200
Safari 1.3 : approx 1000 and 1500
IE (Mac) : approx 500 and 600

Tremendous !
IE is very much faster than FF ? ! ? !

It [also] strongly depends on your processor speed, methinks.

Is all the above on the same machine, Stéphane?
 
A

ASM

Evertjan. a écrit :
ASM wrote on 08 mei 2007 in comp.lang.javascript:



Is all the above on the same machine, Stéphane?

Yes, of course.

I'm very surprised that IE fights (and so much) FF in arithmetic.
 
D

Dr J R Stockton

In comp.lang.javascript message <nsSdndaurb5DWqLbnZ2dnUVZ_rWnnZ2d@comcas
Um... that site makes no mention of the unary + operator being faster
than "casting" to a Number.

Do you mean my site or XKR?

The XKR page begins "In JavaScript it is possible to use the + operator
alone before a single element. ... This is especially useful when one
wants to convert a string to a number quickly, but can also be used on a
select set of other types."

My site may not have mentioned the speed for long; but it did so before
I despatched the article to which you have responded.

But it should be easy enough for you to run a speed test.
 
L

-Lost

Evertjan. said:
ASM wrote on 08 mei 2007 in comp.lang.javascript:


<script type='text/javascript'>

var t;
var s = '1.2345';
var r;

t = new Date();
for (var i=0;i<100000;i++)
{r = +s;r = +s;r = +s;};
alert(new Date()-t + ' miliseconds');

t = new Date();
for (var i=0;i<100000;i++)
{r = Number(s);r = Number(s);r = Number(s);};
alert(new Date()-t + ' miliseconds');


</script>

Thanks for this Evertjan..
 
L

-Lost

Evertjan. said:
ASM wrote on 08 mei 2007 in comp.lang.javascript:
Evertjan. a écrit :
Boudiou !
FireFox 2 : approx 3800 and 5200
Safari 1.3 : approx 1000 and 1500
IE (Mac) : approx 500 and 600

Tremendous !
IE is very much faster than FF ? ! ? !

It [also] strongly depends on your processor speed, methinks.

Yeah, I agree. My times were just a *little* bit longer than
Stéphane's. Internet Explorer was faster as well (both on Mac and on
Windows).
Is all the above on the same machine, Stéphane?

I used my Windows laptop and my *very old* iMac.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
...
t = new Date();
for (var i=0;i<100000;i++)
{r = +s;r = +s;r = +s;};
alert(new Date()-t + ' miliseconds');
...

A test harness along those lines is a permanent option in my Web page
js-quick.htm, albeit concealed behind the "Demo" button as Number 6.
One just fills in the code to be compared and chooses a count.
 

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
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top