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