On this site, I got this regex
:
@"^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$"
Then I tried to adapt to my reality, my rule being: Only accept digit (number) in the input. I'm not that stuff with regex
, I have trouble with it. I did so:
"^\[1-9]\d{2}\[2-9]\d{4}\[0-9]\d{4}$"
And give me an error of:
Error: $ parse: lexerr Lexer Error
I opened the AngularJS page to find out what the error was and I got this:
Lexer Error: Unexpected next to column {1} in expression [{2}]. Description Occurs when an expression has a lexical error, for example a malformed number (0.5-) or an invalid unicode escape.
The error message contains more precise error.
To solve, learn more about Angular expressions, identify the error and fix the expression's syntax.
How do I improve my regex
, obeying my rule? Only receive number and if there are 11 digits, the third one has to be 9.
My html looks like this:
<label for="celular" class="col-sm-1 control-label">Celular</label>
<div class="col-sm-2">
<input id="celular" class="form-control input-sm" name="celular" type="tel" ng-model="vm.data.celular" ng-required="true"
ng-pattern="^\[1-9]\d{2}\[2-9]\d{4}\[0-9]\d{4}$" />
<div class="message" ng-messages="form.celular.$error" ng-show="form.celular.$touched">
<div ng-message="required">Por favor, preencha o campo celular!</div>
<!--<div ng-message="minlength">O campo celular deve ter no mínimo 11 caracteres.</div>
<div ng-message="maxlength">O campo celular deve ter no máximo 11 caracteres.</div>-->
<div ng-message="pattern">Formato de celular inválido Ex:11988887777</div>
</div>
</div>
Before I validated min and max, but it is not necessary with a regex
so, since the regex
itself already validates.
Even though I put the regex
posted by colleague Mariano, I type any number and the cell phone is validated. Here's how it is:
<label for="celular" class="col-sm-1 control-label">Celular</label>
<div class="col-sm-2">
<input id="celular" class="form-control input-sm" name="celular" type="tel" ng-model="vm.data.celular" ng-required="true"
ng-pattern="[1-9]\d9?\d{8}" />
<!--@"^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$"-->
<div class="message" ng-messages="form.celular.$error" ng-show="form.celular.$touched">
<div ng-message="required">Por favor, preencha o campo celular!</div>
<!--<div ng-message="minlength">O campo celular deve ter no mínimo 11 caracteres.</div>
<div ng-message="maxlength">O campo celular deve ter no máximo 11 caracteres.</div>-->
<div ng-message="pattern">Formato de celular inválido Ex:11988887777</div>
</div>
</div>
If I put it that way, it says invalid format for whatever format I type.
ng-pattern="/^[1-9]\d9?\d{11}$/"