...

View Full Version : Resolved Cancel default form behavior



Dan06
12-12-2009, 01:58 AM
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.



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;
}

Dan06
12-12-2009, 02:08 AM
Ah, it was a YUI framework thing; code should have been:


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);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum