1

I need to format the address with capitalized first letters and ", " before numbers. For example:

zur sandgrube 17  -> Zur Sandgrube, 17

I'm doing this in a way that check when the textboxes are updated. However it only changes with the first function that deletes spaces and updates capitalized characters.

I think that there is something wrong with the second function indf

$("input[type='text']").change(function(e) {
  switch (e.target.name) {
    case 'firstname':
    case 'lastname':
    case 'address2':
    case 'company':
    case 'city':
      formatta(e);
      break;
    case 'address1':
      formatta(e);
      indf(e);
      break;
    case 'dni':
      e.target.value = e.target.value.toUpperCase();
      break;
  }
});

function formatta(e) {
  //this work, capitalize and delete double spaces
}

function indf(e) {
  var tmp = $.trim(e.target.value);
  var ct = 0;
  for (x = 5; x < tmp.length; x++) {
    if (ct == 0) {
      if (tmp.charAt(x) == 0 || tmp.charAt(x) == 1 || tmp.charAt(x) == 2 || tmp.charAt(x) == 3 || tmp.charAt(x) == 4 || tmp.charAt(x) == 5 || tmp.charAt(x) == 6 || tmp.charAt(x) == 7 || tmp.charAt(x) == 8 || tmp.charAt(x) == 9) {
        ct = 1;
        tmp = tmp.substring(0, x - 1) + "," + tmp.substring(x, tmp.length);
      }

    }
  }
  e.target.value = tmp;
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
lummy
  • 11
  • 1

2 Answers2

3

You can achieve this by using a combination of several regular expressions. There's a handy function in this answer which will capitalise every word in a given string. Then you can use \s+ to find all repeated whitespace and \s\d+ to find the numerical values to prefix with a comma. Try this:

function formatta(s) {
  s = toTitleCase(s); // capitalise
  s = s.replace(/\s+/, ' '); // remove repeated whitespace
  s = s.replace(/\s(\d+)/, ', $1'); // add the comma before number
  return s;
}

function toTitleCase(str) {
  return str.replace(/\w\S*/g, function(txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  });
}

console.log(formatta('zur   sandgrube 17'));
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • `/\s(\d+)/` can be `/[^,]\s(\d+)/`. Otherwise you will end up with multiple commas. But this probably would be an edge case. – Rajesh Jul 12 '18 at 10:10
0

Split the name string into an array, using .split. Pass each portion to a function that capitalises the first letter and if isNan, returns the capitalised string else returns the comma in front of the number.

Build a new string out of the portions, replacing the space in front of the comma.

var origStr = "zur sandgrube 17";


var strPortions = origStr.split(' ');

var newString = '';

strPortions.forEach(function(portion){
  newString += capitalise(portion);
})
console.log(newString.replace(/ ,/,',')); // gives Zur Sandgrube, 17

function capitalise(str) {
  var firstLetter = str.charAt(0).toUpperCase();
  var newStr ='';
  isNaN(firstLetter)
   ? newStr = firstLetter + str.slice(1) + ' '
   : newStr =  ', ' + str;
   return newStr;
}
gavgrif
  • 15,194
  • 2
  • 25
  • 27