Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain this e-mail validation regular expression: [closed]

Tags:

javascript

I have this script uses regular expressions to check that a form field contains a valid email address.Please explain me from declare

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;

Thank you

Source:

<script type="text/javascript">

/***********************************************
* Email Validation script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
var returnval=emailfilter.test(e.value)
if (returnval==false){
alert("Please enter a valid email address.")
e.select()
}
return returnval
}

</script>

<form>
<input name="myemail" type="text" style="width: 270px"> <input type="submit" onClick="return checkmail(this.form.myemail)" value="Submit" />

</form>
like image 885
user737618 Avatar asked Jul 11 '11 06:07

user737618


People also ask

What is the regular expression for email validation?

To get a valid email id we use a regular expression /^[a-zA-Z0-9.! #$%&'*+/=? ^_`{|}~-]+@[a-zA-Z0-9-]+(?:\. [a-zA-Z0-9-]+)*$/.

How do you validate expressions in regex?

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false ( === false ), it's broken. Otherwise it's valid though it need not match anything.

How do I validate an email format?

A valid email address consists of an email prefix and an email domain, both in acceptable formats. The prefix appears to the left of the @ symbol. The domain appears to the right of the @ symbol. For example, in the address [email protected], "example" is the email prefix, and "mail.com" is the email domain.

What is the simplest regular expression for email validation 2?

+)$ is the simplest regular expression the checks the @ symbol only. It doesn't care about the characters before and after the '@' symbol. Let's take an example to understand how it validates the email address.


2 Answers

/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

/ = Begin an expression
^ = The matched string must begin here, and only begin here
\w = any word (letters, digits, underscores)
+ = match previous expression at least once, unlimited number of times
[] = match any character inside the brackets, but only match one
\+\. = match a literal + or .
\w = another word
- = match a literal -
* = match the previous expression zero or infinite times
@ = match a literal @ symbol
() = make everything inside the parentheses a group (and make them referencable)
[] = another character set
\w- = match any word or a literal -
+ = another 1 to infinity quantifier
\. = match another literal .
* = another 0 to infinity quantifier
\w+ = match a word at least once
[\w-]*\. = match a word or a dash at least zero times, followed by a literal .
() = another group
[a-z]{2,4} = match lowercase letters at least 2 times but no more than 4 times
| = "or" (does not match pipe)
\d+ = match at least 1 digit
$ = the end of the string
/ = end an expression
i = test the string in a case i nsensitive manner

Or you could try this awesome link. You know, whatever.

like image 184
rockerest Avatar answered Sep 30 '22 17:09

rockerest


var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

is Regulare expression to test email addres.

Read about regurlare expression on wiki :- http://en.wikipedia.org/wiki/Regular_expression

like image 43
Pranay Rana Avatar answered Sep 30 '22 17:09

Pranay Rana