1

I am using following ng-pattern for validating the email

 "/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/" />

My code for email field is as below :

<input type="text" id="email" name="email" data-ng-model="userInfoDto.email" class="form-control" placeholder="Email" title="Email" data-ng-required="true  autofocus="autofocus" data-ng-blur="chcekUniqueEmail()" data-ng-pattern="/^(([^<>()[]\\.,;:\s@\"]+(\.[^<>()[]\\.,;:\s@\"]+)*)|(\".+\"))@‌​(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z‌​]{2,}))$/" /

but i am getting following error

  • Invalid location of text (]+(.[^) in tag ().
  • Start tag () not closed properly, expected '>'.

Please,any suggestion.

leppie
  • 115,091
  • 17
  • 196
  • 297

1 Answers1

1

If you want to validate email then use input with type="email" instead of type="text". AngularJS has email validation out of the box, so no need to use ng-pattern for this.

Here is the example from original documentation:

<script>
function Ctrl($scope) {
     $scope.text = 'me@example.com';
     $scope.email = /^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$/;
}
</script>
<form name="myForm" ng-controller="Ctrl">
    Email: <input type="email" name="input" ng-model="text" required>
    <span class="error" ng-show="myForm.input.$error.required"> Required!</span>
    <span class="error" ng-show="myForm.input.$error.email"> Not valid email!</span>
    <tt>text = {{text}}</tt><br/>
    <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
    <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
    <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
    <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
    <tt>myForm.$error.email = {{!!myForm.$error.email}}</tt><br/>
</form>

For more details read this doc: https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

Ganesh Ghalame
  • 6,367
  • 3
  • 24
  • 29
  • Hi Ganesh, thanks for reply but this default behaviour email field of AngularJS fails in following testcases 1).email@domain.com 2) email.@domain.com 3) email..email@domain.com 4) email@domain 5) email@domain.web 6)email@111.222.333.44444 so that I am using this pattern – Datta Desai Aug 19 '15 at 07:47
  • Hi Ganesh, thanks for reply but this default behaviour email field of AngularJS fails in following testcases 1).email@domain.com 2) email.@domain.com 3) email..email@domain.com 4) email@domain 5) email@domain.web 6)email@111.222.333.44444 so that I am using this pattern /^(([^<>()[]\\.,;:\s@\"]+(\.[^<>()[]\\.,;:\s@\"]+)*)|(\".+\"))@‌​(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z‌​]{2,}))$/ – Datta Desai Aug 19 '15 at 07:53
  • check script code of this https://docs.angularjs.org/guide/forms#modifying-built-in-validators – Ganesh Ghalame Aug 19 '15 at 08:54