Replicate C# Server Side Validation in Javascript

I basically have the following validation on my page - its a word rule in that a description in a text box cannot be greater than 3 words excluding the word 'and'. I have implemented the following server side validation in C# which is working fine

if (Desc.Trim().ToString() != "")
    MatchCollection collection = Regex.Matches(Desc.Replace("and", ""), @"[\S]+");

    if (collection.Count > 3)
        ErrorMsg.Append("Description should contain at most 3 words(excluding 'and').");

However I am having difficulty getting the same working in Javascript. I have tried the following but it isnt working so far so hoping for someone that has a better knowledge of Javascript can see the error. Note the if is part of a bigger validate function that fires on the page - the alerts were just there to see if it got into this if (which it doesnt) - when this is block is removed the rest of the JS on the page is working fine.

if (Desc.val().trim() != "")

    if (Desc.val().match(regexWordRule).length > 3)
        errorText += "Description should contain at most 3 words(excluding 'and').";

    valid = false;

and the below is my regexWordRule defined at the very top of the js file.

var regexWordRule = /[\S]+/;


You could find a better solution, but this approach came to my mind, so I am posting it:

var input = "and lorem and ipsum";

// remove ands
var deandizedinput = input.replace(/\band\b/g, ' ');

// replace all white spaces with a single space
var normalizedinput = deandizedinput.replace(/\s+/g, ' ');

// split the input and count words
var wordcount = normalizedinput.trim().split(' ').length;

Fiddle here.

Need Your Help

Sample HTML code for a complex form control (see mockup)

html forms jquery-ui user-interface

I have tried searching for this but I do not know the name of the control or UI so I am not having any luck. I'd like to know if there is a name for this UI and if you know of a good link to sampl...

AJAX Mouse Over Image, Display HTML Content

javascript html ajax image mouseover

I'm trying to use AJAX to display html content when mousing over an image. My code seems good but doesn't seem to work. deakin-campus / discover-deakin are the html pages I'm trying to display.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.