Created: August, 1999; Updated October 2002

How do I validate a form when the form has an image as submit?

Question: I am trying to trigger a javascript form validation function when an image submit button is pressed. It works fine in explorer but in netscape, it does not work.

Does anyone have experience with a similar problem ?

Answer: JavaScript does not recognize images as a valid form object, unfortunately. So you have to do something which will require your users to have JavaScript - that is, use an image with a link with the JAVASCRIPT: protocol, or a link with an ONCLICK in it.

This form will only submit if you change dog to cat


Go

Source Code

<script type="text/javascript" language="JavaScript">
<!--
//--------------------------------/
// CODE BY JOE CRAWFORD
// http://artlung.com/
//
// This code is a means of using
// image as a replacement
// for a submit button, which
// allows for validation to
// take place
//---------------------------------/
function validate_and_submit_Form() {
if (document.forms[0].bar.value != 'cat')
	{ alert ('You didn\'t change dog to cat!'); }
else
	{ document.forms[0].submit() }
}
//-->
</script>
blog comments powered by Disqus