Regular expression for EDU emails with sub domains [duplicate] - javascript

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I am attempting to create a regular expression for validating edu emails which may or may not have a sub domain. Some schools have emails like "example#hms.harvard.edu" while other schools have emails like "example#stanford.edu".
([0-9]|[a-z]|[A-Z])+#([0-9]|[a-z]|[A-Z])+([0-9]|[a-z]|[A-Z])\.edu$"
This is the current regular expression that I have but I am not well versed in these.
I am looking to create an expression that will validate emails with one domain and emails with a subdomain.
Any help would be appreciated. Thanks!

In most cases, /^[-\w.]+#[-\w.]+\.edu$/ should be enough.
The only problem with this solution would be, that it can also accept domains like hms..harvard.edu.
To prevent this, you could use this regex instead: /^[-\w.]+#([-\w]+\.)*[-\w]+\.edu$/
Edit: use \w instead of [0-9a-zA-Z_]

This should do the trick (modified from http://emailregex.com/ - referenced in How to validate an email address using a regular expression?):
/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+edu))$/

Related

Are PDO prepared statements enough [duplicate]

This question already has answers here:
Are PDO prepared statements sufficient to prevent SQL injection?
(7 answers)
Closed 6 years ago.
From a security standpoint, are PDO prepared statements sufficient for preventing mysql related security issues? Or should I be character validating server side as well. Currently I am using pdo prepared statements and client side javascript form checking (but as I understand it the javascript can be disabled).
Kindest Regards,
Chris
There's actually three concerns here, each of which requires a different approach.
Data validation concerns verifying that all required form parameters are supplied, that they're an acceptable length and have the right sort of content. You need to do this yourself if you don't have an ORM you can trust.
Data escaping concerns inserting data into the database in a manner that avoids SQL injections. Prepared statements are a great tool to protect from this.
Data presentation concerns avoiding XSS issues where content you're displaying can be misinterpreted as scripts or HTML. You need to use htmlspecialchars at the very least here.
Note that all three of these are solved problems if you use a development framework. A good one to have a look at is Laravel since the documentation is thorough and gives you a taste for how this all comes together, but there are many others to choose from.
You could pass the data directly into your database, but what if the data the user submits is dodge, or maybe it's just invalid? They may submit a letter instead of a number, or the email address may contain an invalid character.
You can enhance your validation on the server side by using PHP's inbuilt Filters.
You can use these to both sanitize and validate your data.
Validation filters check that the data the user has provided is valid. For example, is the email valid? Is the number actually a number? Does the text match a certain regex?
Sanitization filters basically remove invalid characters for a given data type. Ie removing unsafe characters, removing invalid email/URL characters, removing non numeric characters.
There are a bunch of helper methods that can sanitize and validate single values, arrays and associative arrays, and the _GET and _POST arrays.
Nettuts has a few good tutorials on the matter here and here.

email address validation in JavaScript using regular expression [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 7 years ago.
I'm currently trying to validate email address using regular expression in JavaScript. These are the requirements of the Email address:
The email field contains a user name part follows by "#" and a domain name part.
The user name contains word characters including hyphen ("-") and period (".").
The domain name contains two to four parts of alphabet characters word extension.
Each word extension is separated by a period (".") and the last extension must have two to three characters.
Among four requirements, the third one is most confusing to me. I will be very appreciate if someone can help me. Thank you.
I have tried the first answer in this page, but this answer accept even 5 or more extensions, so it doesn't meet my third requirement.
For Javascript, here is the regex you need which follows the RFC 5322 standard:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*#([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
Side note: it is better to use a very weak regex (basically just matching the '#') and sending a confirmation email.

javascript method to check a valid URL [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I there any JavaScript way (regex) or module to check if a string is a URL which the follows following schema http://en.wikipedia.org/wiki/URI_scheme.
NOTE -
I have seen other questions related to this in stackoverflow and none of them are satisfactory.
Most of them are the regex which matches the http/https/ftp URL but what about feed or mailto URL or URL with any other schema as specified in http://en.wikipedia.org/wiki/URI_scheme
The following regex does what you are requesting.
It validates a string if it holds a URL as specified in RFC 3986.
Both absolute and relative URLs are supported.
^([a-z][a-z0-9+\-.]*:(//([a-z0-9\-._~%!$&'()*+,;=]+#)?([a-z0-9\-._~%]+|\[[a-f0-9:.]+\]|\[v[a-f0-9][a-z0-9\-._~%!$&'()*+,;=:]+\])(:[0-9]+)?(/[a-z0-9\-._~%!$&'()*+,;=:#]+)*/?|(/?[a-z0-9\-._~%!$&'()*+,;=:#]+(/[a-z0-9\-._~%!$&'()*+,;=:#]+)*/?)?)| ([a-z0-9\-._~%!$&'()*+,;=#]+(/[a-z0-9\-._~%!$&'()*+,;=:#]+)*/?|(/[a-z0-9\-._~%!$&'()*+,;=:#]+)+/?))(\?[a-z0-9\-._~%!$&'()*+,;=:#/?]*)?(\#[a-z0-9\-._~%!$&'()*+,;=:#/?]*)?$
I did not come up with this regex, it is created by JGSoft

Regex Comma Separated Emails

I am trying to get this Regex statement to work
^([_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})+(\s?[,]\s?|$))+$
for a string of comma separated emails in a textbox using jQuery('#textbox').val(); which passes the values into the Regex statement to find errors for a string like:
"test#test.com, test1#test.com,test2#test.com"
But for some reason it is returning an error. I tried running it through http://regexpal.com/ but i'm unsure ?
NB: This is just a basic client-side test. I validate emails via the MailClass on the server-side using .NET4.0 - so don't jump down my throat re-this. The aim here is to eliminate simple errors.
Escaped Version:
^([_a-z0-9-]+(\\.[_a-z0-9-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})+(\\s?[,]\\s?|$))+$
You can greatly simplify things by first splitting on commas, as Pablo said, then repeatedly applying the regex to validate each individual email. You can also then point out the one that's bad -- but there's a big caveat to that.
Take a look at the regex in the article Comparing E-mail Address Validating Regular Expressions. There's another even better regex that I couldn't find just now, but the point is a correct regex for checking email is incredibly complicated, because the rules for a valid email address as specified in the RFC are incredibly complicated.
In yours, this part (\.[a-z]{2,3})+ jumped out at me; the two-or-three-letters group {2,3} I often see as an attempt to validate the top-level domain, but (1) your regex allows one or more of these groups and (2) you will exclude valid email addresses from domains such as .info or .museum (Many sites reject my .us address because they thought only 3 letter domains were legal.)
My advice to reject seriously invalid addresses, while leaving the final validation to the server, is to allow basically (anything)#(anything).(anything) -- check only for an "at" and a "dot", and of course allow multiple dots.
EDIT: Example for "simple" regex
[^#]+#[^.]+(\.[^.]+)+
This matches
test#test.com
test1#test.com
test2#test.com
foo#bar.baz.co.uk
myname#modern.museum
And doesn't match foo#this....that
Note: Even this will reject some valid email addresses, because anything is allowed on the left of the # - even another # - if it's all escaped properly. But I've never seen that in 25 years of using email in Real Life.

Regex for valid URL (Javascript) (with a twist) [duplicate]

This question already has answers here:
Javascript regex to find a base URL
(2 answers)
Closed 6 years ago.
a simple regext that can test
Either but atleast one of (http, https, or www).
I've seen examples that has compulsion of protocol, or allows directly like google.com.
but for users, they are used to typing www., not all would type the protocol.
But still it should be a valid one, and not a "abced.com"
https://regex101.com/#javascript
there use (option1|option2|option3)
If you just need to test if the url starts with http, https, www then you can simply use /^(https?|www)/.
If you need a full regex for the url then check out Mathias Bynens post on the matter https://mathiasbynens.be/demo/url-regex and pick the one most suited to your needs.

Categories

Resources