Problem with regular expression in asp.net with angularJS (1)

1

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&iacute;nimo 11 caracteres.</div>
                    <div ng-message="maxlength">O campo celular deve ter no m&aacute;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&iacute;nimo 11 caracteres.</div>
                    <div ng-message="maxlength">O campo celular deve ter no m&aacute;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}$/"

    
asked by anonymous 07.10.2016 / 18:01

2 answers

0

I solved it 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="text" ng-model="vm.data.celular" ng-required="true"
                       ng-pattern="/^[1-9]{2}\s?9\s?\d{8}$/" />
                <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="pattern">Formato de celular inválido Ex:11988887777</div>
                </div>
            </div>
    
10.10.2016 / 14:56
0
  

Only receive number and if there are 11 digits, the third must be 9.

The following regular expression will only accept 10 digits or 11 digits beginning with any digit except% with%. If there were 11 digits, the third one should be 0 .

ng-pattern="/[1-9]\d9?\d{8}/"
    
09.10.2016 / 09:56