Hi Bhavana,
Welcome to the InfoPath forums, It is possible double click on the text box and select the display tab and check "Limit text box to" and give 40 characters and in the Data tab click Rules -> Add -> Set condition and in the first drop down select "the expression" and in the corresponding text box type string-length(.) < 40 and click ok.
Now click "Add Action" and select "Show a dialog box expression" in the Action drop down and in the expression text box make this statement concat(40 - string-length(.), " Characters can enter"). and click ok 4 times. you will get the result.
And In InfoPath 2007 we can use JScript, for that
select Tools -> Form options -> Programming in that select JScript where "Form template code language" and click ok.
Hope this will help you.