afeef afeef - 1 year ago 63
PHP Question

How to make first letter after hypen in string to be capital

If I have string like

then I need the output to be

I tried these scripts:

$form['surname']->render(array('onChange'=>"javascript:this.value = this.value.toLowerCase()", 'onBlur'=>"javascript:this.value=this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()})"));

<input type="text" name="surname" onchange="javascript: this.value = this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toUpperCase() })" onblur="javascript: this.value = this.value.replace(/\w\S*/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() })" id="surname">

On trying script this makes
but make string
stays as its is. The output after my script is
but I need

The problem is that I cannot make string first text capital. Any suggestion is most welcome.

Answer Source

There's no need for JS or PHP code here as you can do this in CSS alone using text-transform: capitalize:

span {
    text-transform: capitalize;

i need to type on textbox so onchange event would fire css trick don"t works

Yes it does, as it works for input elements too:

.capitalise {
  text-transform: capitalize;
<input type="text" class="capitalise" value="hybert-metal" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download