2

I'm Trying to validate my form using Jquery validator, if the input name is in the array, it should return custom validator error message(Already exist), please see my below code, i have tried few, but I cant able to get how to achieve this.

<!DOCTYPE html>
<html>
<head>
    <title>Jquery Validator</title>
</head>
<body>
    <div>
        <form id="MyForm" action="." type="post">
            <input type="text" name="name">
            <button type="submit">Save</button>
        </form>
    </div>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/additional-methods.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var name_list = ["Danny", "Bucky", "James"]
            jQuery.validator.addMethod("alreadyexist", function(value, element) {
                .....
                .....
                .....
            }, "The Name is already Exist");

            $("#MyForm").validate({
                rules: {
                    "name": {
                        required: true,
                        alreadyexist: true,

                    },
                },
            });

        });
    </script>
</body>
</html>

I know that few lines of code to be added inside the addMethod function , but I'm not getting the idea. Can you guys please help me for this. It will be great-full for me. Thanks in advance

1 Answer 1

4

You can use indexOf() to check if the element exists in array and return true or false. indexOf() returns the index of the element in array if an element is found. See the working snippet:

$(document).ready(function() {
   var name_list = ["Danny", "Bucky", "James"]
   $.validator.addMethod("alreadyexist", function(value, element) {
     return name_list.indexOf(value) == -1;
   }, "The Name is already Exist");

   $("#MyForm").validate({
     rules: {
       "name": {
         required: true,
         alreadyexist: true
       }
     }
   });

 });
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/additional-methods.min.js"></script>
<div>
  <form id="MyForm" action="." type="post">
    <input type="text" name="name">
    <button type="submit">Save</button>
  </form>
</div>

You can read more about how indexOf() works here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.