0

I'm like to use this function to replace accented characters on form

function remove_accents(text)
{       
    //text = text.toLowerCase();                                                         
    text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'a');
    text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'e');
    text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'i');
    text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'o');
    text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'u');
    text = text.replace(new RegExp('[Ç]','gi'), 'c');
    return text;                 
}

If i call an alert, all work as expected

alert (remove_accents('ãéíóú'));  // ok

Now, I need to apply this to all INPUT fields on a form, to replace specialcharacters with normal and I'm trying in this way, but with NO success.

$(document).ready(function(){
     $(":input" ).blur(function() {
          // alert ('working!');
          remove_accents();
     }); 
});   

What's wrong? Any help apprecited.

user1504222
  • 259
  • 1
  • 9

1 Answers1

1

You're not passing an argument to remove_accents() or using the return value. You need to pass the input's value, and then replace it back into the value.

$(document).ready(function() {
  $(":input").blur(function() {
    this.value = remove_accents(this.value);
  });
});

function remove_accents(text) {
  //text = text.toLowerCase();                                                         
  text = text.replace(new RegExp('[ÁÀÂÃ]', 'gi'), 'a');
  text = text.replace(new RegExp('[ÉÈÊ]', 'gi'), 'e');
  text = text.replace(new RegExp('[ÍÌÎ]', 'gi'), 'i');
  text = text.replace(new RegExp('[ÓÒÔÕ]', 'gi'), 'o');
  text = text.replace(new RegExp('[ÚÙÛ]', 'gi'), 'u');
  text = text.replace(new RegExp('[Ç]', 'gi'), 'c');
  return text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input>
Barmar
  • 596,455
  • 48
  • 393
  • 495