Yup validation, 2 regex on one string - reactjs

I have this schema that validates 2 password fields based on several requirements :
"Password must have 8 or more characters, at least one uppercase letter, and one number."
"Password cannot contain special characters other than _ # . -"
Right now i am showing both validation errors in one string, but i need to show 2 separate errors for these conditions.
The question is, how do i create 2 regex that don't conflict with each other when validating and will show me the necessary error?
const format = /[a-z`!#$%^&*()+=\[\]{};':"\\|,<>\/?~]/;
const passwordFormat = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d_#.-]{8,}$/;
return yup.object().shape({
password: yup
.string()
.required("Password is required")
// validates the password
.matches(passwordFormat, "Password must have 8 or more characters, at least one uppercase letter, and one number.")
//checks if there are special characters
.test('special-chars', "Password cannot contain special characters other than _ # . -", function(value) {
return format.test(value);
})
.strict(true)
});

You can try these two:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\S]{8,}$ - "Password must have 8 or more characters, at least one uppercase letter, and one number"
^[-#\.\w]*$ - "Password cannot contain special characters other than _ # . -"

Related

How to put space in the name of a slash command is nextcord?

#nextcord.slash_command(name = 'avatar', description = f'🎴 Display your avatar or a specific user avatar.')
async def get_user_avatar(self, ctx, user: nextcord.Member = None)
I want to have a slash command with a name like this: " user avatar " with a space between this words.
You can't, slash_command names needs to be lowercase, between 1 and 32 characters long, and only consist of these symbols: a-z, 0-9, -, _, and other languages'/scripts' symbols.
Your best option would be to change it to: user_avatar.

How to validate one field against another with Yup?

I'm setting up a form with two fields; start month and end month.
Start month and end month are just represented by integers (0-11) for the corresponding months. I need validation to ensure that end month comes after start month (ie the integer for end month is bigger).
I've looked at other similar issues from a few years back, but yup seems to have updated to render them useless. I've tried the below code with a few variations.
I am also having difficulty validating end month as a number (.number() - I'm assuming I have to maybe to do this within the test function.
let schema = yup.object().shape({
startMonth: yup
.number()
.required()
.positive()
.integer(),
endMonth: Yup.string().test(
"End Month Validation",
"error message",
value => {
return value > startMonth;
}
)
.number()
.required()
.positive()
.integer(),
});
Errors:
Line 102: 'startMonth' is not defined no-undef
Another approach would be to make use of .ref() and .moreThan() to perform this validation logic.
Something like the following should achieve what you require:
let schema = Yup.object().shape({
startMonth: Yup
.number()
.required()
.positive()
.integer(),
endMonth: Yup.number() /* Remove .string() */
.required()
.positive()
/* Reference startMonth field in validating endMonth value */
.moreThan(Yup.ref('startMonth'), "End month must come after start month")
.integer(),
});
schema.validate({ startMonth : 1, endMonth : 2 }) // Okay!
schema.validate({ startMonth : 11, endMonth : 2 }) // Throws exception
Hope that helps!

How to validate user input of currency

So I currently developing a website that support many languages. I have an input box where user can input the amount of currency inside. I need a function to validate if that input is legit or not.
however, because different countries use different format of number.
for example: England use '.' for decimal and ',' for thousand separator .
Where as Germany use ',' for decimal and '.' for thousand separator.
French use ',' for decimal and (space) for thousand separator...
And for Chinese/Jap , they even dont use number "1-9" to describe number
I can make a very big if-else function to do the validate base on the language they are using. something like this
number = userinput()
if "de":
return deValidator(number)
if "fr":
return frValidator(number)
if "en":
return enValidator(number)
if "zh":
return zhValidator(number)
However, is there any wiser way to do it?? what I am looking for is something like a already-built validator/library or an easier approach to solve this problem without having to writing different validator for different language
You can leverage on toLocaleString() method to help to build a validator; The toLocaleString() method returns a string with a language sensitive representation of the number.
const number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// → 123.456,789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// → 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// → 一二三,四五六.七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789
With this method, you can also format numbers with currency information:
const number = 10000000;
number.toLocaleString('it-IT', {style: 'currency', currency: 'EUR'})
// → 10.000.000,00 €
number.toLocaleString('it-IT', {style: 'currency', currency: 'USD'})
// → 10.000.000,00 US$
number.toLocaleString('en-US', {style: 'currency', currency: 'EUR'})
// → €10,000,000.00
number.toLocaleString('en-US', {style: 'currency', currency: 'USD'})
// → $10,000,000.00
For more details: toLocaleString https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Need to write a regex to parse the command

Need to write a regex to get 3 groups from strings-
<whatever text including new lines optional -group 1>/command <text until \n or </p> is encountered- group 2><whatever text including new lines optional -group 3>
what I tried is-
Pattern pattern1 = Pattern.compile('(.*?)[/]command (.*?)\n?(.*?)');
It should give the following output for string-
some\nthing/command cmdtext/nasdfjaklsdjf\nfgskdlkfg\ndgsdfgsdfgsdfg
group 1 - some\nthing
group 2 - cmdtext
group 3 - asdfjaklsdjf\nfgskdlkfg\ndgsdfgsdfgsdfg
What I am not getting is how to get the occurrence of </p> and .* is not considering the group. Although this is working for me-
String a = '\na\na\n\n\n\n\n\naaa';
Pattern pattern2 = Pattern.compile('\n(?s:.)*');
Matcher mchr = GiphyPattern.matcher(a);
system.assert (mchr.matches());
This regular expression should match what you need:
'([\\s\\S]*)/command (.*?)(?:\n|</p>)([\\s\\S]*)'
You cannot match \n with .* So I am using \\s\\S instead (which is actually \s\S but with Apex escaped backslashes).

How to search for :) in Solr

How does one search for specific punctuation in Solr, such as :)? I have tried URL encoding the text but I still get this message:
org.apache.solr.search.SyntaxError: Cannot parse ':': Encountered " ":" ": "" at line 1, column 0.
Was expecting one of:
<NOT> ...
"+" ...
"-" ...
<BAREOPER> ...
"(" ...
"*" ...
<QUOTED> ...
<TERM> ...
<PREFIXTERM> ...
<WILDTERM> ...
<REGEXPTERM> ...
"[" ...
"{" ...
<LPARAMS> ...
<NUMBER> ...
<TERM> ...
"*" ...
Additionally, I need to perform this search on a text field, not on a string field. How should I configure the analyser to save punctuation?
Note that searching google for the subject is impossible due to two prolific Solr contributors with the name "Smiley"!
What configurations you have for the text field?
You should take care the splitting is not happening on the puntuations e.g. if using StandardTokenizerFactory or word delimiter filter.
You can define a custom field with WhitespaceTokenizerFactory or KeywordTokenizerFactory and have further filters like lower case on it.
Also, There are some characters which Solr/Lucene uses for some operation e.g. + - ! ( ) { } [ ] ^ " ~ * ? :
You would need to escape the special characters with backslash. Check Escape Special Characters
instead of :) search for "\:\ )" , both chars :,) have special meaning in SOLR.
for all special operatos you need to escape by prefixing with '\' char .

Resources