Restrict duplicate words in email regular expression javascript/jquery [duplicate] - javascript

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I want to restrict duplicate words in an email using regex.
Currently I am using below regex that also restrict 2 consecutive dots (ex: a#gmail..com)
regexp: /A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*##(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z][{a-z},0-9](?:[a-z0-9-]*[a-z0-9]){1-2}?)$/i,
Now I want to also restrict duplicate words in email for example :-
abc.xyz#gmail.com.com
It should not accepted second .com .

What about:
email="hello.world.world";
email=email.split(".");
fail = email.some((ae,ai)=>email.some((be,bi)=>bi==ai?false:ae==be));
//loop trough all pairs, if they are at the same index skip ( we dont want to check email[0]==email[0]), else compare, if same fail.
if(fail){alert("Wrong email");}

Related

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

JS - Transform single email validation pattern into multiple email validation pattern. [duplicate]

This question already has answers here:
Javascript multiple email regexp validation
(7 answers)
Validate a comma separated email list
(5 answers)
How to write regex to verify a comma delimited list of values
(4 answers)
Closed 4 years ago.
I am struggling to create a Regex pattern that would validate multiple emails. I know that this topic has been widely discussed, however, having researched them all I could not find the answer to my specific question. My issue is as follows.
The project I am working on is written in PHP and uses FILTER_VALIDATE_EMAIL. I aimed at writing a front-end email validator to use for both single and multiple emails in a way, consistent with FILTER_VALIDATE_EMAIL. Here https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js I have found the ideal Regex pattern compliant with latest RFC standard provisions, namely:
^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}#)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126})+(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$
Now I've been trying to make this particular pattern function for multiple emails. Here comes my question, how can I get it done? I was trying to set
[\s*,]*
in order to make multiple addresses pass, by so far all I got is a headache, so your assistance is highly welcome, thanks in advance!
Try this..
var pattern = /^\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
if(!pattern.test(inputYour))
{
console.log('not a valid e-mail address');
}​
for multiple email..
var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
validate(email.trim());
});
validate function should have your above code...
This is for check in one go...
var email = 'test#example.com, hello#example.com,mail#example.com';
alert ( (/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#(([^<>()[\]\.,;:\s#\"]+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i.test(email)) );
link https://www.experts-exchange.com/questions/28940315/JavaScript-REGEX-validate-multiple-emails-addresses-separated-my-commas.html

How to Split string before '#' in angularJS [duplicate]

This question already has answers here:
How can I extract the user name from an email address using javascript?
(9 answers)
Closed 4 years ago.
I want to split an email of user before letter
#
in Javascript especialy in angularJS. for example if its
blabla#gla.com
it will turn into
blabla
can someone give me a simple example to make it?
because i must split it from API and store it as localstorage,
some of example that i find its use limitTo but can we use it to cut it in specific way from # until end?
Try this str.split()
var email = "blabla#gla.com";
var userName = email.split('#');
console.log(userName[0]);
Or use str.substring()
var userName = email.substring(0, email.indexOf('#'));
console.log(userName);

Developing a Regex pattern for an email [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 9 years ago.
I have no experience with regular expressions in java script, but I need to derive a pattern for
FMLast1234#ung.edu. There only needs to be a pattern for FMLast1234 because #ung.edu needs to remain the same. I am not sure if I just do this \b[a-z][a-z][a-z][0-9] or what.
Does there need to be a range for each character in the pattern? I need to check for variations of the pattern FMLast1234 not just a random assortment of characters and numbers.
/[a-zA-Z0-9]#ung.edu/.test("123#ung.edu") or
if(emailString.split('#')[1] === "ung.edu")
Edit : As per plalx comment here is my answer
/^\w+#ung.edu$/.test("aaa123#ung.edu")

regular expression for validating email addresses [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the best regular expression for validating email addresses?
I have the following expression
**replacePattern3 = /(\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
replacedText = replacedText.replace(replacePattern3, '$1');**
but it doesn't validate email if the email in this form a.b#alexmed.com it only validate email in form a#alexmed.com
i need another expression to validate the form a.b#alexmed.com
I think \w+ is the same as [a-zA-Z0-9], so it doesn't like the dot. I'm not good at this, but try
**replacePattern3 = /(\[a-zA-Z0-9\.]+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
You also might want to add a few more symbols to most places, because domain names can have numbers and certain symbols in them.

Categories

Resources