Validation in IE 7 - Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saith
    New Member
    • Feb 2008
    • 7

    Validation in IE 7 - Issue

    I need to define a text box that accepts a string input. If the user enters a value thats invalid I want to display a warning but prevent him from taking focus away from that box unless he corrects the data input.

    For example:
    <input name="Textbox1" value="Hello World One" onchange="if (this.value == 'bug'){alert('E rror: - 1'); return false;}"></input>

    In this test if i enter 'bug' focus should not leave this field unless i change 'bug' to something else and I should get the alert each time i attempt to.

    This input field works fine under IE6 but under firefox or IE 7.

    I heard that IE 6 used standards that were not in compliance with W3C...I dont know if this is the reason why i have this problem :(
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Instead of return false, use this.focus().

    Comment

    • Saith
      New Member
      • Feb 2008
      • 7

      #3
      Thanks for the response, but this does not work for me. I've using a bunch of stuff to get focus back to the input box that generated the error but even when focus is returned the user can Tab out without gettign the error message again :(

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by Saith
        Thanks for the response, but this does not work for me. I've using a bunch of stuff to get focus back to the input box that generated the error but even when focus is returned the user can Tab out without gettign the error message again :(
        That's because onchange is fired when the input changes, not when you blur. Use onblur, but alerts onblur are not advisable. Use some visible notification, e.g. red border and an error message next to the field instead.

        Comment

        • Saith
          New Member
          • Feb 2008
          • 7

          #5
          The onblurr with with this.focus and return true did the trick. thanks!

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            No problem, you're welcome. Glad to hear that you got it working.

            Comment

            • Saith
              New Member
              • Feb 2008
              • 7

              #7
              Hello again, something else just came to mind...Why is it not appropriate to use an alert in a onBlur event handler?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by Saith
                Hello again, something else just came to mind...Why is it not appropriate to use an alert in a onBlur event handler?
                See this link (How it works).

                Comment

                Working...