0

I am trying to implement validation with jquery. But do not know where I am doing wrong. Below is my code. Please help.

<script type="text/javascript">
$(document).ready(function() {
    alert("ready");
})
$("#myForm").validate({
    rules: {
        txt: {
            required: true,
            minLength: 2,
        }
    },
    messages: {
        txt: {
            required: "Enter your text",
            minLength: "At least 2 characters are necessary",
        }
    }
});
</script>
<form action="" id="myForm">
    <p>
        <input type="text" id="txt" placeholder="Input" />
    </p>
    <p>
        <input type="submit" id="sbmt" value="Submit Button" />
    </p>
</form>
3
  • Without validate.js? The function .validate() is present only there! Commented Jun 18, 2015 at 12:44
  • @PraveenKumar, I know first i was using validate.js file, then i removed because i am trying to implement validation without using that file. I am messed up here in my code i know. I just shared it. Can you please tell me that the way which i am trying, is it possible or not? If yes then how? Commented Jun 18, 2015 at 12:46
  • Have a look at mine! Commented Jun 18, 2015 at 13:11

2 Answers 2

1

Converting your script to manual, we get this:

$("#myForm").submit(function (e) {
  if ($(this).find("#rules").val().trim().length < 2) {
    alert("Rules length should be at least 2");
    return false;
  }
  if ($(this).find("#messages").val().trim().length < 2) {
    alert("Messages length should be at least 2");
    return false;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Use this code may be it helps you.

<script type="text/javascript">
$(document).ready(function() {
  $("#myForm").validate({
    rules: {
        txt: {
            required: true,
            minLength: 2,
        }
    },
    messages: {
        txt: {
            required: "Enter your text",
            minLength: "At least 2 characters are necessary",
        }
    }
  });
});
</script>

5 Comments

It wont work. Because I do not want to use validate.js file. And if i use .validate() then validate.js file is needed.
obvious validate.js needed .. otherwise you have to do it manualy
Manually? How? I want to know any way to make it happen but not using validate.js file.
and use jquery syntex instand of javascript syntex.
Syntex tank? In stand? What stand? :D

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.