Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation issue

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi,

The following code validates the first select box but then the form still submits - can anyone tell me why

Code:
<script type="text/javascript">
function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_modelid"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please select model..."))
				return false;
		}
		elm = fobj.elements["x" + infix + "_doorsid"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please select bodystyle..."))
				return false;
		}
		elm = fobj.elements["x" + infix + "_specid"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please select spec..."))
				return false;
		}
		elm = fobj.elements["x" + infix + "_engineid"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please select engine variant..."))
				return false;
		}
	}
	return true;
}

function submitit(formvar)
{
  if (!ew_ValidateForm(formvar)) return false;
  formvar.x_action.value='calcRED';
  formvar.submit();
  return true;
}
</script>

<form name="form1" id="form1" action="form1.asp" method="post" style="margin:0">

<select id='x_modelid' name='x_modelid'>
<option value="" selected>
Model
</option>
</select>

<select id='x_doorsid' name='x_doorsid'>
<option value="" selected>
Doors
</option>
</select>

<select id='x_specid' name='x_specid'>
<option value="" selected>
Spec
</option>
</select>

<select id='x_engineid' name='x_engineid'>
<option value="" selected>
Engine
</option>
</select>

<input type="image" onClick="submitit(this);" src="images/submit.gif" border="0" width="56" height="30" />

</form>
 
Try:
Code:
<form name="form1" id="form1" action="form1.asp" method="post" style="margin:0" onsubmit="return submitit();">

<input type="image" src="images/submit.gif" border="0" width="56" height="30" />

Lee

 
Hi,

thanks for your post, however it still submits the form
after telling me to enter a model...

changed the submitit call to

Code:
onsubmit="return submitit(this);"

...as the function expects a form name
 
no probs...

Code:
<script type="text/javascript">
function ew_ValidateForm(fobj) {
    if (fobj.a_confirm && fobj.a_confirm.value == "F")
        return true;
    var i, elm, aelm, infix;
    var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
    for (i=0; i<rowcnt; i++) {
        infix = (fobj.key_count) ? String(i+1) : "";
        elm = fobj.elements["x" + infix + "_modelid"];
        if (elm && !ew_HasValue(elm)) {
            if (!ew_OnError(elm, "Please select model..."))
                return false;
        }
        elm = fobj.elements["x" + infix + "_doorsid"];
        if (elm && !ew_HasValue(elm)) {
            if (!ew_OnError(elm, "Please select bodystyle..."))
                return false;
        }
        elm = fobj.elements["x" + infix + "_specid"];
        if (elm && !ew_HasValue(elm)) {
            if (!ew_OnError(elm, "Please select spec..."))
                return false;
        }
        elm = fobj.elements["x" + infix + "_engineid"];
        if (elm && !ew_HasValue(elm)) {
            if (!ew_OnError(elm, "Please select engine variant..."))
                return false;
        }
    }
    return true;
}

function submitit(formvar)
{
  if (!ew_ValidateForm(formvar)) return false;
  formvar.x_action.value='calcRED';
  formvar.submit();
  return true;
}
</script>

<form name="form1" id="form1" action="form1.asp" method="post" style="margin:0" onSubmit="return submitit(this);">
<input type="hidden" name="action" value="">

<select id='x_modelid' name='x_modelid'>
<option value="" selected>
Model
</option>
</select>

<select id='x_doorsid' name='x_doorsid'>
<option value="" selected>
Doors
</option>
</select>

<select id='x_specid' name='x_specid'>
<option value="" selected>
Spec
</option>
</select>

<select id='x_engineid' name='x_engineid'>
<option value="" selected>
Engine
</option>
</select>

<input type="image" src="images/submit.gif" border="0" width="56" height="30" />

</form>
 
the form is still submitting after prompting to enter
select box values

validation function is same as above
Code:
<script language="JavaScript" type="text/javascript">
function submitit(formvar)
{
  if (!ew_ValidateForm(formvar)) return false;
  formvar.action.value='calcGREEN';
  return true;
}
</script>

<form name="form1" id="form1" action="form1.asp" method="post" onSubmit="return submitit(this);" style="margin:0">

<input type="image" src="images/submit.gif" border="0" alt="GO!" width="56" height="33"/>

</form>
 
Firefox error console says you don't have a function named ew_HasValue. I also don't see a function named ew_OnError. Firefox's error console is a WONDERFUL tool for catching JS problems.

Lee
 
hi - those functions are in an include

Code:
// Process validate error
function ew_OnError(input_object, error_message) {
	alert(error_message);
	if (typeof ew_GotoPageByElement != 'undefined') // check if multi-page
		ew_GotoPageByElement(input_object);
	ew_SetFocus(input_object);
	return false;
}

// Check if object has value
function ew_HasValue(obj) {
	if (!obj)
		return true;
	var type = (!obj.type && obj[0]) ? obj[0].type : obj.type;
	if (type == "text" || type == "password" || type == "textarea" ||
		type == "file" || type == "hidden") {
		return (obj.value.length != 0);
	} else if (type == "select-one") {
		return (obj.selectedIndex > 0);
	} else if (type == "select-multiple") {
		return (obj.selectedIndex > -1);
	} else if (type == "checkbox") {
		if (obj[0]) {
			for (var i=0; i < obj.length; i++) {
				if (obj[i].checked)
				return true;
			}
			return false;
		}
	} else if (type == "radio") {
		if (obj[0]) {
			for (var i=0; i < obj.length; i++) {
				if (obj[i].checked)
				return true;
			}
			return false;
		} else {
			return obj.checked;
		}
	}
	return true;
}

going to try FF to trap any JS errors
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top