SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation - return false not working

    Hi guys, quickly made a script to test the value of a drop down box. If it is a default value, an alert box pops up. This works fine, however, on pressing ok on the alert, the form is still submitted.

    I have placed return false after the alert box to no avail.

    Could anyone tell me why this is?

    HTML Code:
    <head>
    	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <html>	<title>form</title>
    	<script type="text/javascript" charset="utf-8">
    		function addEvent(elm, evType, fn, useCapture) {
    			 // cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
    			// By Scott Andrew
    			if (elm.addEventListener) {
    				elm.addEventListener(evType, fn, useCapture);
    				return true;
    			} else if (elm.attachEvent) {
    				var r = elm.attachEvent('on' + evType, fn);
    				return r;
    			} else {
    				elm['on' + evType] = fn;
    			}
    		}
    
    		function validateDropDown() {
    			dropDown = document.getElementById('dropDownId');
    			if(dropDown.value == "defaultValue") {
    				alert('Please select a real value');
                                    return false;
    			}
    			 
    		}
    
    		function init() {
    			form = document.getElementById('yourFormId');
    
    			addEvent(form, 'submit', validateDropDown, false);
    		}
    
    		addEvent(window, 'load', init, false);
    	</script>
    </head>
    <form id="yourFormId" action="blah.php" method="post">
    	<select id="dropDownId">
    		<option value="defaultValue">Default</option>
    		<option value="somethingElse">Something Else</option>
    	</select>
    	<input type="submit" id="submit" />
    </form></html>
    International calls from the UK
    Cheap International Calls

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    Code:
    function validateDropDown() {
        dropDown = document.getElementById('dropDownId');
        if(dropDown.selectedIndex == 0) {
            alert('Please select a real value');
            return false;
        }
    }
    or
    Code:
    function validateDropDown() {
        dropDown = document.getElementById('dropDownId');
        if(dropDown&#91;dropdDown.selectedIndex&#93;.value == "defaultValue") {
            alert('Please select a real value');
            return false;
        }
    }
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the response, but it isnt the validation that is a problem. The script checks the drop down box just fine, its just once the alert pops up and you click ok, the form still submits.

    return false in the script doesnt prevent the form from submitting.

    Any ideas?

    thanks
    International calls from the UK
    Cheap International Calls

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, right! Sorry.

    How about this?
    Code:
    function validateDropDown(e) {
        dropDown = document.getElementById('dropDownId');
            if(dropDown.value == "defaultValue") {
                alert('Please select a real value');
                if (window.event) {
                    window.event.returnValue = false;
                } else {
                    e.preventDefault();
                }
            }
    }
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent, thanks once again!
    International calls from the UK
    Cheap International Calls


Bookmarks

Posting Permissions

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