Hi....... I need to validate the email address when submit the form using java script. When I enter one email address i have to validate the mail id. I unable to validate How can i validate the email address when i click on the form . Pls help me.. Regards, Subha
Hi Subha... This function will work for you. function checkEmail(myForm) { if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){ return (true) } alert("Invalid E-mail Address! Please re-enter.") return (false) } Code (markup): Thanks -Xak
the above function will work for most emails but it wont catch the weird ones that are valid in accordance to RFCs. also, it references form incorrectly (ms way) and a child of the form called email as well... Consider this: these are all valid email addresses! * "Abc\@def"@example.com * "Fred Bloggs"@example.com * "Joe\\Blow"@example.com * "Abc@def"@example.com * customer/department=shipping@example.com * $A12345@example.com * !def!xyz%abc@example.com * this regex that follows is a collection of chars and letters etc that try to conform to the RFCs, i have not tested it against all of the above but it should definitely work better than most for example, VBB's own regex for making email links only recognises the last one of the list we have - that's poor programming. var validEmailRegex = /^(([^<>()[\]\\.,;:\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,}))$/; var isValid = function(email) { return email.test(validEmailRegex); } if (!isValid("customer/department=shipping@example.com")) alert("invalid address!"); Code (markup): read this for the explanation behind what's valid and what is not: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
one more way check if length <5 position of @ is not < 2 at=email.indexof("@")<2 position of dot is not <4 dot=email.lastindexof(".")<4 dot-@ is not <2 dot-at<2 length-dot is not <2
Click the following link to see the validation script http://www.smartwebby.com/DHTML/email_validation.asp
WOW.. many words, no answer. String.prototype.isEmail = function() { return this.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/); }
perhaps you only read between the lines? let's take your 'answer' and put it to the test: <script type="text/javascript"> var data = [ '"Abc\@def"@example.com', '"Fred Bloggs"@example.com', '"Joe\\Blow"@example.com', '"Abc@def"@example.com', 'customer/department=shipping@example.com', '$A12345@example.com', '!def!xyz%abc@example.com', '_somename@example.com' ]; String.prototype.isEmail = function() { return this.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/); } var count = data.length; while (count--) { var el = data[count]; if (!el.isEmail()) document.write("INVALID: the email <strong>" + el + "</strong> incorrectly reported as non-valid<br />"); else document.write("OK: the email <strong>" + el + "</strong> recognised correctly<br />"); }; </script> PHP: and the outcome of this little test, to no surprise if i may add: OK: the email _somename@example.com recognised correctly INVALID: the email !def!xyz%abc@example.com incorrectly reported as non-valid INVALID: the email $A12345@example.com incorrectly reported as non-valid INVALID: the email customer/department=shipping@example.com incorrectly reported as non-valid INVALID: the email "Abc@def"@example.com incorrectly reported as non-valid INVALID: the email "Joe\Blow"@example.com incorrectly reported as non-valid INVALID: the email "Fred Bloggs"@example.com incorrectly reported as non-valid INVALID: the email "Abc@def"@example.com incorrectly reported as non-valid i dont like arguing but when we change it to use my regex: <script type="text/javascript"> var data = [ '"Abc\@def"@example.com', '"Fred Bloggs"@example.com', '"Joe\\Blow"@example.com', '"Abc@def"@example.com', 'customer/department=shipping@example.com', '$A12345@example.com', '!def!xyz%abc@example.com', '_somename@example.com' ], validEmailRegex = /^(([^<>()[\]\\.,;:\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,}))$/; String.prototype.isEmail = function() { return this.match(validEmailRegex); } var count = data.length; while (count--) { var el = data[count]; if (!el.isEmail()) document.write("INVALID: the email [b]" + el + "[/b] incorrectly reported as non-valid<br />"); else document.write("OK: the email [b]" + el + "[/b] recognised correctly<br />"); }; </script> PHP: the outcome becomes: OK: the email _somename@example.com recognised correctly OK: the email !def!xyz%abc@example.com recognised correctly OK: the email $A12345@example.com recognised correctly OK: the email customer/department=shipping@example.com recognised correctly OK: the email "Abc@def"@example.com recognised correctly OK: the email "Joe\Blow"@example.com recognised correctly OK: the email "Fred Bloggs"@example.com recognised correctly OK: the email "Abc@def"@example.com recognised correctly
Don't get me wrong but aren't u wasting your precious time with this kind of complex validation ? I do like what you are doing there, good job but i prefer to be more practical when dealing with this kind of validation. "Only letters, numbers, underscores, and one dot (.) are allowed" that's what some websites like Yahoo.com prevent you before creating a new email account.
well - no. when you are in commerce and need to marginalise your potential userbase, you are not allowed to do things like tell them: "sorry, i don't like / support your email / browser / os / resolution." ... not if you desire to be somewhat commercially successful anyway. and no - i am not wasting my time - the regex is already there, i just paste it in... the time wasting goes on here -- i'd consider it a success and less of a time-wasting exercise if at least 2-3 people see this, copy the regex and save it for later use... now, from a practicality point of view - i can see why an administrator would want to disallow anything that is deemed too complex - makes it difficult to support, narrow down etc. That's particularly scary, considering about 20% of people are not 100% certain of their email addresses anyway and are prone to making a mistake when entering it. Add a hook to a signup form which verifies email and then saves the 'bad' ones vs the 'good' ones via ajax into a DB - you just won't believe the amount of crap being typed. So yes, making it less complicated on the outside gives "natural selection" less of a chance - even dumb people can signup. I really hate restrictive programming that's done for no reason, like: not allowing spaces in usernames or passwords... why? etc etc...
I saved your regex for my personal use, so your works is good. Here is a good reason to think about. What if your customer is making a typo and insert a blank space (or a char from your above list ) ? isn't that a problem for your e-commerce website ? Not sure but i think there are more people making typos comparing with the people with weird emails.
oh, don't get me wrong - this regex is the tip of the iceberg. in ADDITION to the above, if it validates, the email string is sent via ajax to a PHP script which does ANOTHER SANITY CHECK. if that's also ok, it is being tested for an existing user account under that email. Then, it is exploded into userbits and hostbits. You verify the domain / and / or subdomain exists. If it does, you verify there are valid MX records for it. etc etc... sometimes, I have had to verify that port 25 on at least one of the hosts listed in the MX records is open. and you can also - once you open port 25, do an SMTP session like: helo localhost mail from:<test@test.com> rcpt to:<useremailaddress@theirhost> once you enter the rcpt line above, it may THEN reject it saying there is no such user, their mailbox is full or all sorts, useful data you can show the end user--especially if you need them to get a confirmation or registration / activation email from you. THAT's being thorough and an email verified. so - in view of the amount of checking the PHP script will have to do, the clientside 'wash' is only necessary to save some cpu time and bandwith anyway...
Hi XAK.... This function is very usefull.. its working.. and then how to use multiple email id validation script for this function... Pls help me.. Thanks Subha