1

I want to every word start with capital letter and rest of are should be in small letters while entering in text field.

I don't know how to give regular expression.

KARASZI István
  • 30,900
  • 8
  • 101
  • 128
mohana rao
  • 433
  • 3
  • 13
  • possible duplicate of [Javascript - How to capitalize first letter of each word, like a 2-word city?](http://stackoverflow.com/questions/4878756/javascript-how-to-capitalize-first-letter-of-each-word-like-a-2-word-city) – Rory McCrossan May 23 '12 at 13:03
  • Means I wan "Adfsd Dfad Cadsf" like that – mohana rao May 23 '12 at 13:04
  • If you will enter capital letter it should not display. – mohana rao May 23 '12 at 13:05
  • Wait, do you want to do a replace or validation? If you want to validate (i.e., check user input) you want my answer; if you want to do a replace, accept the answer from @MaxArt. – apsillers May 23 '12 at 13:15
  • while I entering it will be done – mohana rao May 23 '12 at 13:18
  • That doesn't clarify anything, unfortunately. Do you want to *change the input* to conform to your format once it is entered, or do you want a *function that returns true or false* that tells whether your input matches your format? – apsillers May 23 '12 at 13:22
  • Thank you for continuous effort "Apsillers". I got the output for my side. Please leave it. – mohana rao May 23 '12 at 13:34

3 Answers3

4

Why not use css transform:capitalize, it would be easier? :D

Mobilpadde
  • 1,871
  • 3
  • 22
  • 29
2

Looks like a classic capitalization function.

input.value = input.value.replace(/\b[a-zA-Z]+/g,
    function(m) {return m.charAt(0).toUpperCase()+m.substring(1).toLowerCase();})
MaxArt
  • 22,200
  • 10
  • 82
  • 81
0

Assuming you want to validate some input, you should write a match for what you don't want and then negate it:

!( /\b[a-z]|\w[A-Z]/.test(input_string) )

This regex matches for lowercase letters at the start of a word or uppercase letters immediately after another letter (i.e., in the middle of a word). If you negate this result, as I have done here, you'll get the validation you want.

MDN has a good intro to Regular Expressions and the JavaScript RegExp object

apsillers
  • 112,806
  • 17
  • 235
  • 239