Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript, IE, Firefox.

    I'm creating a shoutbox, and putting in simple form validation to ensure the user at least puts something in the name field. This works fine in IE, but Firefox submits the page whether there's something in the name box or not. Does anyone have any idea why this isn't working in Firefox?

    <script language="javascript">
    function CheckShout() {
    var complete = true;
    if ( Shoutbox.Shouter.value == null || Shoutbox.Shouter.value == '' ) {
    window.alert('You need to fill in your name.');
    complete = false;
    }
    return complete;
    }
    </script>

    <form name="Shoutbox" id="Shoutbox" onsubmit="return CheckShout();" action="ShoutSubmit.asp" method="post">
    Name: <input type="text" name="Shouter">
    Shout:
    <textarea rows="4" cols="20" name="Shout"></textarea>
    <input type="submit" value="Shout it!">
    </form>

    Thanks in advance for your help, encouragement and warm cuddly glows of joy.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Javascript, IE, Firefox.

    <P ID="edit" class=small>(Edited by jscher2000 on 26-Aug-04 14:58. Sorry, didn't mean to refer to TEXTAREA... also fixed a Lounge markup problem and added a note.)</P>Firefox follows the W3C document object model. To create a reference to the TEXT INPUT element, give it its own ID (e.g., ShoutText) and simplify your code as follows:

    <script language=&quot;javascript&quot;>
    function CheckShout() {
    var complete = true;
    var userText = document.getElementById(&quot;ShoutText&quot;
    if ( userText.value == null || userText.value == '' ) {
    window.alert('You need to fill in your name.');
    complete = false;
    }
    return complete;
    }
    </script>

    Note that this will work in IE 5 and higher, but it not compatible with IE 4. Your original syntax should work in IE 4.

    One other note: if you use the Tools>JavaScript console in Firefox, you will get reasonably specific error messages identifying the first line of code that doesn't work. So if the above doesn't help, please let us know the error message in the console.

    Added: W3C also has a getElementByName() method, but because only IDs are sure to be unique, I don't use it.

  3. #3
    New Lounger
    Join Date
    Aug 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Javascript, IE, Firefox.

    Spot on. Thanks for your help.

    I'd been editing that page for so long, I couldn't see the wood for all the trees!

Posting Permissions

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