Email regex validation javascript should not contain example.com - javascript

I have requirement for email validation that the email should not be with a particular domain,
for example if the email is email#example.com it is invalid. That is the RegExp should exclude the domain example.com
I tried to use like to exclude a string like
^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$.?!example.com
It's not working
Please help me with a RegExp.

Use this regex
(?=.*#)(?!.*example).*
This matches, all emails, that does not come from domain example.com
I've made a function for you.
function isValidEmail(input, excludedDomain) {
alert(new RegExp("(?=.*#)(?!.*" + excludedDomain + ").*").test(input));
}
isValidEmail("hi#example.com", "example"); //false
isValidEmail("hi#hotmail.com", "example"); //true
Demo:http://jsfiddle.net/W3rLU/

The correct Regexp is something like this.
^\w+#(?!example\.com)[a-zA-Z_]+?\.[a-zA-Z]{2,3}$
Debuggex Demo
It means: after the #, check that is not followed by "example.com" and then check that is followed by a character and a two or three letter domain.
I also recommend you to change it a bit to allow domain names with numbers and dashes (e.g. myname#domain-123.com) and subdomains (myname#subdomain.domain.com) with this expression:
^\w+#(?!example\.com)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,6})$
Debuggex Demo

Related

URL validation using Javascript doesn't work well

I use the following validation for url:
jQuery.validator.addMethod("urlValidatorJS", function (value, element) {
var RegExp = (/^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{2,40}(:[0-9]{1,40})?(\/.*)?$/);
return this.optional(element) || RegExp.test(value);
}, "The url is incorrect");
For some reason the following invalid url is valid according the validation above:
http://www.example.com/ %20here.html (url with space).
How can I fix the validation?
(\/.*)?
You said the (optional) last thing in the URL is a slash followed by any number of any character.
Be more specific than . if you don't want to allow spaces.
At first: there already is a validation module from jQuery which you could use.
This would be your regex to match the URLs you mentioned (don't miss the i flag in the end!):
/^(https?:\/\/)(?:[A-Za-z0-9]+([\-\.][A-Za-z0-9]+)*\.)+[A-Za-z]{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/i
You were missing that there can also be other adresses like http://some.other.example.com. Also as mentioned in the other answer your identifier . was too greedy. I replaced it with \Sfor any non-whitespace character. You also had some unnecessary identifiers like {1} and your additional HTTP in the first part of your regex.
You can also use pages like regex101 to examine your regex and try it on different strings. It also explains each part of your regex so you can debug it better.

Rewrite regex to accept conditional terms

^([a-z0-9_\.-])+#[yahoo]{5}\.([com]{3}\.)?[com]{3}$
this currently matches xxxx#yahoo.com , how can I rewrite this to match some additional domains? for example, gmail.com and deadforce.com. I tried the following but it did not work, what am I doing wrong?
^([a-z0-9_\.-])+#[yahoo|gmail|deadforce]{5,9}\.([com]{3}\.)?[com]{3}$
Thank you in advance!
Your regex doesn't say what you think it says.
^([a-z0-9_\.-])+#[yahoo]{5}\.([com]{3}\.)?[com]{3}$
Says any characters a-z, 0-9, ., - one or more times.
That later part where you are trying match yahoo.com is incorrect. It says y, a, h, or o, any of those characters are allowed 5 times. Same with the com so aaaaa.ooo would be valid here. I'm not sure what the ([com]{3}\.)?[com]{3} was trying to say but I presume you wanted to check for .com.
See character classes documentation here, http://www.regular-expressions.info/charclass.html.
What you want is
^([a-z0-9_.\-])+#yahoo\.com$
or for more domains use grouping,
^([a-z0-9_.\-])+#(yahoo|gmail|deadforce)\.com$
You haven't stated what language you are using so a real demo can't be given.
Functional demo, https://jsfiddle.net/qa9x9hua/1/
Email validation is a notoriously difficult problem, and many people have failed quite horribly at trying to validate them themselves.
Filter var has a filter just for emails. Use that to check for email address validity. See http://php.net/manual/en/function.filter-var.php
if (filter_var('bob#example.com', FILTER_VALIDATE_EMAIL)) {
// Email is valid
}
There's probably no downside to doing the domain check the easy way. Just check for the domain strings in the email address. e.g.
if (
filter_var($email, FILTER_VALIDATE_EMAIL) &&
preg_match("/#(yahoo|gmail|deadforce)\.com/", $email)
) {
// Email is valid
}
In terms of your original regular expression, quite a lot of it was incorrect, which is why you were having trouble changing it.
regexper shows what you've created.
([a-z0-9_\.-])+ should be [a-z0-9_\.-]+ or ([a-z0-9_\.-]+)
The () are only capturing results in this section. If you want results move the brackets, if not remove them.
[yahoo]{5} should be yahoo
That's matching 5 characters that are one of y,a,h,o so it would match hayoo etc.
\.([com]{3}\.)?[com]{3} should be \.com
Dunno what this was trying to accomplish but you only wanted .com
Take a look at http:// www.regular-expressions.info /tutorial.html for a guide to regular expressions

Regexp javascript - url match with localhost

I'm trying to find a simple regexp for url validation, but not very good in regexing..
Currently I have such regexp: (/^https?:\/\/\w/).test(url)
So it's allowing to validate urls as http://localhost:8080 etc.
What I want to do is NOT to validate urls if they have some long special characters at the end like: http://dodo....... or http://dododo&&&&&
Could you help me?
How about this?
/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/
Will match: http://domain.com:port/path or just http://domain or http://domain:port
/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/
match URLs without path
Some explanations of regex blocks:
Domain: \w+(\.\w+)* to match text with dots: localhost or www.yahoo.com (could be as long as Path or Port section begins)
Port: (:[0-9]+)? to match or to not match a number starting with semicolon: :8000 (and it could be only one)
Path: \/?(\/[.\w]*)* to match any alphanums with slashes and dots: /user/images/0001.jpg (until the end of the line)
(path is very interesting part, now I did it to allow lone or adjacent dots, i.e. such expressions could be possible: /. or /./ or /.../ and etc. If you'd like to have dots in path like in domain section - without border or adjacent dots, then use \/?(\/\w+(.\w+)*)* regexp, similar to domain part.)
* UPDATED *
Also, if you would like to have (it is valid) - characters in your URL (or any other), you should simply expand character class for "URL text matching", i.e. \w+ should become [\-\w]+ and so on.
If you want to match ABCD then you may leave the start part..
For Example to match http://localhost:8080
' just write
/(localhost).
if you want to match specific thing then please focus the term that you want to search, not the starting and ending of sentence.
Regular expression is for searching the terms, until we have a rigid rule for the same. :)
i hope this will do..
It depends on how complex you need the Regex to be. A simple way would be to just accept words (and the port/domain):
^https?:\/\/\w+(:[0-9]*)?(\.\w+)?$
Remember you need to use the + character to match one or more characters.
Of course, there are far better & more complicated solutions out there.
^https?:\/\/localhost:[0-9]{1,5}\/([-a-zA-Z0-9()#:%_\+.~#?&\/=]*)
match:
https://localhost:65535/file-upload-svc/files/app?query=abc#next
not match:
https://localhost:775535/file-upload-svc/files/app?query=abc#next
explanation
it can only be used for localhost
it also check the value for port number since it should be less than 65535 but you probably need to add additional logic
You can use this. This will allow localhost and live domain as well.
^https?:\/\/\w+(\.\w+)*(:[0-9]+)?(\/.*)?$
I'm pretty late to the party but now you should consider validating your URL with the URL class. Avoid the headache of regex and rely on standard
let isValid;
try {
new URL(endpoint); // Will throw if URL is invalid
isValid = true;
} catch (err) {
isValid = false;
}
^https?:\/\/(localhost:([0-9]+\.)+[a-zA-Z0-9]{1,6})?$
Will match the following cases :
http://localhost:3100/api
http://localhost:3100/1
http://localhost:3100/AP
http://localhost:310
Will NOT match the following cases :
http://localhost:3100/
http://localhost:
http://localhost
http://localhost:31

Javascript regular expression for WebURL

I want regular expression in javascript that can validate any WebURL.
It should accept below formats:
google.com/...
www.google.com/...
http://google.com/...
https://google.com/...
I have tried lots of regular expressions for that.But no one is looking perfect.Below are some of the tried regular expressions:-
/(http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
/[a-z]+:\/\/(([a-z0-9][a-z0-9-]+\.)*[a-z][a-z]+|(0x[0-9A-F]+)|[0-9.]+)\/.*/
/(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
/^(((ht|f){1}((tp|tps):[/][/]){1}))[-a-zA-Z0-9#:%_\+.~#!?&//=]+$/
I want regular expression should take only 3 WWW.Not more than 3 and not less than 3 WWW.
Here there are two methods to validate the URL
^(https?|ftp|file)://.+$
^((https?|ftp)://|(www|ftp)\.)[a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?$
Try these...
What do you think about:
((http|https|)\://){0,1}([w]{3}.){0,1}[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}
Explanation
((http|https|)\://){0,1} for optional protocol (""or "http://" or "https://" will be ok)
([w]{3}.){0,1} for optional www string
[a-zA-Z0-9\-\.]+ for domain name
[a-zA-Z]{2,3} for domain suffix like: com, uk, biz, tv, etc.
This one would match all of your URLs:
(https?://)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?
Altough it's not possible, to check for exactly three "W" as there might be a subdomain. If you really need this check, I would use a second regex to test that.

Email regex in JS?

I have a regex that is supposed to match email addresses.
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
When I run the below code in my javascript, it returns null. Could it be an issue with my JS syntax, or is it an issue with the regex?
alert(emailString.match(regex));
This regular expression does not include lowercase letters.
Try this:
^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$
Most probably you've forgotten to set the case-insensitive option.
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
And of course, you're missing lots of valid addresses (.museum etc.)...
The problem with using regex to validate emails is even the expression that is the "standard" misses completely valid addresses. You would be far better off checking to see if it contains the # symbol and a . . Or to be really fancy you can poll the email address and if no response is given mark it as invalid, this of course comes with an overhead.
Posting a bit late but this regex works 100% across all email formats.
let rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
console.log(rEmail.test("john#gmail.com")); //true
console.log(rEmail.test("john#gmail.com123")); //false
console.log(rEmail.test("john.something#gmail.com")); //true
console.log(rEmail.test("john123#gmail.com")); //true
I assume you have not specified the case-insensitive modifier:
var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
// ^
Otherwise the expression only matches upper case letters.
You might want to see RFC 5322, in particular Section 3.4.1
You can use this one. it will support after [dot] 2 ,3 character as
per your domain
var email_filter = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (email_filter.test('yourEmail#gmail.com')) {
alert('Valid Email');
}

Categories