SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    JavaScript to jQuery conversion?

    Hi guys,

    I have these JavaScript codes below, please help me convert this into jQuery without using a plugin.
    Code:
    <!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.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The codes above are just simple JavaScript validation.
    Quality codes are optimized and tested...
    Click here for inspiration..

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Something like this maybe?

    Code:
    <!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>

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why spend time converting from JavaScript to JQuery given that JQUery is just a collection of JavaSript functions and so JQuery is still JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @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.
    Quality codes are optimized and tested...
    Click here for inspiration..

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by solidcodes View Post
    Because mixing them produces error.
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •