I'd like to validate the password before the form is submit using Regex, Id like to know what whats the equivalent regex for the following PasswordValidator settings:
UserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true,
};
im using the AngujarJS pattern validation.
Example:
<input type="password" class="form-control" name="inputPassword" ng-minlength="6" np-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*(_|[^\w])).+$/" ng-model="userData.Password" />
<p class="alert-danger" ng-show="userDetails.inputPassword.$error.pattern">The Password is invalid</p>
But it simply ignore it.
Thank you very much!
Update 1
with the help of accepted answer i come to the following regex ^(?=.[a-z])(?=.[A-Z])(?=.*(_|[^\w])).+$ and added the length validation as ng-minlength="6" for some reason the pattern validation doesnt work on IE9 where the validation prevent the user save, rendering it totally useless.
Update 2
By suggested by stribizhev, changing the any symbol regex to (?=.*[\W_]) fixed IE9 validation, so it works 100% in IE9, final code is
<input type="password" class="form-control" name="inputPassword" ng-minlength="6" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).+$/" ng-model="userData.Password" />
<p class="alert-danger" ng-show="userDetails.inputPassword.$error.pattern || userDetails.inputPassword.$error.minlength">The Password is invalid</p>
thank you all very much
^(?=.*[!##$&*])(?=.*[a-z])(?=.*[A-Z]).{6,}$
I found that answer on SO which will give you hint how to construct your own regex https://stackoverflow.com/a/5142164/2892208
Related
I'm making an html5 form and only want users to sign up with .edu or .gov email addresses. What would the regex look like for that? I know it must be checked serverside as well, but one step at a time.
Example:
<input type="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}$" />
Building on How to validate an email address in JavaScript? I came up with this using a simplified RFC 2822 compatible pattern:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9]\.)+(edu|gov)
Regex Demo
The major difference is how the domain part is evaluated. I took this from my previous answer.
<form action="javascript:console.log('ok')">
<input type="text" name="domain"
pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9]\.)+(edu|gov)" title="Sorry, only email addresses from .edu and .org domains are allowed.">
<input type="submit">
</form>
Add this:
^[a-zA-Z0-9_.+-]+#(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(domain|domain2)\.(edu|gov)$
Demo
i am using KeyFilter Module of primeng here is my code :
<input type="text" pInputText [(ngModel)]="price.TintCost" [pKeyFilter]="patternDecimal" name="tintCost" required="true" />
here is my typescrip code :
patternDecimal: RegExp = /^[0-9]+(\.[0-9]{1,2})?$/;
and here is version of primeng :|
"primeng": "^5.2.0-rc.1",
i tested in regex then i can type dot(.) but when i apply to KeyFilter, it doesn't allow the dot(.). Someone help me, please
I solved this problem by adding a mask as default
KeyFilter.DEFAULT_MASKS['currencyRegex'] = /^-?(?:0|[1-9]\d{0,2}(?:,?\d{3})*)(?:\.\d+)?$/;
I solved this problem by change the pValidateOnly property to true.
The problem is that the KeyFilter check any press on keyboard and if the complete value is no the correct, then dont permit write, just if you copy and paste the value.
In the documentation say
Instead of blocking a single keypress, the alternative validation mode
which is enabled with pValidateOnly property validates the whole input
with a built-in Angular validator.
https://www.primefaces.org/primeng-6.1.6/#/keyfilter
Example that work for me.
Component.ts
public twoDecimal: RegExp = /^\s*-?(\d+(\.\d{1,2})?|\.\d{1,2})\s*$/
Component.html
<input name="decimalField"
#decimalField="ngModel"
[pKeyFilter]="twoDecimal"
[pValidateOnly]="true"
[(ngModel)]="item.decimalField"
type="text" pInputText>
<div *ngIf="!decimalField.valid" class="alert alert-danger">
<p>Incorrect format.</p>
</div>
The answer of #Norberto Quesada is correct.
Without pValidateOnly the regex will validate on every key stroke.
Let's say you want to enter the value "47.11":
You begin to enter "4" => this would be valid, no input blocked.
Same for "47"
As soon as you enter "47. => validation fails, input blocked.
I was thinking maybe it's possible to enter "4711" first and then the "." in between but for some reason this doesn't seem to work, too... Maybe this is a bug?
Anyways, you can take a look at this stackblitz example for better understanding.
I've also prepared an example of using ValidateOnly and in addition to that restrict the input to only numbers using keyDown event
I have a plunkr here. How can I get the Submit button to be disabled till the dob is in mm/dd/yyyy pattern?
https://plnkr.co/edit/GtPDxw?p=preview
Here's the form
<form [formGroup]="flashyForm">
<input formControlName="dob" pattern="^\d{2}\/\d{2}\/\d{4}$" placeholder="Date of Birth">
<button type="submit" [disabled]="!flashyForm.valid">Submit</button>
</form>
You can try pattern="^(0[1-9]|1[0-2])/(0[1-9]|1[0-2])/([0-9]{4})$".
Customize year as per your need.
Pattern needs to be a string like
pattern="\d{2}\/\d{2}\/\d{4}"
^ and $ are added automatically. I don't remember if \ need to be escaped as \\, but I guess so.
See also https://github.com/angular/angular/issues/10150
In my html 5 page, I want to force the user to add in the end of his login the ".app" string.
Do you know how I can make that ?
My current code:
<input type="text" name="user_login" placeholder="firstname.app" required style="width:92%;" pattern="/.app/">
Thanks you for your help.
EDIT
I have resolved my problem with a jQuery check and a html 5 pattern
JS:
// If the user login doesn't finish by .app, we add them
if(!$('input[name=user_login]').val().match('[.]app$'))
$('input[name=user_login]').val($('input[name=user_login]').val() + '.app');
HTML5:
<input type="text" name="user_login"
placeholder="firstname.app" required style="width:92%;"
pattern=".*\.app">
There are a few differences between regex in JavaScript and in HTML attributes :
you don't have to put your regex between /, contrary to js regex literals
the start and end are implicit, so here you should add .* to match the start
You also forgot to escape the dot.
All in one, you probably want this :
<input type="text" name="user_login"
placeholder="firstname.app" required style="width:92%;"
pattern=".*\.app">
Demonstration
try this:
if($('input[name="user_login"]').val().indexOf(".app") >= 0){
//code
}
or pattern:
<input type="text" name="user_login" placeholder="firstname.app" required style="width:92%;" pattern="\w*.app">
Here is demo
You could do it by specifying the specific pattern:
<input type="text"
name="user_login"
placeholder="firstname.app"
required style="width:92%;"
pattern="[A-Za-z-0-9]+\.app">
The email validation given in jquery validate results in internet explorer 8 browser hanging when we add a long mail id like this
A#ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXyz
return this.optional(element) || /^[a-zA-Z0-9]+([_.-]?[a-zA-Z0-9]+)#[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)(\.[a-zA-Z]{2,4})+$/i.test(value);
what could be the possible changes made to avoid this
<input type = "email" name="email" />
This would work