Javascript Regex - not accepting space, but it should

advertisements

I just want my input to accept only letters and spaces because my input serves for name and this doesn't work I already tried to put some space in the regex. My regex is working but spaces isn't.

// without space
$('#name').on('input', function (e) {
    this.value = this.value.replace(/[^a-zA-Z.,ñÑ ]/g, '');
});

// with space but it didnt work
$('#name').on('input', function (e) {
    this.value = this.value.replace(/[^a-zA-Z .,ñÑ]/g, '');
});

// with space again but it fails work also
$('#name').on('input', function (e) {
    this.value = this.value.replace(/[^a-zA-Z. ,ñÑ]/g, '');
});


Use \s for single whitespace. Additionally \s+ for matching multiple whitespaces