form validation messages disappear

form validation messages disappear

Hi guys,

I'm learning jquery. I'm trying to display error messages when form validation fails.

Currently the error message does show but disappears straight away. I am doing this in wordpress, trying to create my own validation plugin.

It does show the error message 'This field cannot be empty' when I click submit but disappears straight away.

The below code is in the sks_email_form.php file

    1. function jquery_validation(){
    2. //echo 'Inside jquery_validation'; //works
    3. //echo plugins_url('/js/validation2.js', __FILE__); // correct 
    4. if(!is_admin()){
    5. ?><script type="text/javascript" src="<?=plugins_url();?>/Sks_email_form_plugin/js/validation2.js"></script>
    6. <script type="text/javascript">
    7. var $j = jQuery.noConflict();
    8. $j(document).ready(function(){
    9. $j('#sks_email_form').submit(function(){
    10. validation();
    11. });
    12. });
    13. </script>
    14. <?}
    15. }
    16. function sks_email_form(){
    17. echo "<form name='sks_email_form' id='sks_email_form' method='post' action=''>
    18. <table class='email_table'>
    19. <tr>
    20. <td>
    21. Full name:
    22. </td>  
    23. <td>
    24. <input type='text' name='fullname' id='fullname'/>
    25. </td>
    26. </tr>
    27. <tr>
    28. <td id='full_name_err_msg'>
    29. </td>
    30. </tr>
    31. <tr>
    32. <td>
    33. Email:
    34. </td>
    35. <td>
    36. <input type='text' name='email' id='email'/>
    37. </td>
    38. </tr>
    39. <tr>
    40. <td>
    41. Message:
    42. </td>
    43. <td>
    44. <textarea name='msg' id='msg' cols='35' rows='6'></textarea>
    45. </td>
    46. </tr>
    47. <td>
    48. <input type='submit' name='submit' id= 'submit' value='Submit'/>
    49. </td>
    50. <td>
    51. <input type='reset' name='reset' value='Reset'/>
    52. </td>
    53. </tr>
    54. </table>
    55. </form>";
    56. }

    In javascript file I have the following:

    1. var $j = jQuery.noConflict();
    2. (function($j){
    3. validation = function(){
    4. var full_name = $j('#fullname');
    5. var full_name_err_msg = $j('#full_name_err_msg');
    6. //alert('full_name: '+full_name.val()); // works
    7. is_field_empty(full_name, full_name_err_msg);
    8. }
    9. function is_field_empty(elem, elem_err_msg){
    10. //alert('inside is_field_empty function'); // works
    11. if(elem.val()==""){
    12. elem_err_msg.css('border', 'red solid 1px');
    13. elem_err_msg.html('This field cannot be empty');
    14. }
    15. }
    16. })(jQuery);

    Can anyone please tell me what I am doing wrong? 

    Thanks

    Alucard007