File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Comparing two string variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Comparing two string variables" Watch "Comparing two string variables" New topic
Author

Comparing two string variables

Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

Leading or trailing whitespace perhaps?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
No, first alert shows: current=*123.00* new=*123.00*
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Did you escape it?



Eric
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 14, 2008
Posts: 67
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

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

Joined: Nov 14, 2008
Posts: 67
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

Joined: Jan 10, 2002
Posts: 61606
    
  67

No. Once a JSP, Struts-generated or otherwise, leaves the server, it's just an HTML response like any other.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
When you view the page source does anything look strange to you in your JavaScript markup?

Eric
Cloudgen C.F. Wong
Greenhorn

Joined: Jul 15, 2009
Posts: 19
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?


cheers
Pawel Nowacki
Ranch Hand

Joined: Nov 14, 2008
Posts: 67
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

Joined: Jul 15, 2009
Posts: 19
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

Joined: Nov 14, 2008
Posts: 67
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
 
subject: Comparing two string variables