I am trying to create my own comment form but it seems that the function checkForm() isn't called by the onclick event. Can anyone help me out? <html> <head> <script type="txt/javascript"> script called... function checkForm(){ var name,email,url,msg; with(window.document.comment_form) { name=comment_name; email=comment_email; url=comment_website; msg=comment_msg; } if(trim(name.value)=='') { alert('Please enter your name'); name.focus(); return false; } else if(trim(email.value)!='') { alert('Please enter a valid email'); email.focus(); return false; } else if(trim(msg.value)=='') { alert('Please enter your msg'); msg.focus(); return false; } else { return true; } } function trim(str) { return str.replace(/^\s+|\s+$/g,''); } </script> </head> <body> <form method="post" name="comment_form"> <table width="550" border="0" cellpadding="2" cellspacing="1"> <tr> <td width="100">Name *</td> <td><input name="comment_name" type="text" size="30" maxlength="30"></td> </tr> <tr> <td width="100">Email</td> <td><input name="comment_email" type="text" size="30" maxlength="30"></td> </tr> <tr> <td width="100">URL:</td> <td><input name="comment_website" type="text" size="30" value="http://" size="30" maxlength="50"></td> </tr> <tr> <td width="100">Comment</td> <td><textarea name="comment_msg" cols="80" rows="5"></textarea></td> </tr> <tr> <td width="100"> </td> <td><input name="Send_button" type="submit" value="Submit Comment" onclick="return checkForm();"></td> </tr> </body> </html> HTML:
Try putting onsubmit in the form tag: <form action="pagenamehere.htm" onsubmit="return formCheck()" method="post"> PHP:
<form onsubmit="return checkForm();"> HTML: or <form onsubmit="return checkForm()"> HTML: or <form onSubmit="return checkForm()"> HTML: or <form onSubmit="return checkForm();"> HTML:
1. Remove the script called... 2. Change <script type="txt/javascript"> to <script type="text/javascript">
<html> <head> <script type="text/javascript"> function checkForm(){ var name,email,url,msg; with(window.document.comment_form) { name=comment_name; email=comment_email; url=comment_website; msg=comment_msg; } if(trim(name.value)=='') { alert('Please enter your name'); name.focus(); return false; } else if(trim(email.value)!='') { alert('Please enter a valid email'); email.focus(); return false; } else if(trim(msg.value)=='') { alert('Please enter your msg'); msg.focus(); return false; } else { return true; } } function trim(str) { return str.replace(/^\s+|\s+$/g,''); } } </script> </head> <body> <form method="post" name="comment_form"> <table width="550" border="0" cellpadding="2" cellspacing="1"> <tr> <td width="100">Name *</td> <td><input name="comment_name" type="text" size="30" maxlength="30"></td> </tr> <tr> <td width="100">Email</td> <td><input name="comment_email" type="text" size="30" maxlength="30"></td> </tr> <tr> <td width="100">URL:</td> <td><input name="comment_website" type="text" size="30" value="http://" size="30" maxlength="50"></td> </tr> <tr> <td width="100">Comment</td> <td><textarea name="comment_msg" cols="80" rows="5"></textarea></td> </tr> <tr> <td width="100"> </td> <td><input name="Send_button" type="submit" value="Submit Comment" onClick="return checkForm();"></td> </tr> </body> </html> HTML: