Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Cancel default form behavior

    The code below is supposed to cancel the default form behavior when it finds certain info missing. Currently, however, in spite of detecting missing info it allows the form submission to continue. Anyone see what wrong? Thanks.

    Code:
    var DOM = YAHOO.util.Dom;
    YAHOO.util.Event.onDOMReady(init);
     
    function init() {
    	YAHOO.util.Event.addListener("contact_form", "submit", checkForm);
    }
     
    function checkForm() {
    	var fName = DOM.get('f_name');
    	var lName = DOM.get('l_name');
    	
    	if (fName.value == "") {
    		DOM.get('f_name_error').innerHTML = "This field is required!";
    		fName.focus();
    		return false; 
    	}
    Last edited by Dan06; 12-12-2009 at 01:08 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, it was a YUI framework thing; code should have been:
    Code:
    function checkForm(e) {
    	var fName = DOM.get('f_name');
    	var lName = DOM.get('l_name');
    	
    	if (fName.value == "") {
    		DOM.get('f_name_error').innerHTML = "This field is required!";
    		fName.focus();
    		YAHOO.util.Event.preventDefault(e);
    	}


  •  

    Posting Permissions

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