ng-pattern for only number with no decimal - angularjs

Need ng-pattern for restricting entry of decimal in input type number.I have used
ng-pattern="/^[0-9]$/"
to get the result but its not showing error on 1.0,2.0,3.0 etc.
Example:
2.0 = fail
2 = pass
3.0=fail
3=pass
<div class="col-md-5 col-md-offset-7">
<input type="number" min="0" class="form-control" id="equalizingFactor"
name="equalizingFactor" ng-model="flatVariable.equalizingFactor"
itle="Equalizing Factor" ng-maxlength="3" ng-pattern="/^[0-9]$/"
message="The field should contain only numbers with no decimal places"
ng-readonly="flatVariable.pgmInd30"/>
</div>
Should only have numbers no decimals included.

These should work fine:
ng-pattern="/^[0-9]*$/" // 0 or more numbers
ng-pattern="/^[0-9]+$/" // 1 or more numbers
ng-pattern="/^\d*$/" // 0 or more numbers
ng-pattern="/^\d+$/" // 1 or more numbers

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.

Input number with 2 decimal places and max length of 12

I have an input of type number, the value in this input will be saved in a column of type number(12,2), so I want to set the max length to 12 (12 digits in total,
2 of which are after the decimal point).
I tried as following :
<input type="number" md-maxlength="12" ng-pattern="/^[0-9]{0,10}([,.][0-9]{0,2})?$/" step="0.01" ..>
Whenver I type something it's invalid, this only occurs when I add the md-maxlength="12" which I use to display the max length under the input field.
As you can see in the picture the input is invalid.
How can I solve this ?
Edit:
ng-maxlength instead of md-maxlength resolves this issue but the x/12 label under the input which indicates the max length is not displayed anymore.
Edit 2:
The regex now indicates that only 10 number are allowed before decimal point (comma or dot) and 2 are optional after, you can see that the regex is working just fine here : https://regex101.com/r/fdDLFP/3, the problem is when I add the md-maxlength.

Issue in Password Stength : regex [duplicate]

This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 6 years ago.
Markup
<form ng-submit="doRegister(registerForm);" novalidate name="registerForm">
<input type="password" name="Password" ng-model="register.Password"
ng-pattern="/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/"/>
<span ng-show="registerForm.Password.$error.pattern" class="help-block">
min 1 lower char, 1 upper char, 1 digit and one special char
</span>
<button type="submit" ng-disabled="registerForm.$invalid" >
Submit
</button>
</form>
I am testing the Password Strength with min 1 lower char and 1 upper char and 1 digit and one special char
I am testing this string: a1A#s.com //Failed
I am testing this string:
12345aA! //Passed
Am I doing anything wrong?
It is failing because # is not part of your character class in last lookahead. You can also simplify your regex to this:
/^(?=.*[a-z])(?=.*[a-z])(?=.*[0-9])(?=.*[#!$#%]).{3,}$/
RegEx Demo

Display number always with 2 decimal places in <input>

I have a float value for the ng-model that I would like to always display with 2 decimal places in the <input>:
<input ng-model="myNumb" step ="0.01" type="number">
This works for most case when "myNumb" has decimal. But it will not force display of the 2 decimal places if "myNumb" has less than 2 decimal places (3.2), or an integer(30)
How can I force a display of 2 decimal place in the <input> field
AngularJS - Input number with 2 decimal places it could help...
Filtering:
Set the regular expression to validate the input using ng-pattern. Here I want to accept only numbers with a maximum of 2 decimal places and with a dot separator.
<input type="number" name="myDecimal" placeholder="Decimal" ng-model="myDecimal | number : 2" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" step="0.01" />
Reading forward this was pointed on the next answer ng-model="myDecimal | number : 2".
If you are using Angular 2 (apparently it also works for Angular 4 too), you can use the following to round to two decimal places{{ exampleNumber | number : '1.2-2' }}, as in:
<ion-input value="{{ exampleNumber | number : '1.2-2' }}"></ion-input>
BREAKDOWN
'1.2-2' means {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}:
A minimum of 1 digit will be shown before decimal point
It will show at least 2 digits after decimal point
But not more than 2 digits
Credit due here and here
{{value | number : fractionSize}}
like {{12.52311 | number : 2}}
so this will print 12.52
Simply use the number pipe like so :
{{ numberValue | number : '.2-2'}}
The pipe above works as follows :
Show at-least 1 integer digit before decimal point, set by default
Show not less 2 integer digits after the decimal point
Show not more than 2 integer digits after the decimal point
Did you try using the filter
<input ng-model='val | number: 2'>
https://docs.angularjs.org/api/ng/filter/number
Another shorthand to (#maudulus's answer) to remove {maxFractionDigits} since it's optional.
You can use {{numberExample | number : '1.2'}}
best way to Round off number to decimal places is that
a=parseFloat(Math.round(numbertobeRound*10^decimalplaces)/10^decimalplaces);
for Example ;
numbertobeRound=58.8965896589;
if you want 58.90
decimalplaces is 2
a=parseFloat(Math.round(58.8965896589*10^2)/10^2);
(Math.round(2.782061* 100 )/100).toFixed(2);
This will convert that into Two decimal places:
For Ex: 2.782061 ---> 2.78 two decimal Places
Use currency filter with empty symbol ($)
{{val | currency:''}}

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