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.

2
  • I have changed to $("input" ) but function in not applyed yet Commented Jun 4, 2020 at 23:47
  • I was wrong anyway and learned something new now. Sorry for that. ;) Commented Jun 4, 2020 at 23:52

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.