使用自定义验证器的ASP.Net字数统计 [英] ASP.Net word count with a custom validator

查看:95
本文介绍了使用自定义验证器的ASP.Net字数统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的ASP.Net 2.0项目的要求将特定字段的最大字数限制为10个字(不是字符)。我目前正在使用带有以下ServerValidate方法的CustomValidator控件:

A requirement for an ASP.Net 2.0 project I'm working on limits a certain field to a max of 10 words (not characters). I'm currently using a CustomValidator control with the following ServerValidate method:

Protected Sub TenWordsTextBoxValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TenWordsTextBoxValidator.ServerValidate
    '' 10 words
    args.IsValid = args.Value.Split(" ").Length <= 10
End Sub

有人有没有更全面/准确的字数统计方法?

Does anyone have a more thorough/accurate method of getting a word count?

推荐答案

此正则表达式的效果很好:

This regex seems to be working great:

"^(\b\S+\b\s*){0,10}$"

更新:上面有一些缺陷,所以我最终使用了此RegEx:

Update: the above had a few flaws so I ended up using this RegEx:

[\s\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\xBF]+

split()该正则表达式上的字符串,并使用 length 结果数组以获取正确的工作d。

I split() the string on that regex and use the length of the resulting array to get the correct word count.

这篇关于使用自定义验证器的ASP.Net字数统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆