0

i am trying to doing input validation its my complete code but it is not working well.

<html>

<head>

<title>Testing Validation</title>
<script type = "text/javascript">
function getvalue()
{
    value = document.getElementById('myId').value;
    if(value < 'a' || value > 'z' || value != '@' && value != '.')
    {

        alert("Not a valid E-mail adress");
    }
}

</script>
</head>

<body>
<form name = "frm">
<input type="text" name="input" size="40" id="myId" />
<input type = "submit" name = "submit" onclick="getvalue()" />
</form>
</body>

</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Imran Ali
  • 59
  • 2
  • 10

1 Answers1

2

If you want to check that an email is valid, look for solutions using regex. This has been discussed before on SO: Using a regular expression to validate an email address

Community
  • 1
  • 1
marcgg
  • 65,020
  • 52
  • 178
  • 231
  • 1
    There exist email addresses with non-latin characters and it will be more in [near future](http://en.wikipedia.org/wiki/Internationalized%5Fdomain%5Fname). There are also tld's with more than 4 chars (e.g. `.museum`). See [this answer](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/1931322#1931322) for another regex example. – BalusC Jun 24 '10 at 14:48
  • It still assumes a limit on the length of the TLD, so it isn't future proof. Oh, and it rejects email addresses with plus characters in them (as well as the aforementioned problem about non-latin characters). – Quentin Jun 24 '10 at 15:16
  • @david: I'm not trying to get the optimal solution, just something that'll help imran while keeping it simple ^^ – marcgg Jun 24 '10 at 16:02
  • Rejecting perfectly good email addresses is somewhat worse then "suboptimal". There are too many broken regular expressions purporting to validate email addresses resulting in people being unable to sign up to websites they want to use, frustrating end users and losing companies business. We don't need any more of this junk being published, and where it is published, it needs warnings wrapped around it so, hopefully, nobody will be foolish enough to actually try to use it. You said you are trying to keep it simple, but you aren't keeping it simple enough, you are being too specific throughout – Quentin Jun 24 '10 at 16:24
  • @david dorward: I've edited my answer. My main point was "do not make a loop, use a regex", I didn't pay attention to the actual regex I gave (first google result). What you're saying makes sense, hence the update. – marcgg Jun 24 '10 at 16:58
  • That probably isn't a good resource to support your argument, the accepted answer for it says **don't** use a regex! – Quentin Jun 24 '10 at 17:03
  • @david: I know, but there are also good regex below. And server side validation isn't everything – marcgg Jun 25 '10 at 08:53
  • Who mentioned server side validation? (It is almost everything as it happens (what it isn't is highly convenient for the user, what it is is "not subject to tampering"), but using a broken regular expression there is as problematic as doing it with the client. – Quentin Jun 25 '10 at 10:05
  • @David: Then I'm not I get what you said before ^^, the accepted answer links to http://fightingforalostcause.net/misc/2006/compare-email-regex.php, which is a pretty decent resource regarding the subject – marcgg Jun 25 '10 at 13:02
  • That resource is covered with a list of provisos! – Quentin Jun 25 '10 at 13:07
  • @david: what would you recommend then? – marcgg Jun 25 '10 at 13:15
  • Keep things simple. Check it has one and only one @ in it. Try to send email to it. – Quentin Jun 25 '10 at 13:20