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:
Update: the above had a few flaws so I ended up using this RegEx:
I
split()the string on that regex and use thelengthof the resulting array to get the correct word count.