Forbid whitespace character in account table

Good evening

I want to forbid whitespace charcter in account table.
Contrary to other tables, this on does not seem to accept regex validation.

I tired to set up a validation rule :
image

But this solution does not work.

Is there a better way to achieve this please ,
Thank you

1 Like

I’ve tried all the tricks I know but think it’s likely to be code based.
Tagging my good friend @StephenChapman who may have a code solution :man_technologist::australia:

2 Likes

Ahoy! @Francois I also tried field validation rules with regex and didn’t have much luck!
Below is the JavaScript method. When a white space is added to field_xx, it will automatically erase it on keyup. Hope that helps.

$(document).on('knack-view-render.any', function(event, scene) {
  // Prevent whitespace input in a text field with key 'field_xx
  $('#field_xx').on('input', function() {
    this.value = this.value.replace(/\s/g, '');
  });
});
3 Likes

Thank you Carl and Stephen

Too bad this feature is not natively available…
@StephenChapman I can not make it work… Do you please have an idea of what I am doing wrong ?

1:
image

2:
image

3:
image

Thanks a lot !

@Francois I agree, it is unfortunate that there’s no method of doing so natively.
The reason its not working for you example is because name fields are split into separate inputs, so you have to target those inputs differently.
Here is the updated code to cover any inputs in your name field (i.e. first, middle, last, etc), replace field_x with your name field:

$(document).on('knack-view-render.any', function(event, scene) {
  // Prevent whitespace input in a name field with key 'field_58'
  $('#kn-input-field_xx input').on('input', function() {
    this.value = this.value.replace(/\s/g, '');
  });
});

If you wanted to only target one of the fields (e.g. the first name), you can swap out the event handler to this:

$('#kn-input-field_60 #first').on('input', function() {
2 Likes

Thank you Stephen.

It works fine in the front interface input.
To improve my knowledge, what would be the code to make it work from the builder as well?

I don’t think its possible to add custom code to run with the builder.

1 Like

Just stepping in for a huge thanks for this!
Owe you a beer.

Bruno

2 Likes