views:

941

answers:

3

I have a Django form that needs to do some client-side validation before submitting the form. This is the code in the template:

<form action="/{{ blog_entry.id }}/addComment/" method="post">
{{ commentForm.name.label_tag }} 
{{ commentForm.name }}
<span id="spanNameReq" style="color:Red; display:none;">Required</span>
<br />     
{{ commentForm.email.label_tag }} 
{{ commentForm.email }}
<span id="spanEmailReq" style="color:Red; display:none;">Required</span>
<span id="spanEmailInvalid" style="color:Red; display:none;">Invalid e-mail address</span>
<br />
{{ commentForm.website.label_tag }} 
{{ commentForm.website }}
<span id="spanWebsiteInvalid" style="color:Red; display:none;">Invalid URL</span>
<br />    
{{ commentForm.comment.label_tag }} 
<span id="spanCommentReq" style="color:Red; display:none;">Required</span>   
<br />
{{ commentForm.comment }}
<br />  
<input type="submit" value="Add comment" onclick="javascript:var ret = validateComment(); return ret;" />  
</form>

But the problem is that validateComment does not get called at all, and the form gets submitted straight away. Strangely enough if I replace the onclick event with

javascript:alert('test');

or

javascript:return false;

that JS code gets executed fine (and in the second case it won't submit the form).

Why does it not execute the function I specified? Just to confirm that I have included the script file in the HTML head (including the JS code embedded in the template does not make a difference), and if I don't use a Django form, but a normal HTML form it works fine.

A: 

Firstly, please drop that 'javascript' pseudo-protocol prefix. It has absolutely no place in an onclick attribute. It was invented for use in hrefs, but was replaced by the on* methods - the whole point of which is that you are supposed to just use the javascript itself.

Secondly, why are you bothering with the variable? Why not just return validateComment()?

Thirdly, how are you determining that the function is 'not being called at all'? It seems likely that the function is being called, it's just not returning what you think it is. Please post the code of the function, and maybe someone will spot what's wrong.

Daniel Roseman
Thanks for the comments. Yes, you're right about the prefix and variable, I just put them in because I was trying different things to see if that would work.I know that the function is not called, because if the only line in validateComment is 'return false;' it won't stop the form submission.
tomlog
A: 

Writing Javascript events directly in your HTML can give you problems later - instead try giving the form an id, and catching the submit event from Javascript in the <head> section

for example using jQuery, and the form has id="my_form":

<script type="text/javascript">

$(document).ready(function(){

$('#my_form').submit(function(){

    if ([test here]){
        return true; 
    } else {
        return false; //prevents submission
    } 

})

})

</script>
joeformd
Thanks for the suggestion, but using the form submit event does not make a difference in this case. It won't go through that function.
tomlog
A: 

I don't think your function is being called. My hunch is you have have misspelled something. Try putting an alert() in your function.

Dan.StackOverflow
Dan, I have tried this already. I have double and triple checked the spelling. As mentioned, if I use a 'normal' HTML form rather than a Django form it works fine.
tomlog
modify your question to include the html and javascript function that does an alert() that includes the form as output from the template and then a 'normal' html form. Just so we can get a better look.
Dan.StackOverflow
It turns out that there was a syntax bug in the JavaScript funtion.
tomlog