Javascript Regex matching logical or - javascript

I am wondering if JavaScript includes the logical or when pattern matching. I would like to check if the user is either using #hotmail.com or #gmail.com, with anything allowed to come before it, and nothing allowed after the #hotmail or #gmail.com.
Instead of having to write two separate conditions to check each one is there away to combine it into one test. Thank you.

Try this regex:
/^(.+)#(hotmail|gmail)\.com$/

Related

Custom - Javascript regular expression to validate custom email addresses

I want it to be correct my JavaScript regex pattern to validate below email address scenarios
msekhar#yahoo.com
msekhar#cs.aau.edu
ms.sekhar#yahoo.com
ms_sekhar#yahoo.com
msekhar#cs2.aau.edu
msekhar#autobots.ai
msekhar#interior.homeland1.myanmar.mm
msekhar1922#yahoo.com
msekhar#21#autobots.com
\u001\u002#autobots.com
I have tried the following regex pattern but it's not validating all the above scenarios
/^[_a-z0-9]+(\.[_a-z0-9]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
Could any one please help me with this where am doing wrong?
The following regex should do:
^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#(([^<>()[\]\.,;:\s#\"]+\.)+[^<>()[\]\.,;:\s#\"]{2,})$
Test it: https://regex101.com/r/7gH0BR/2
EDIT: I have added all your test cases
I have always used this one but note it doesn't trigger on escaped unicode:
^([\w\d._\-#])+#([\w\d._\-#]+[.][\w\d._\-#]+)+$
You can see how it works here: https://regex101.com/r/caa7b2/4
First off [_a-z0-9]+ is going to match the username fields for the majority of those testcases. Anything further testing of username field content will result in a mismatch. If you write a pattern that expects two .-delimitered fields, it'll match when you provide two .-delimitered fields and only then, not anything else. Make a mental note of that. I think you probably meant to put the . in the first character set, and omit this part here: (.[_a-z0-9]+)...
As for the domain part of the email address, similar story there... if you're trying to match domains containing two labels (yahoo and com) against a pattern that expects three... it's going to fail because there's one less label, right? There are domain names that only contain one label which you might want to recognise as email addresses, too, like localhost...
You know, there is a point to where you can dig yourself down a very deep rabbit hole trying to parse email addresses, much to the effect of this question and answer sequence. If you're making this complex using regular expressions... I think maybe a better tool is a proper parser generator... otherwise, write the following:
A pattern that matches anything up until an # character
A pattern that matches the # character (this will help you learn how to avoid your .-related error)
A pattern that matches everything (this will help you understand your .-related error)
Combine the three above in the order presented.

Simple email regex, make sure user includes dot domain?

I don't mess around with Regex too much but have been able to get this one online. /.+#.+/. This will return true with both joe#joe and joe#joe.com. I want to make it so a user must supply a domain extension otherwise I want it to fail, I presume this is quite simple but I just can't figure it out. I've tried /.+#.+.\S/ but that didn't work. Any help would be great, thanks!
This will be used in both PHP and javascript. The current one works in both, the new will need to also.
Here is expression
/\w+#\w+\.\w{2,10}/
to allow more characters:
/[\w\-\._]+#[\w\-\._]+\.\w{2,10}/
The regex here works for me (from http://www.regextester.com/19 )
/^[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
As does this example of regex inside JavaScript from plnkr here: http://embed.plnkr.co/ZlbA1I2TsDBUmDb9o0gj/
Given that I don't know what the rest of your code is and you might really need this for both PHP and JavaScript I will suggest a different approach as I don't agree with the solution given in the accepted answer because it will match email addresses like -#-.aa, .#.-.aa, .#_.aa etc.
I'd suggest you use PHP's filter_var with the FILTER_VALIDATE_EMAIL filter which
validates e-mail addresses against the syntax in RFC 822, with the exceptions that comments and whitespace folding and dotless domain names are not supported.
and probably an additional AJAX call from JavaScript .
Regular Expressions matching all valid email address are not trivial at all and I'm not even sure you can rely on them for matching ALL valid email addresses.
For more information please take a look at Validate email address in JavaScript? and Using a regular expression to validate an email address
.+\#.+\.{1}.+
This is a simple regex and will match the required criteria
For a simple regex that will accept any domain extension of one character or longer, try: /.+#.+\..+/
For a 2 character domain extension or longer, try: /.+#.+\..{2,}/

Combining RegEx's

I have two regex's that I am trying to combine. One is email specific and the other checks certain special characters. I have arrived at this solution following much toying:
"^([-0-9a-zA-Z.+_]+#[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}|[\\w\\-ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŒœßØøÅåÆæÞþÐð _]){0,80}$"
It does seem to check what I need it to, but for instance the following is still returned valid: abc#foo it does not force a full email address.
Am I using the correct approach or is there a simpler way to structure this RegEx? I'm on a learning curve with regex so all advice appreciated.
Move the multiplier {0,80} inside the parenthesis:
"^([-0-9a-zA-Z.+_]+#[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}|[\\w\\-ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŒœßØøÅåÆæÞþÐð _]{0,80})$"
// here __^^^^^^^
Also [a-zA-Z]{2,4} is really poor to validate TLDs, have a look at IANA.
And me#localhost is a valid email address.

Same regex for client-side and server-side validation

I am trying to find out whether my client-side Javascript regex
/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
for email validation (I'm using it just to make sure that the email is formatted properly, not as a primary validation method) will work on the server side with PHP.
I am not sure whether I can use the same one even though both languages use Perl-based regex syntax. Thank you for your help.
You should be able to use the same syntax.
You should use
preg_match(String $pattern, String $email[, array $matches])
with your pattern. It puts all occurrences into the array $matches, if given.
It returns true if a match is found. For E-Mails in particular it's always a
better idea to use the functions of others, because for example "$#us" is a valid
email address
This regex will work nearly identically in both JavaScript and PHP. There are some minuscule differences, for example \s matches the "next line" control character U+0085 in PHP, but not in JavaScript, but they are unlikely to matter in this context (it's unusual anyway to allow newlines and tabs in email addresses - why not use a simple space instead of the generic whitespace shorthand \s).
If you have to do these kinds of comparisons/conversions regularly, I heartily recommend you taking a look at RegexBuddy which can convert regexes between flavors with a single click.

Call a function if a valid URL is found in a textarea

What would be the best way to immediately call a function (myFunction()) as soon as a valid URL has been typed into a textfield? I've googled around but I haven't found anything that helps. Using a regular expression would probably be best but I need one that recognizes all sorts of URLs:
http://google.tld, www.google.tld, http://www.google.tld
But still doesn't consider things like "index.php" to be a URL. Does anyone know about such an expression?
^((?:https?|ftp):\/\/)?([\w\.]+.)([a-z]{2,4})$
Supports ftp as well ;)
You might struggle a bit as there would be so many different possibilities. This one will match any url that is technically a valid http or https path (which includes any character in the path after the domain name, any number of subdomains, etc)
((http)s?(://))?[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*(/(.*))?
Of if you'd like to exclude intranets, you can force a tld using the following:
((http)s?(://))?[a-zA-Z0-9\-]+(\.[a-zA-Z0-9]+)*(\.[a-zA-Z0-9]{2,4})+(/(.*))?
this will match any URL that ends with something like '.com' or '.ch' (you have to maintain the list of valid TLDs)
^(http:\/\/)?([\w\.]+\.)((com)|(ch))$
with javascript the forwardslash doesn't need escaping and TLDs could be less strict e.g. just something with 2-4 characters.
^(https?://)?([\w\.]+\.)([a-z]{2,4})$
Considering a comment to this question by CanSpice the idea of allowing TLDs with different length is difficult to cover as the event trigger may fire too early. A time delay upon the onchange trigger may solve this kind of issues.
Precise requirements and pros/cons of each solution should be weighted.
example at rubular

Categories