www.webdeveloper.com
Results 1 to 5 of 5

Thread: String comparison issues

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Question String comparison issues

    I'm having some issues trying to write a function that compares two strings from textboxes. It works fine in IE, but falls over during testing in Chrome and Firefox. I've got a sample of the code here, but I'm unsure as to the best way to make this work across browsers:

    Code:
    <html>
      <head>
        <SCRIPT language="Javascript">
          <!--
          function testText()
          {
            var txtContent = new String(document.getElementById(arguments[0]).value);
            var confContent = new String(document.getElementById(arguments[1]).value);
    
            if( txtContent.length >= 4 && txtContent.toLowerCase() == confContent.toLowerCase ) {
              alert('Match!');
            } else {
              alert('No match!');
            }
          }
          //-->
        </SCRIPT>
      </head>
      <body>
    
        <form>
          <input type='text' name='text1'><br />
          <input type='text' name='text2'><br />
          <input type='button' value='Test' OnClick="testText('text1','text2')">
        </form>
    
      </body>
    </html>
    Any ideas on how I can get this function working across different browsers?

    Cheers,

  2. #2
    Join Date
    Dec 2009
    Posts
    3
    Never mind, I found the problem - IE's Javascript engine is playing too fast and loose with the rules: It accepts both 'name' and 'id' fields in document.getElementbyId, whilst Firefox and Chrome require corresponding 'id' fields to be set in order for the function to work.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You have also an HTML error: your document has no Doctype. And the notation <br />
    is specific to XHTML not to HTML. Make up your mind: either HTML Doctype and rules or XHTML Doctype and rules.

  4. #4
    Join Date
    Dec 2009
    Posts
    3
    I'm not that concerned about that - It was only test code to test the comparison function

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Rohaq View Post
    I'm not that concerned about that
    You should. Without a Doctype (that means in quirksmode) browsers do interpret the javascript and CSS codes in peculiar and different ways than the standard way, thus your test will be unconvincing. Moreover, HTML/XHTML errors might change, distort or even block the JavaScript effects.

    Whenever you encounter a JavaScript or CSS problem, make sure, first, that the HTML/XHTML pattern code is correct and valid.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles