• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing two string variables

 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have a form with text input and this is the function that is executed onchange event (inputObject is the input text object that this event was fired on):



Now when alert comes out it says: current=*123.00* new=*123.00* but it shows B on the next alert, why??
Both variables should be strings. originalInputValues array consist of inputObject.value's previously inserted.
It seems so obvious to be equal. Why it is not??
I've tried equals() and eval(). Please help
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leading or trailing whitespace perhaps?
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, first alert shows: current=*123.00* new=*123.00*
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you escape it?



Eric
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of using escape is the same as before:

current=*123.00* new=*123.00*

and B being displayed

Could it be that there is some issue in javascript when comparing array entries with strings ??
What else could it be?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be no problems, make sure you are not cached.

What about forcing to be strings?

if (original.toString() == newValue.toString()){

Eric
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've cleaned cached, reopened browser and tried:

if (original.toString() == newValue.toString()){

with the same result...

It can't be happening for real....

This originalInputValues array is associative array, could it be the reason? Associative arrays have something like {one:value1, two:value2, three:value3, ...} representation.
But alert shows it like "value1" (probably alert is using toString() anyway) and this condition is also false when using toString() on both variables.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This originalInputValues array is associative array

There's no such thing as an "associative array" in JavaScript. It's just a JavaScript object to which dynamic properties have been added. And no, values stored as object properties act the same as any other values, so that can't be an issue.
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think problem can be somewhere else than in javascript. This script is embedded in jsp file working in Struts framework.
I've tried and write whole contruction in plain html/javascript and it works.

What would be the possible reason for it that it isnt working in jsp?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Once a JSP, Struts-generated or otherwise, leaves the server, it's just an HTML response like any other.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you view the page source does anything look strange to you in your JavaScript markup?

Eric
 
Cloudgen C.F. Wong
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Nowacki wrote:Hi Ranchers,

I have a form with text input and this is the function that is executed onchange event (inputObject is the input text object that this event was fired on):



Now when alert comes out it says: current=*123.00* new=*123.00* but it shows B on the next alert, why??
Both variables should be strings. originalInputValues array consist of inputObject.value's previously inserted.
It seems so obvious to be equal. Why it is not??
I've tried equals() and eval(). Please help


Have you tried to convert the string into ascii value for inspection?
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved. I've played events too much. I have onchange, onfocus and onblur events in one input:



That is because I want this field to be checked if it is a number. Then I want to disable submit button if all values on form come back to their original values.
To do this I must remember original value (onfocus) and then if something change compare this new value to original and see if other values are also in original state so I can
disable submit button.
onblur use this self.focus() function because if someone put a non-number value, browser shouldnt permit user to tab to another field (keep focus). SetTimeout is for Firefox browser so it
postpone focus execution.

Problem was that i've put this in each function and while processing one event there was other event that overlapped and override my key variable (after first alert).
I've changed it to pass this.value and this.name. Also, I've added break to for loop that iterates through originalInputValues whenever entry is found.

Im not happy with this design. But this is the only idea that comes to my mind right now. Maybe you have some advise on how to save original state of all form values and then
watch for it and disable/enable submit button?
 
Cloudgen C.F. Wong
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about disable all the remaining input box(es) once the user focus on one input box and the format of the input text is not numeric value.
Using the onkeyup event to check the format and enable all the remaining field if it is numeric value.

It may be better than the setTimeout for focus the original input box.

Another choice is using a plugin for handling keydown event. Maybe you can use the my jquery format plugin: http://cloudgen.w0ng.hk/jquery/format.php
 
Pawel Nowacki
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good to me, but if I disable remaining input box(es) Im still able to tab to save button (or anything else not disabled) and there is a requirement that it should not
pass to other control if its not number.
Still setTimeout its not good thing, I agree.
I'll check this link. Thank you Cloudgen.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic