0

I have looked through a lot of the other threads on here with this question. I am having issues capitalizing the first letter of an input.

http://jsfiddle.net/sA9c8/2/

I cannot seem to get the substr() function to work properly. It always returns the whole string with each word capitalized instead.

function crmForm_lastname_onblur(sourceField, sourceRowId) {
var x=document.getElementById("crmForm_lastname");
x.value=x.value.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.substr(0,1).toUpperCase()+letter.substr(1);
});

console.log(x.value);
}

Any help would be appreciated. I am no means a Javascript expert but I can understand what the code is doing in certain areas. Thank you.

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
AJL
  • 13
  • 3
  • First word? Your example capitalize first letter of each word. – Alex Feb 05 '14 at 18:16
  • 1
    That was my mistake Pinal I meant the first letter of the first word. Not the whole word in general. Adeneo solved this for me below. Sorry for any confusion. – AJL Feb 05 '14 at 18:28

2 Answers2

1

Are you trying to do this

function crmForm_lastname_onblur(sourceField, sourceRowId) {
    var val = sourceField.value;

    if (val.charAt(0).toUpperCase() != val.charAt(0)) 
         sourceField.value = val.charAt(0).toUpperCase() + val.slice(1)
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Is `slice(1)` faster than `substr(1)`? Is `charAt(0)` preferred over `[0]`? – crush Feb 05 '14 at 18:16
  • @crush - Have no idea, I just started from scratch, and used slice, as that's what I usually use to slice strings ? – adeneo Feb 05 '14 at 18:17
  • I feel like I remember it being faster. There's probably a question somewhere with an answer with benchmarks. – crush Feb 05 '14 at 18:18
  • I'm not sure I agree it's more readable :D but if it has better support, then that's a great reason. – crush Feb 05 '14 at 18:18
  • 1
    There something here about slice -> http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring-in-javascript – adeneo Feb 05 '14 at 18:19
  • And charAt here -> http://stackoverflow.com/questions/5943726/string-charatx-or-stringx – adeneo Feb 05 '14 at 18:20
  • That's a good point about not being able to set the char at position 0. I even tried doing that for this question at first before remembering that was a dumb idea, hah. – crush Feb 05 '14 at 18:21
  • Yes @adeneo! that is exactly what I was looking to accomplish! Thank you. You just saved me from going crazy. – AJL Feb 05 '14 at 18:26
0

Given that your Regex is only matching the first letter after a word boundary, your replacement function should be:

x.value = x.value.toLowerCase().replace(/\b[a-z]/g, function (letter) {
    return letter.toUpperCase();
});

to capitalize the first letter of each word (demo).

If you only want the very first letter in the input captialized, change your Regex and keep the same replacement function above:

x.value = x.value.toLowerCase().replace(/^[^A-Za-z]*\b[a-z]/, function (letter) {
    return letter.toUpperCase();
});

/^[^A-Za-z]*\b[a-z]/ captures the first letter of the input (demo).

pete
  • 24,141
  • 4
  • 37
  • 51
  • Hi Pete, thank you for this. The only shortfall of this is that it will force all lowercase on everything else. Adeneo's solution above works in the fact that I didn't take into consideration last names like 'Von Something' or 'Van RandomName'. His solution allows you to still change the case sensitivity of the remaining letters but force the first letter to be in caps. Thank you for this though as this will come in handy with other inputs I need to use on my project! – AJL Feb 05 '14 at 18:37
  • @AnthonyJude: The only reason it forces lowercase on everything else is because of the call to `toLowerCase` before the `replace`. If that's a concern, just remove the call to `toLowerCase`. – pete Feb 05 '14 at 18:45