Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

textarea控制字数的正则怎么写,要求允许换行

需求:textarea中允许输入各种字符,且可以换行,字数控制在500内

我写的正则是 /^.{0,500}$/

现在遇到的问题是用户回车后,他就报超过500字

在线等,多谢

<body>
<textarea name="" id="txt" cols="30" rows="10"></textarea>
<script>
    var re=/^.{0,500}$/;
    $('#txt').change(function(){
        if(!re.test($(this).val())){
            alert();
        }
    })

</script>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这种简单的字数统计,用不到正则。

console.log($('#txt').val().length);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...