Click to See Complete Forum and Search --> : Javascrtipt email validation function
karen987
04-27-2007, 03:09 PM
Can someone show me what to add to validate an email in the validate function below. As you can see there are 2 fields, Name and Email, i have the email already setup to not allow spaces, but how to check that a valid email will be added?
thanks for any help.
if (String(emailfrm.name.value).replace(/ /g, "") == "") {
alert("Please enter name.")
document.emailfrm.name.focus()
return false }
if(document.emailfrm.name.value.length>20) {
alert("Maximum 20 characters allowed in 'name'.")
document.emailfrm.name.focus()
return false; }
if (String(emailfrm.email.value).replace(/ /g, "") == "") {
alert("Please enter email.")
document.emailfrm.email.focus()
return false }
gemguy
04-28-2007, 03:34 AM
hi,
it's very simple. just have a look at
http://www.hscripts.com/scripts/JavaScript/emailValidate.php
hope u enjoyed...
rgds
Gem:o Guy:cool:
karen987
04-28-2007, 03:51 AM
Thank you for the tip Gemguy,
I note in the link you gave me, they add a list of domain endings to check. I'm hesitant to use that, simply because domain endings are always changing new ones being added on, and if you use the code, then you anyone who uses an email not on the list, will be rejected.
Just something for you to think about too...anyhow i managed to rustle up the code below....i hope it's ok:)
var email = emailfrm.email.value;
var at = email.indexOf("@");
var dot = email.indexOf(".", at);
if (at<0 || dot<=at) {
alert("That doesn't appear to be a valid email address");
document.emailfrm.email.focus() ;
return false;
}
I would go the way of the RegEx, IM no particular fan but they are more reliable than just testing for certain characters, you can with use of a regex limit the input and keep the "Expected" intput to within your exacting parameters all in one string! and not several lines of complex code.
If you search this site & the web, you will find plenty of examples under regular expressions, form validation & field validation.
karen987
04-28-2007, 04:30 AM
Thank you \\, for your feedback, well i've come across weird nicknames but this takes the prize, what should i call you? doubleslash? :D
That said, i'm not html literate, so i think i'll stick to what is above, unless you see something wrong with it?
thanks for replying.
gemguy
04-28-2007, 07:39 AM
hi karen..
Very nice to hear from u as quickly. Ya! but just i came across that link & just forwarded... Also im thinking of what u had said. what doubleslash:confused: said is too nice to work on. It will make our work easy... Fine i expect any suggestion on the same...
Gem:p Guy;)
Thank you \\, for your feedback, well i've come across weird nicknames but this takes the prize, what should i call you? doubleslash? :D
That said, i'm not html literate, so i think i'll stick to what is above, unless you see something wrong with it?
thanks for replying.
lol. I wouldnt know how to pronounce it but it comes from "\\.\PhysicalDrive0" which on most computers is drive c:\ and also the boot drive.
Its your choice to what you want to use in your site but if you want to mimic a RegEx, your going to end up with one long function.
try here: http://www.codeproject.com/aspnet/Valid_Email_Addresses.asp if your stuck.