text box validation word count (asp.net)


I have a text box assigned as nvarchar(2000) in the database but i really want  it to be to a max 300 words.
I tryed using maxlegth in <asp:Textbox ID="txtN_Hero" runat="server" Width="600px" MaxLength="300"
and this didn't work. Is there any other validation to check for a max wording in this textbox? I can't change the database field because i have data on the database and it might shorten them out once i change it.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael FowlerSolutions ConsultantCommented:
The MaxLength property is for the number of characters allowed

To limit the number of words, first set the MaxLength to 2000 to ensure compliance with the database and then use validation to ensure the max number of words is not exceeded
Rafiq J.Net DeveloperCommented:
int  Length =  textbox.text.length;

if(length <300 )
perform your operation....
Prakash SamariyaIT ProfessionalCommented:
You have to write javascript function like below and call  javascript function in TextArea/Textbox KeyUp event
<script>function trim_words(theString, numWords) {
    expString = theString.split(/\s+/,numWords); //\s include space, tab, new line
    theNewString=expString.join(" ");
    return theNewString;

Open in new window

F IgorDeveloperCommented:
In CodeBehind try adding "onblur" event during page load, checking number of words:

Example in C#
protected void Page_Load(object sender, EventArgs e)
            txtN_Hero.Attributes.Add("onblur","if (this.value.split(/\s+/).length>300){ alert('Num words are > 300' ; this.focus(); }");

Open in new window

txtN_Hero.Attributes("onBlur")  ="if (this.value.split(/\s+/).length>300){ alert('Num words are > 300' ; this.focus(); }"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.