form fields validation with java issue

7 replies
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>
#fields #form #issue #java #validation
  • Profile picture of the author Nathan K
    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>
    {{ DiscussionBoard.errors[9861576].message }}
  • Profile picture of the author ferenan
    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
    {{ DiscussionBoard.errors[9861716].message }}
    • Profile picture of the author kingjpm
      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>
      Signature
      RogueDen.com
      {{ DiscussionBoard.errors[9862144].message }}
  • Profile picture of the author Nathan K
    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>
    {{ DiscussionBoard.errors[9862160].message }}
  • Profile picture of the author ferenan
    [DELETED]
    {{ DiscussionBoard.errors[9863951].message }}
    • Profile picture of the author ferenan
      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.
      {{ DiscussionBoard.errors[9885913].message }}
  • Profile picture of the author Nathan K
    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"
                }
            })
        })
    {{ DiscussionBoard.errors[9888450].message }}
    • Profile picture of the author ferenan
      Originally Posted by Nathan K View Post

      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"
                  }
              })
          })
      Thanks a lot ....I'll go with this.
      {{ DiscussionBoard.errors[9888754].message }}

Trending Topics