Email Validating regex to include period in the hostname compulsorily - regex

I have the requirement to validate an email using the following conditions
If the email name (the part before the # sign) entered starts with a period (.) the system should display a message error. Example:.mail#mail.com.
If the email name has characters different than the ones in the ASCII-US table, the system should display an error message.
If the email name ends with a period, the system should display an error message. Example: mail.#mail.com.
If the email name is longer than 64 characters, the system should display an error message.
If the complete email address is longer than 254 characters, the system should display an error message.
If the hostname (part after # sign) does not starts with a letter or number (a-z and 0-9 characters), the system should display an error message.
If the host name should always have a period but shouldn't end with one.
If the host name is larger than 63 characters, the system should display an error message.
I am using this following regex which satisfies all of them except the (7) where there should be a 'period' compulsorily present in the hostname, it fails at emails like "abc#efg"
/^(?=.{1,254}$)(?=.{1,64}#)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
I need to keep this same regex as it satisfies all other conditions except the one i mentioned, what change can I make to satisfy that condition as well

After a few trial and errors I formulated the following regexp which takes care of all the above required criteria. Adding the answer here for anyone that stumbles across this in the future.
/^(?=.{1,254}$)(?=.{1,64}#)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/

Related

Error converting client characters into server’s?

I have textarea field where user copy/paste the text from word document. When user tried to save the form this error message was returned fromt he server:
Error converting characters into server's character set. Some character(s) could not be converted.
After looking carefully over the text that user entered in the field I found out which character is causing the issue. So when user copy/paste the text from the word document dash character is different/longer than standard dash. So the text looks like this:
Red – All devices muted/Do not disturb is enabled
I simply replaced the dash and text looked like this:
Red - All devices muted/Do not disturb is enabled
and I was able to save the form. I'm wondering what I can do to correct/prevent this kind of issues? I use ColdFusion 2018 on the back end in combination with Sybase database. If anyone have suggestions on how to fix this error please let me know.

Regex Expression to replace email address domain, for users email address

I am trying to solve an email domain co-existence problem with Exchange online. Basically i need it so when a message is sent to one tenant (domain.com) and forwarded to another tenant (newdomain.com) - that the To and/or CC headers are replaced with the endpoint (newdomain.com) email addresses before they are delivered to the final destination.
For Example:
1) Gmail (or any) user sends and email to sally.sue#domain.com, MX is looked up for that domain, it is delivered to the Office 365 Tenant for domain.com
2) That same office 365 tenant, is set to forward emails to sally.sue#newdomain.com (different tenant)
3) When the message arrives to sally sue at newdomain.com and she hits "Reply All" the original sender AND her (sally.sue#domain.com) are added to the To: line in the email.
The way to fix that is to use Header Replacement with Proofpoint, which as mentioned below works on a single users basis. The entire question below is me trying to get it to work using RegEx (As thats the only solution) for a large number of users.
I need to convert the following users email address:
username#domain.com to username#newdomain.com
This has to be done using ProofPoint which is a cloud hosted MTA. They have been able to provide some sort of an answer but its not working.
Proofpoint support has suggested using this:
Header Name : To
Find Value : domain\.com$
Replace : newdomain\.com$ or just newdomain.com
Neither of the above work. In both cases the values are just completely ignored.
This seems to find the values:
Header Name : To
Find Value : \b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
Replace : $1#fake.com
But the above simply and only replaces the To: line (in the email) with the literal string: $1#fake.com
I would also need to be able to find lowercase and numbers in email addresses as well. i believe the above example only finds caps.
I need it do the following:
Header Name : To
Find Value : \b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b (find users email address, domain)
Replace : user.name#newdomain.com
This is for a large number of users so there is no way to manually update or create separate rules for each user.
If i do create a individual rule, then it works as expected but as stated that requires manually typing out each user To: address And their new desired To: address.
This solution here almost worked: Regex to replace email address domains?
I have a couple of observations from general experience, although I have not worked with Office365 specifically.
First, a regex used for replacement usually needs to have a "capture group". This is often expressed with parentheses, as in:
match : \b([A-Z0-9._%-]+)#domain.com$
replacement : $1#newdomain.com
The idea is that the $1 in the replacement pattern is replaced with whatever was found within the () in the matching pattern.
Note that some regex engines use a different symbol for the replacement, so it might be \1#newdomain.com or some such. Note also that some regex engines need the parentheses escaped, so the matching pattern might be something like \b\([A-Z0-9._%-]+\)#domain.com$
Second, if you want to include - inside a "character class" set (that is, inside square brackets []), then the - should be first; otherwise it's ambiguous because - is also used for a range of characters. The regex engine in question might not care, but I suggest writing your matching pattern as:
\b([-A-Z0-9._%]+)#domain.com$
This way, the first - is unambiguously itself, because there is nothing before it to indicate the start of a range.
Third, for lowercase letters, it's easiest to just expand your character class set to include them, like so:
[-A-Za-z0-9._%]

Use Regular Expression inside URL for API call

I am calling one API that takes phone number as an input and provide response matching that phone number.
URL ends with this:
https://www.something.com/something&api_key=xxxx&sig=xxxx+"&%24filter=endswith(MobilePhone%2c'1234512345')
If in server mobile number is saved in same format as specified in url, that time it is giving me correct response.
But the problem is: If in server phone number is saved in different format like 12-3451-2345(with hyphen) or 12 3451 2345(with space), that time it is giving me empty response.
I tried using regular expression and wild card character in the above url, so that even if the hyphen and spaces are present in phone number in server, it should give me the correct response. But it didn't work. Please help me out with this.
Thanks in advance.

How to have multiple regex for same element

I have a text box which has a regular expression which is something like below
^AB[a-zA-Z0-9]{20}$
which basically allows charecter AB , followed by 20 either alphabetic or numbers, and for example lets consider the validation error for not following this regex is Some Test Error
I have a scenario where user enters AB1234 and tabs out of the text box, and the error Some Test Error shows, but I have a requirement of not showing the same error message Some Test Error if user is trying to follow the format but not adhering to the entire regex.
Scenario 1 :- User enters CD12345675438976524381
I need to show Some Test Error
Scenario 2 : USer enters AB12345
I need to shoe Different Test Error, because user tried to enter a value starting from AB*
How can achieve this, is there a way of specifying multiple regex's?
I am not sure which language you are using... but I suppose that you may change the regex, when user got the message once. While the user is trying to enter the entire string, don't count the number, unless the user input the 21st char or something not belong to [a-zA-Z0-9]...
I wish I made myself understood, the point is that I suppose you change the regex in time.
I think you can for example use multiple regexes and check the input:
if input is valid, everithing is ok,
if input is invalid check: a) if starts with AB (regex: ^AB) or if is valid length (regex ^([^A][^B][a-zA-Z0-9]{20})$) show proper info
if is totally invalid, give another info
OR you can use one long regex, like:
^(AB[a-zA-Z0-9]{20})$|^(AB[a-zA-Z0-9]{0,19}|AB[a-zA-Z0-9]{21,})$|^([^A][^B][a-zA-Z0-9]{20})$
DEMO
which capture given type of input in saparete groups,
and then find which groups was captured to check level of correctness:
if group 1 exist - valid string,
if group 2 - starts with AB but inproper length,
if group 3 - proper lenght, invalid beginning
I sure there are also other solutions.

regex validation for keyboard special character- grails

i am very new to this Grails.
I know there are ways in it to stop the input with specific character using constraints and matches for the field.
I am using it to stop the user from entering any special character from the keyboard
I have used
matches:/^[^$##*^%~]*$/
it checks that field does not have *^%$##~, and it works fine for this set of characters but I also want to restrict the user from adding +-(}/\|{[?]!<>~;',=&_.:" (in short all the special symbols on keyboard). And using only this constraints. I have tried putting them in this regular expression pattern but it is still allowing it or if does allow than it not showing in error message which were entered in the field.
For ex:- If I have entered (+)&^ than error message is shown only as "Please do not enter ^." but I want, "Please do not enter (+)&^."
Please let me know if anyone knows.
Please also note that I am required to use only Grails/Groovy support no JS/JQuery.
Thanks
Below regex will prevent from entering any character other than alphanumeric, and also at least one character. If you do not want minimum one character, then replace + with *
/^[^a-zA-Z0-9]+$/