How to allow only 3 digit and one dot in Regular Expression - angularjs

I want to do like this : 3.40 and not more than 3 characters and one dot:
<md-input-container class="md-block">
<label>Marks/CGPA</label>
<input type="text" name="education.cgpa" ng-model="education.cgpa"
ng-pattern="/^[0-9]{0,4}$/">
<div class="input-validation" ng-show="educationSaveForValidate['education.cgpa'].$error.pattern">
Insert valid CGPA
</div>
</md-input-container>
How can I allow only 3 digits and one dot in Regular Expression?

You may use a single regex like
ng-pattern="/^(?!.{5})\d*\.?\d+$/"
or - to allow an empty string:
ng-pattern="/^(?!.{5})\d*\.?\d*$/"
You may also move the length check out of the regex:
ng-pattern="/^\d*\.\d*$/" ng-maxlength="4"
Details
^ - start of string
(?!.{5}) - a negative lookahead that fails the match if there are any 5 chars in the input string
\d* - 0+ digits
\.? - an optional .
\d* - zero or more digits (if \d+ is used, then 1 or more digits)
$ - end of string.
To disallow any leading/trailing spaces, add ng-trim="false".

https://regex101.com/r/kF0hJ5/17
Check this link above, I hope it'll help you. Sorry for commenting link here. Doing so as I have less repo.

i have solved my problem this way......
ng-pattern="/^\d\.\d{0,2}$/"

Related

AngularJs input with required alphanumeric

So I have this form that has an id input field.
I want to restrict it to alpha numeric with no spaces so I set this up:
self.pattern = /^[a-z0-9]*$/;
Which is used in my view like this:
<input class="form-control" name="id" type="text" ng-model="controller.model.id" ng-disabled="controller.id" ng-pattern="/^[a-z0-9]*$/" ng-trim="" ng-maxlength="10" required />
But I want to modify my pattern to say that there must be at least 1 letter, but it can have 0 digits.
Can someone let me know how to do this?
You may use
ng-pattern="/^\d*[a-z][a-z\d]*$/"
Add i modifier if you need to also match uppercase letters:
ng-pattern="/^\d*[a-z][a-z\d]*$/i"
If you need to disallow leading/trailing whitespaces add ng-trim="false".
Pattern details
^ - start of string
\d* - 0+ digits
[a-z] - a letter
[a-z\d]* - 0 or more letters or digits
$ - end of string.

Valid regex expressions won't work with AngularJS

I'd like to check if user input is correct for phone numbers in two formats:
01 1234567 (two numbers, a space, seven numbers)
+12 123 123 123 123 (plus sign, two numbers, a space, three numbers, a space, three numbers, a space, three numbers
no character at all (no input)
I wrote a regex for this [0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7}|. It works when checked with online regex checkers, but it won't work (user can write whatever they want) when used with AngularJS: ng-pattern="[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7}|".
You need to define a regex that will match the whole string that matches your patterns as optional patterns:
ng-pattern="/^(?:\+[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{2} [0-9]{7})?$/"
^^ ^^
Or, a bit shorter:
ng-pattern="/^(?:\+[0-9]{2}(?: [0-9]{3}){3}|[0-9]{2} [0-9]{7})?$/"
If you define the pattern in a JS file as a variable use
var mypattern = /^(?:\+[0-9]{2}(?: [0-9]{3}){3}|[0-9]{2} [0-9]{7})?$/;
Note that when using regex delimiters the anchors are required for the regex to match entire input.
See the regex demo.
Details
^ - start of string
(?: - start of an optional non-capturing group:
\+ - a + char
[0-9]{2} [0-9]{3} [0-9]{3} [0-9]{3} (equal to [0-9]{2}(?: [0-9]{3}){3}) - 2 digits and then 3 occurrences of a space, 3 digits
| - or
[0-9]{2} [0-9]{7} - 2 digits, space, 7 digits
)? - end of the optional group
$ - end of string.

Regex to reject if all numbers and reject colon

I am trying for a regex to
reject if input is all numbers
accept alpha-neumeric
reject colon ':'
I tried ,
ng-pattern="/[^0-9]/" and
ng-pattern="/[^0-9] [^:]*$/"
for example ,
"Block1 Grand-street USA" must be accepted
"111132322" must be rejected
"Block 1 grand : " must be rejected
You may use
ng-pattern="/^(?!\d+$)[^:]+$/"
See the regex demo.
To only forbid a : at the end of the string, use
ng-pattern="/^(?!\d+$)(?:.*[^:])?$/"
See another regex demo
The pattern matches
^ - start of string
(?!\d+$) - no 1+ digits to the end of the string
[^:]+ - one or more chars other than :
(?:.*[^:])? - an optional non-capturing group that matches 1 or 0 occurrences of
.* - any 0+ chars other than line break chars, as many as possible
[^:] - any char other than : (if you do not want to match an empty string, replace the (?: and )?)
$ - end of string.
According to comments, you want to match any character but colon.
This should do the job:
ng-pattern="/^(?!\d+$)[^:]+$/"

Custom validation for input text to validate minimum 2 lowercase characters, 2 uppercase characters, 2 integers and 2 special symbols In Angular JS

<input ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9])/" name="text" type="text" ng-model="formData">
From this I can check minimum 1 lowercase character, 1 uppercase character, 1 integer and 1 special symbol.
But I want to validate 2 lowercase characters, dynamically minimum numbers will change. So I have to write common validation.
How can I do this?
Can anyone help me. Thanks in advance.
use this for 2 lowercase, 2 uppercase, 2 integer, 2 special character
(?=.*[a-z]{1,2})(?=.*[A-Z]{2,2})(?=.*\d{2,2})(?=.*[^a-zA-Z0-9]{2,2})
try it here

regex pattern repetition and ng-invalid

I have this pattern like so
<input name="ip" ng-pattern="/^http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]{4,5}/" class="form-control full-width text-right" type="text" ng-model="resource.ip"/>
The last part {4,5} is at least 4 and no more than 5. But when I type more than 5 integers at the end the form doesn't change to ng-invalid
This works on http://www.regexr.com/
Any idea why my input is allowing more than 5 characters?
Just add $ to define the match end.
^http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]{4,5}$
From the Reference > Archors > end:
Matches the end of the string, or the end of a line if the multiline flag (m) is enabled. This matches a position, not a character.

Resources