0

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.

Aravind E
  • 1,031
  • 3
  • 15
  • 25

4 Answers4

2

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" />
Fissio
  • 3,748
  • 16
  • 31
  • I guess just `` would work there. – Fissio Jul 24 '15 at 11:32
  • Thanks Fissio. My problem statement is it can also accept zero, but my whole text should not have zeros. (i.e) if my input is 00000 it should throw error, if my input is 0000001 it should accept – Aravind E Jul 24 '15 at 11:39
  • Yeh, that's exactly what it does - as soon as you enter a non-zero digit it validates just fine. – Fissio Jul 24 '15 at 11:41
  • But i can enter zero, my number should contain atleast a single non-zero number. my input can be 10000,01200,00010,01000,00010 it should accepts the given input(max length is 30).if my input is 0000000000 it should throw error. – Aravind E Jul 24 '15 at 11:50
  • Yeah actually, I noticed another bug there - check updated fiddle for (hopefully) completely working example, http://jsfiddle.net/HB7LU/15632/ – Fissio Jul 24 '15 at 12:07
1

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]+)?$"
Community
  • 1
  • 1
Ved
  • 11,837
  • 5
  • 42
  • 60
  • Thanks Ved.. but it accepts all zeros if i give any number in between its throws an error. (i.e) if i enter my input as 000000 it should throw an error. if i enter my input as 0000001201 it should accept. – Aravind E Jul 24 '15 at 11:29
  • oh.. I missed it. I thought you just need all zero.. wait.. I am updating. – Ved Jul 24 '15 at 11:31
0

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

Community
  • 1
  • 1
Denis Thomas
  • 1,012
  • 1
  • 8
  • 17
0

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

Ankit
  • 1,471
  • 17
  • 29