Forum Moderators: open
<script type="text/javascript" language="JavaScript">
function checkComment(f)
{
var validcontent = /^([0-9a-zA-Z\.\ \?])$/;
if (!validcontent.test(f.comment.value)) {
alert("Your Comment field can only contain numbers, letters, .,? and spaces");
document.getElementById('comment').focus();
return false;
}
}
</script><form method="POST" action="#" name="form1" onsubmit="checkComment(this)">
<textarea cols="20" rows="5" name="comment" id="comment"></textarea>
<input type="Submit" name="submit" value="send">
</form>
I just learned since it's using a custom templating language I don't have access to the form tag to use the onsubmit event. Is there a way to modify the script so that it uses onclick with the submit button?
<form action="#" method="get" id="my_form"><div>
<script type="text/javascript">
// <![CDATA[
function onsubmithandler(){ alert ( "Submit Event Caught!" ); } // Either of these will work:
document.forms[0].onsubmit=onsubmithandler;
document.getElementById('my_form').onsubmit=onsubmithandler;
// ]]>
</script>
<input type="submit" value="Submit" />
</form>
In a case like yours, you can't access the form, so a workaround is to grab one of the form input elements, and get its "form" property, like so:
<form action="#" method="get" id="my_form"><div>
<input type="submit" id="submit_button" value="Submit" />
<script type="text/javascript">
// <![CDATA[
function onsubmithandler(){ alert ( "Submit Event Caught!" ); }
// This is a way to do it if you don't have access to the form object.
document.getElementById('submit_button').form.onsubmit=onsubmithandler;
// ]]>
</script>
</div></form>
} else {
document.getElementById('form1').submit();
}
<form method="POST" action="#" name="form1" >
<textarea cols="20" rows="5" name="comment" id="comment"></textarea>
<input type="button" name="submitBtn" value="send" onclick="checkComment();">
</form>
<script language="javascript" type="text/javascript">
function validateFields() {
var validcontent = /^[\w\s\.\,\?\!]*$/;
var chkfields = new Array()
chkfields[0] = "orxgiftmessage"
chkfields[1] = "orxdelinstructions1"
chkfields[2] = "orxdelinstructions2"
chkfields[3] = "orxcusservicesinstructions"
for (i=0; i < chkfields.length; i++)
if (!validcontent.test(document.getElementById(chkfields[i]).value)) {
alert("Please make sure you are only using alphanumeric characters,!,?, . and commas in the message fields");
return;
}
if (!document.wizform.cterm.checked) {
alert('Please read our Terms & Conditions and then tick the confirmation box provided.');
document.wizform.cterm.focus();
return;
}
<template_tag step=finish,mode=process,param1=process,check=yes,callonly=yes>
}
</script>