Problem with regular expression in Asp.net with Angularjs(1)

Asked

Viewed 110 times

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 which is: Only accept digit(number) in the input. I’m not there those things with regex, I have difficulty with it. I did so:

"^\[1-9]\d{2}\[2-9]\d{4}\[0-9]\d{4}$"

And gives me a mistake of:

Error: $parse:lexerr Lexer Error

I opened the Angularjs page to know what the error is and I got it:

Lexer Error: Unexpected next at column{1} in Expression [{2}]. Description Occurs when an Expression has a lexical error, for example the malformed number (0.5e-) or an invalid exhaust.

The error message contains a more precise error.

To resolve, Learn more about Angular Expressions, identify the error and fix the Expression’s syntax.

How I improve this mine regex, obeying my rule? Only receive number and if there are 11 digits, the third 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 thus, for the regex already validated.

Even though I put the regex posted by colleague Mariano, I type any number and the mobile is validated. See how you are:

<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 any format I type. ng-pattern="/^[1-9]\d9?\d{11}$/"

2 answers

0

I resolved so:

<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>

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 starting with any digit, except 0. If there were 11 digits, the third must be a 9.

ng-pattern="/[1-9]\d9?\d{8}/"
  • Mariano, I can’t load the error message to invalidate off-standard cell phones. I will edit and put the error messages in case the format is invalid.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.