I have two questions about my form , how do i prevent the form from sending empty non required fields (like if someone doesn't have a phone number, also how do i prevent the form from sending harmful characters to my server ?
I did some research and i think the answer may be in ISEMPTY but i don't know how to implement it.

This is my form validation
$("#myForm").on('valid', function(e){      e.preventDefault();      
      var name = $("input#name").val();
      var email = $("input#email").val();
      var phone = $("input#phone").val();
      var message = $("textarea#message").val();
      var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone + '&message=' + message;
        data: dataString,
        success: function(){
          $(".contactform").html("<div id='thanks'></div>");
          $('#thanks').html("<h1 class='text-center form_submit_text'>Thanks</h1>")
           .append("<h2 class='text-center form_submit_text'>Hi " + name + ", we will contact you soon </p>")
    return false;

This is my mail
PHP Code:
<?php$name $_POST["name"];$email $_POST["email"];$phone $_POST["phone"];$message $_POST["message"];$msg "Name:$nameEmail:$emailPhone:$phoneComment:$message";function checkInput($msg) {        $msg = @strip_tags($msg);        $msg = @stripslashes($msg);        $invalid_characters = array("$""%""#""<"">""|");        $msg str_replace($invalid_characters""$msg);        return $msg;    }$to "rebecca.charlotte.adams@gmail.com";$subject "The Brass Teacher Mail";$message $msg;$headers "Contact form enquiry";mail($to,$subject,$message,$headers);?>