PDA

View Full Version : form validation


alaios
10-15-2002, 08:18 PM
<script language="Javascript">
function checkit()
{
var check=/[A-Za-z0-9]\w{2,}@[A-Za-z0-9-]{3,}\.\[A-za-z]{3}/
var giaelegxo=document.form2.textfield.value;
if (check.test(giaelegxo))
{
return true;
}
else
{
alert('wrong email);
return false;
}


}
</script>




<form name="form2" id="form2" method="post" action="formmail.php" onSubmit="return checkit()">
<input type="hidden" name="recipient" value="info@evridikifans.gr">
<input type="hidden" name="subject" value="parapona">
<input type="hidden" name="redirect" value="http://www.evridikifans.gr/thanku.htm">
<input type="text" name="textfield" id="textfield" size="15" maxlength="50">
<input type="submit" name="Submit" value="Αποστολή" >
</form>


I want to check with the function if the email adress is valid!!
please check the code because it does not work!!thx

mordred
10-15-2002, 09:52 PM
You need to improve your descriptions of errors and expected behaviour of your script. "it does not work" leaves all the work of detecting what did not work for you to those who try to help you. Please be more specific the next time.

Anyway, I noticed two issues here:

1.
alert('wrong email);

You missed a single quote to end the string.


2.
Your regular expression to test the validity of an email address is not good enough. I won't propose the silver bullet of email validation regexp now, but here's a quick hack that should ensure a better validation routine. If you have specific questions why I did rewrite certain parts of the expression, feel free to ask. :)


var check = /^[a-z0-9][a-z0-9.\-]*@[a-z0-9][a-z0-9.\-]{3,}\.[a-z]{2,}$/i;

beetle
10-15-2002, 11:26 PM
Are you wanting to get good validation? Or are you wanting to write the script for yourself (practice/experience or whatnot?)

Anyhow...for what it's worth, if you DO need validation...I got your hookup right ---> here (http://www.peterbailey.net/fValidate) <---

mordred
10-15-2002, 11:36 PM
But your email validation moans about email@foobar.museum - which is a valid email address by now?

beetle
10-16-2002, 12:07 AM
Ya, I am going to update the email validation with my next version :D

whammy
10-16-2002, 01:01 AM
Just a comment... I see no point in validating every aspect of an email address unless you are going to do it by receiving a response from the email address in question (not to mention there is not ONE reference I've found to a regular expression that validates against EVERY valid email address and actually WORKS... nor have I seen a good reference on every aspect of a "valid" email address that doesn't fail for real ones (tested in real life) in some respect...)

Regular Expressions are beautiful things, but in the case of emails and validation, they are only really useful in making sure the user hasn't made an obvious typo... i.e. I can use asdf@asdf.com and it will ALWAYS validate against any email regex. Or another example... just use myname@whateverdomain.com - it will always validate.

That's why I just stick to a simple one like:

^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}$

...and make sure that the email address entered was real by sending an email to that address and requiring a response.

:D

beetle
10-16-2002, 02:10 AM
Aye, neither have I. Actually, when I made fValidate...I didn't even write the email regex that I'm using. I got a couple online tested them with a variety of addresses. The one I ended up using (from webreference.com) is a good mix of strictness/flexibility. Remember, I make fValidate not for my own purposes, but for distribution. It needs to suit the needs of many. I know very well that the only way to truly test the validity of an email to send a message and wait for confirmation, but that's not what fValidate is about.

Perhaps in my next version I can include 2 or 3, and allow the user to specify a level of 'strictness' with a parameter.

Thanks for the thought-provoking comments. :D

BTW, your email regex will let me send '...@....ab', which, I think, is more than an obvious typo...

whammy
10-16-2002, 02:12 AM
Yup... but like I said, if someone wants to fool a regex, it's easy. And what's the use in getting complicated with a regex when someone can type billgates@microsoft.com ?!?

Better to just check for typos....

alaios
10-16-2002, 07:45 AM
thanks a lot guys....it is fine