简体   繁体   中英

Regular expression for text field

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.

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

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();})

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 (ie, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM