Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Compare

Status
Not open for further replies.

Marine1969

IS-IT--Management
Mar 5, 2015
60
US
Hello,

I am running a comparison between a variable passed to a function and the value from an input box. I have tried setting all variables to strings using variable.toString() and it never comes up true. Why do they never return true when they show the same string? If I put the string into the comparison it comes up true (but not when the string is passed to the function:
JavaScript:
if (bar=='039842000189')

JavaScript:
function barc_a(id, bcb, bcc) {
	myid='a'+id;	
	var bar = document.forms[myid].bc.value;
	bar=bar.toString();
	bcb=bcb.toString();
	bcc=bcc.toString();
	
   if (bar==bcb) {var xbcb = true;} else {var xbcb = false;}
   if (bar==bcc) {var xbcc = true;} else {var xbcc = false;}
   alert(xbcb);
}

 
I found a function called .valueOf() that worked.


Correction: This worked because I had hardcoded the value, it does not work.
 
What type of element is bc exactly? Is "bc" the actual name of the element you are trying to address?

Are you certain this: document.forms[myid].bc.value; is returning an actual value?

What happens if you alert(bar)?








----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
document.forms[myid].bc.value is from a textbox with the value of 039842000189
document.forms[myid].bc.value is 039842000189

bcb is 039842000189
 
HTML:
<input type="text" name="bc" size="15" placeholder="Barcode" <?php echo $msg;?>   onblur="barc_a('<?php echo $row['idinv'];?>', '<?php echo $pro['barcodebottle'];?> ', '<?php echo $pro['barcodecase'];?>')">
 
So if do alert(bar); in your function, does it have an expected value? It doesn't show undefined?

Are all your PHP values actually populated when the page is loaded? bcb and bcc have correct values when the function is called? You can do a view source from the browser menu once the page is loaded in the browser, and check that all values are in your functions calls correctly. You should not see any PHP when viewing the source from the browser. Only the actual values of your variables there. And that is what JS will see.







----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
I found the problem, it's had to spot. In the html code of the barcodebottle php script variable there is a space between > and '. Once I removed the space the issue went away.

Thank you so much for the help.

PHP:
;?> ',

BTW...I only saw it once I looked at the page source.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top