Aravind E
Aravind E

Reputation: 1061

How to check the given text doesnot contain full zero in ng-pattern?

i need to check the given the input text doesnot contain full zeors in ng-pattern. for eg my I/p is :00002210000 it should accept if my I/p is:0000000000 it should not accept it should throw an error.

Upvotes: 0

Views: 1047

Answers (4)

Ankit
Ankit

Reputation: 1510

Try this RegEx : ng-pattern="/^(?!0+$)\d{10}$/" using look-ahead.

Upvotes: 0

Fissio
Fissio

Reputation: 3758

Use javascript match().

if (!myString.match(/(0*[1-9]+0*)+/) {
    alert("Invalid string!");
};

The above requires the input to have at least one non-zero number. Here's a fiddle to demonstrate with ng-pattern as requested:

http://jsfiddle.net/HB7LU/15632/

<input ng-model="myText" ng-pattern="/^(0*[1-9]+0*)+$/" type="text" />

Upvotes: 2

Ved
Ved

Reputation: 12103

It is better to put regex in your controller $scope variable, and bind it inside ng-patter.SEE THIS

 $scope.regex = /([0]+[1-9]+[0]+)?$/;
    ng-pattern="regex";

OR,

ng-pattern="^([0]+[1-9]+[0]+)?$"

Upvotes: 1

Denis Thomas
Denis Thomas

Reputation: 1022

You could use a regEx pattern that checks if the input contains at least one number:

ng-pattern=".*[0-9].*"

RegEx is from Regular Expression For At Least One Number

Upvotes: 0

Related Questions