weixin_33725270 2015-10-15 09:48 采纳率: 0%
浏览 32

输入框中的限制词

I have an input box named "title".

I want the user to only enter 35 words not more than that.

<input name="title" id="title" class="event-name gui-input" placeholder="Title" type="text">

<script type="text/javascript">
    /* <![CDATA[ */
    jQuery(function(){
        jQuery("#title").validate({
            expression: "if (VAL) return true; else return false;",
            message: "Please enter Title"
        });
    });
</script>
  • 写回答

4条回答 默认 最新

  • csdn产品小助手 2015-10-15 09:53
    关注

    Use the JavaScript split method:

    var content = $("input#title").val() //content is now the value of the text box
    var words = content.trim().split(/\s+/); //words is an array of words, split by space
    var num_words = words.length; //num_words is the number of words in the array
    
    if(num_words>35){
        //too many words
        return false;
    }
    

    Hopefully this helps and you can adapt this code to your validation.

    JSFiddle


    One-line solution to return the number of words in the input:

    var num_words = $("input#title").val().trim().split(/\s+/).length;
    

    JSFiddle

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效