0

It's been a long time since I code stuff with validation. My problem today is that I am trying to validate a field in which it must have 10 or more than characters. When you press ok, the jQuery function should trigger.

function pathvalid() {
    var str = document.getElementById("pathenabler");
    var n = str.value.length;
    if (n < 10) {
        document.getElementById("demo").innerHTML = "The path that you have entered is either invalid or         incorrect. Please input a valid path.";
    }
    else {
        document.getElementById("demo").innerHTML = "Not Good day!";
        $('.actionenabler') .removeClass('chkbox');
    }
}
1
  • What is the problem? Commented May 18, 2015 at 13:58

2 Answers 2

2

Simply add this to your OK button

<input type="button" value="OK" onclick="pathvalid();" />

OR

You can write a code using jquery. Your input button

 <input type="button" value="OK" id="btn"/>

Your javascript contains:

$(document).ready(function(){
$("#btn").click(function(){
     if($("#pathenabler").text().trim().length < 10){
         $("#demo").append("The path that you have entered is either invalid or incorrect. Please input a valid path.");
      }
       else{
         $("#demo").append("Not Good day!");
         $('.actionenabler').removeClass('chkbox');
      }
});
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can get the inputs length with $('#pathenabler').val().length;.

If I get you right this example might fit your needs ;)

function check() {
  var length = $('#text').val().length;
  alert(length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="text" type="text">
<button id="check" onclick="check()">Check input length</button>

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.