Ive been wracking my head trying to get Knockout validation to do for password with at least 1 letter and least 1 number. Heres the code I think came closes with but still rule always fails.
self.Password.extend({ pattern: { message: "Password must have at least one number and one letter " }, params: "/^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]*$/" });
s
self.Password = ko.observable().extend({
pattern: {
message: "Password must have at least one number and one letter",
params: /^(?=.*[0-9])(?=.*[a-zA-Z])[A-Za-z0-9]+$/
}
});
Related
I've a reactive forms validation for a password, and the pattern is the following:
new RegExp('^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#$^+=!*()#%&]).{8,50}$');
My goal is to validate the string so it is:
Between 8 & 50 characters
Has a lower case letter
Has an upper case letter
Has a number
And has a symbol
For some reason, it works like a charm, but if I enter a password that starts with a single number, the validation fails.
What am I doing wrong?
Example passwords:
1dD5a971# -- doesn't match
11dD5a971# -- does match
The angular code:
static PASSWORD_PATTERN = new RegExp('^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#$^+=!*()#%&]).{8,50}$');
this.form= fb.group({
user: [...],
password: ['', [Validators.compose([
Validators.required,
Validators.min(8),
Validators.max(50),
Validators.pattern(AddUserComponent.PASSWORD_PATTERN)
])]]
};
Thank you in advance.
I 'm not a regex expert but it may be much better if you make a multi pattern rather than just a complex pattern,the goal was for me to keep it simple as possible and you can have a different message base of the patterns.
this.form = fb.group({
password: [
"",
[
Validators.required,
Validators.minLength(8),
Validators.maxLength(50),
Validators.pattern(/[A-Z]/),
Validators.pattern(/[a-z]/),
Validators.pattern(/[0-9]/),
Validators.pattern(/[!##$]/),
]
]
});
demo 🚀
I'm using the Cognito Javscript SDK, and I've created a form where a user can register an account. If, for whatever reason, server-side validation fails, the response looks like this:
{
"__type":"InvalidParameterException",
"message":"4 validation errors detected: Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\S]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\p{L}\\p{M}\\p{S}\\p{N}\\p{P}]+"
}
The problem with this response is that I can't provide good user feedback because I'd have to parse the response to determine which fields need to be fixed. Is there a way to get the errors back in a format that is better for working with programmatically?
The best way to do this at the moment, is to programmatically grab the values by cutting out the substrings after 'value at'.
I haven't got an example of any library that might help in doing this but this is good feedback though.
Just in case anyone is stumbling on this ages later, the answer is to use response.code:
this.cognitoUser.forgotPassword({
onSuccess: (data) => {
},
onFailure: (data) => {
console.log(data)
/*
{
"__type":"InvalidParameterException",
"message":"4 validation errors etc"
}
*/
console.log(data.code)
/*
"InvalidParameterException"
*/
}
})
You have to first json stringify and then json parse the object to get values.
Try following sample code -
var errorObj = JSON.parse(JSON.stringify(data));
console.log(errorObj.statusCode);
I am trying to enforce strong(er) passwords in my Kohana application using Auth, by using the following regex to require at least one upper case letter, one lower case, one number, one non-alphanum (special character), and a minimum of 8 characters.
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$
The regex is working, as can be seen on Rubular. Here's the code I'm using in Kohana's Model_Auth_User, which extends ORM.
public function rules() {
return array(
'password' => array(
array('not_empty'),
array('min_length', array(':value', 8)),
array('regex', array(':value', '/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/'))
)
);
}
However, when creating a new user account, or changing the password of an existing one, this regex seems to be completely ignored. The min_length from the line above is working fine though!
It will stop me from using test as a password because it's less than 8 characters, but testing123 doesn't give any sort of error message.
Any ideas why this is happening and a way around it?
Figured it out - you have to add the regex to the get_password_validation function (in the same Model) or it doesn't output any error message.
public static function get_password_validation($values) {
return Validation::factory($values)
->rule('password', 'min_length', array(':value', 8))
->rule('password', 'regex', array(':value', '/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9]).{8,}$/'))
->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
}
If added, the regex in the rules() function needs to be removed or it's not possible to login as it runs the regex check on the hashed string, which doesn't contain any special characters.
Hope this helps someone.
I may have let a bug loose on our code that essentially ruined some of our db data.
Every now and again we come across a username that has " " in his email so we need to fix this.
How can I search for a value like "/"fubak#drooop.com"/" in one field using my mongo shell?
I have been trying all the combinations I can think of like:
db.users.find(username: /"/" .* /)
and
db.users.find(username: //.*//)
or
db.users.find(username: /"".*/)
But nothing seems to work.
new RegExp('^".*')
This finds all the records that start with ".
Thanks.
Try this:
db.users.find({ email: "(?:[^\\"]+|\\.)* })
This will match all documents which contains quotes in the email
Not to sure which one is you problem, so the solutions to both possible scenarios with data in MongoDB like this:
{ "email" : "\"/\"fubak#drooop.com\"/\"" }
{ "email" : "/fubak#drooop.com/" }
{ "email": "\"blablabla#blip.ca\"" }
First document match:
db.problem.find({ email: /^\"/ })
Second document match:
db.problem.find({ email: /^\// })
Third document match
db.problem.find({ email: /^\"/ })
Do not know what you mean otherwise by "having quotes" as that is not valid JSON and by extension is also invalid BSON.
I'm using http://bassistance.de/jquery-plugins/jquery-plugin-validation/ to validate my form. Unfortunately, there is no text only method. So I tried to write my own:
$(document).ready(function(){
$("#staffedit").validate(
{
rules: {
editDisplayName: {
textonly: true,
required: true
}
}}
);
jQuery.validator.addMethod(
"textonly",
function(value, element)
{console.log('textonly called.');
console.log(/[-.\w\s]/.test(value));
return this.optional(element) || /[-.\w\s]/.test(value);
},
jQuery.format("Please only enter letters, spaces, periods, or hyphens.")
);
});
The method gets called, since my console.log function does output to the console at the appropriate times. What I don't understand, is why I can still put $ or * in the input field. I can put anything in there, and it still gets evaluated as valid.
I used http://regexpal.com/ to test the regex, and it works fine there.
So, how do I get the plugin to only allow text, spaces, periods, and hyphens?
On a side note, I'd also like to allow characters with accents, like à, É, ā, or ô...
The problem was with how the test() function works. It returns true if it makes any match. That's what had me so confused. The regex's I was using were actually doing what I thought they should. See http://www.w3schools.com/jsref/jsref_regexp_test.asp
/[^-\.a-zA-Z\s]/.test("David"); //Returns false
/[^-\.a-zA-Z\s]/.test("Davi$ *d"); //Returns true
//without the ^
/[-\.a-zA-Z\s]/.test("Davi$ *d"); //Returns true
/[-\.a-zA-Z\s]/.test("David"); //Returns true
As you can see, that's not very helpful. So what I did was pull the test out of the return statement. Like this:
$(document).ready(function(){
$("#staffedit").validate(
{
rules: {
editDisplayName: {
textonly: true,
required: true
}
}}
);
jQuery.validator.addMethod(
"textonly",
function(value, element)
{
valid = false;
check = /[^-\.a-zA-Z\s\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02AE]/.test(value);
if(check==false)
valid = true;
return this.optional(element) || valid;
},
jQuery.format("Please only enter letters, spaces, periods, or hyphens.")
);
});
So I check to see if any of the characters I don't want exist, if they don't test() returns false, so it's valid.
I also figured out the unicode stuff. [\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02AE] matches a bunch of stuff, I used http://en.wikipedia.org/wiki/List_of_Unicode_characters to figure out what to put in. I think I got everything I wanted. Thanks to Kyle Schmidt for posting the link to Javascript + Unicode regexes that helped me figure out the \u syntax. I should probably check the unicode a bit more thoroughly, but that should be enough for now.
Problem solved.
If you get the additional methods js there's a letters validator in there
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.js
You want letterswithbasicpunc I believe
Putting the ^ inside a character class will negate what's in the character class. I'd go for [-.\w\s]