form fields validation with java issue

by 7 replies
10
Hi ,
I need to validate the below form's name and email fields but can't do that because I do not understand any code of Java,can someone please show me how this would be possible.

much more appreciated if I can see the code here with the form and java.

Many Thanks

<form accept-charset="utf-8" action="http://www.example.com" enctype="multipart/form-data">

<input type="hidden" name="aid" id="aid" value="36"/>
<input type="HIDDEN" name="required" value="lemail,llname">
<label for="txtemail">Name:</label>
<input type="text" name="llname"/><br/>
<label for="txtemail">Email:</label>
<input type="text" name="lemail"/><br/>

<input name="Submit" type="submit" class="submit" value="Submit"/>
</form>
#programming #fields #form #issue #java #validation
  • Its not a good idea to only validate your form with javascript, you have to do it in the
    backend as well. So i have inlcuded the php scripts as well.

    For the javascript validation part you can use a plugin called jquery-validate to make
    things a little bit easy.


    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $name = trim($_POST['llname']);
    $email = trim($_POST['lemail']);
    $aid = trim($_POST['aid']);

    if(empty($aid) || empty($name) || empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL)){
    $status = 'Name and a valid Email is required';
    }else{
    $status = 'Thank You';
    }
    }

    ?>
    <html>
    <head>
    <title>Form</title>
    </head>
    <body>
    <?php if(isset($status)): ?>
    <p><?php echo $status; ?></p>
    <?php endif; ?>
    <form id="myForm" method="post" action="">
    <input type="hidden" name="aid" id="aid" value="36"/>

    <label for="llname">Name:</label>
    <input type="text" name="llname" id="llname" required><br/>

    <label for="lemail">Email:</label>
    <input type="text" name="lemail" id="lemail" required><br/>

    <input name="Submit" type="submit" class="submit" value="Submit"/>

    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
    <script>
    $(document).ready(function(){
    $('#myForm').validate({
    "rules" : {
    "llname" : {
    "minlength" : 6,
    "required" : true
    },
    "lemail" : {
    "required" : true
    }
    }
    })
    })
    </script>
    </body>
    </html>
    • [ 1 ] Thanks
  • Thanks Nathan k ,
    Actually The form has the validation on the back end side with PHP script ,but I just want to validate with Java before it goes to main php site.
    Do you mind if you can modified it without php?
    Thanks
    • [1] reply
    • the below is the JavaScript you are asking for
      just change this part to fit your need:

      "llname" : {
      "minlength" : 6,
      "required" : true
      },
      "lemail" : {
      "required" : true
      },



      Code:
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
          <script>
            $(document).ready(function(){
                $('#myForm').validate({
                  "rules" : {
                    "llname" : {
                      "minlength" : 6,
                      "required" : true
                    },
                    "lemail" : {
                      "required" : true
                    }
                  }
                })
            })
          </script>
      • [ 1 ] Thanks
  • kingjpm highlighted the javascript part.
    Here is the source without php.


    <html>
    <head>
    <title>Form</title>
    </head>
    <body>
    <form id="myForm" method="post" action="">
    <input type="hidden" name="aid" id="aid" value="36"/>

    <label for="llname">Name:</label>
    <input type="text" name="llname" id="llname" required><br/>

    <label for="lemail">Email:</label>
    <input type="text" name="lemail" id="lemail" required><br/>

    <input name="Submit" type="submit" class="submit" value="Submit"/>

    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
    <script>
    $(document).ready(function(){
    $('#myForm').validate({
    "rules" : {
    "llname" : {
    "minlength" : 6,
    "required" : true
    },
    "lemail" : {
    "required" : true
    }
    }
    })
    })
    </script>
    </body>
    </html>
    • [ 1 ] Thanks
  • [DELETED]
    • [1] reply
    • Is there any other method that I can be able to customize the alert error messages?
      with the above I cant put my own error messages ...or I am not sure it that would be possible?
      any comment appreciated.
  • Add 'messages' after rules
    Code:
    $(document).ready(function(){
            $('#myForm').validate({
                "rules" : {
                    "llname" : {
                        "minlength" : 6,
                        "required" : true
                    },
                    "lemail" : {
                        "required" : true
                    }
                },
                "messages" : {
                    "llname" : "Full name required",
                    "lemail" : "Email required"
                }
            })
        })
    • [ 1 ] Thanks
    • [1] reply
    • Thanks a lot ....I'll go with this.

Next Topics on Trending Feed