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>
To get a valid email id we use a regular expression /^[a-zA-Z0-9.! #$%&'*+/=? ^_`{|}~-]+@[a-zA-Z0-9-]+(?:\. [a-zA-Z0-9-]+)*$/.
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.
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.
+)$ 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.
/^\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 expressioni = test the string in a case i nsensitive manner
Or you could try this awesome link. You know, whatever.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With