Refuse space only input with ng-required

165 Views Asked by At

Is there a simple way to make inputs and textareas with the ng-required attribute set to true invalid if it contains only spaces or blank characters ?

I'm looking for a generic way that would be applied to all my fields.

Note : I'm using angular 1.5.5

2

There are 2 best solutions below

0
Ellone On BEST ANSWER

Ok, so in the end I didn't manage to find a generic way. I had to change every template, but it can be done quickly using the replace option of any IDE.

I replaced every <textarea occurences with <textarea ng-pattern="/^(?=.*\S).+$/"

So, ng-pattern was my solution choice, there's probably a better way though.

1
WhiteDragon On

There is a way that you can set "ng-required=checkVar" and all input field you want to be validate in the same way can be put same class (class tag), e.x: class="check". Then you can bind an event handle function to all these fields as:

$('.check').on('click', function(e){
    do your work (validating)
    if (validated)
          $scope.checkVar = true;
    else
          $scope.checkVar = false;
}

This is my idea.