JavaScript to jQuery conversion?

Hi guys,

I have these JavaScript codes below, please help me convert this into jQuery without using a plugin.


<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>

This is basic.
I hope some one will help.

Thanks in advance.

The codes above are just simple JavaScript validation.

Something like this maybe?

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Form example</title>
  </head>

  <body>
    <form id="myForm" action="demo_form.asp" method="post">
      <label for="fname">First name:</label>
      <input type="text" name="fname" id="fname">
      <input type="submit" value="Submit">
    </form>

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
      $("#myForm").on("submit", function(e){
        if($("#fname").val() === ""){
          alert("First name must be filled out");
          e.preventDefault();
        }
      });
    </script>
  </body>
</html>

Why spend time converting from JavaScript to JQuery given that JQUery is just a collection of JavaSript functions and so JQuery is still JavaScript.

@felgall ;
Because mixing them produces error.
So I need to find a solution that will not result into error.
Thus I need to experiment.
I’ll try using pure jQuery first perhaps the error will disappear.

I suspect that it may be more beneficial for us to focus on that which is resulting in your error.

What is it that you are you mixing it with?