What's the problem with this code?

Discussion in 'HTML & Website Design' started by cipals15, Nov 28, 2008.

  1. #1
    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">&nbsp;</td>
    <td><input name="Send_button" type="submit" value="Submit Comment" onclick="return checkForm();"></td>
    </tr>
    </body>
    </html>
    HTML:
     
    cipals15, Nov 28, 2008 IP
  2. drew22299

    drew22299 Guest

    Messages:
    76
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try putting onsubmit in the form tag:

    <form action="pagenamehere.htm" onsubmit="return formCheck()" method="post">
    PHP:
     
    drew22299, Nov 28, 2008 IP
  3. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #3
    <form onsubmit="return checkForm();">
    HTML:
    or

    <form onsubmit="return checkForm()">
    HTML:
    or

    <form onSubmit="return checkForm()">
    HTML:
    or

    <form onSubmit="return checkForm();">
    HTML:
     
    cipals15, Nov 28, 2008 IP
  4. broco

    broco Peon

    Messages:
    706
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    1. Remove the script called...
    2. Change <script type="txt/javascript"> to <script type="text/javascript">
     
    broco, Nov 28, 2008 IP
  5. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #5
    <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">&nbsp;</td>
    <td><input name="Send_button" type="submit" value="Submit Comment" onClick="return checkForm();"></td>
    </tr>
    </body>
    </html>
    HTML:
     
    cipals15, Nov 28, 2008 IP