I want to help my users to add a correct string do be used as a meta_key in the database by not allowing illegal characters and replacing bad characters with good ones. I have this and its working great.
$('.custom_field_name').keyup(function () {
var v = this.value.replace(/\W/,'');
if (v!=this.value) this.value = v;
});
But i also want to replace space ' ' with a underline '_', and I have been trying codes like this, not getting anywhere.
$('.custom_field_name').keyup(function () {
var v = this.value.replace(/\W/,'') && (' ','_');
if (v!=this.value) this.value = v;
});
or
$('.custom_field_name').keyup(function () {
var v = this.value.replace(/\W/,'');
var v = this.value.replace(' ','_');
if (v!=this.value) this.value = v;
});