How to use jQuery to onsubmit and check if the value is 2 - 5 characters?

后端 未结 4 1891
走了就别回头了
走了就别回头了 2020-12-15 21:59

Let\'s say I have a text field:

Username: 

4条回答
  •  太阳男子
    2020-12-15 22:16

    I made a Fiddle

    You can use a statement as

    $(document).ready(function(){  
        $("#form").submit(function(e) {
           length = $("#username").val().length;
           if((length > 2 && length < 5)) {
               $("#output").html("correct, logging in");
           } else {
               $("#output").html("incorrect, must have 3 or 4 chars");
           }
           return (length > 2 && length < 5);
        });
    });
    

    And HTML

    username:

提交回复
热议问题