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>
2
  • Lot of similar questions: stackoverflow.com/search?q=[javascript]+validate+email Commented Jun 24, 2010 at 14:51
  • I hope you also validate the email address on the server side. JavaScript validation could be faked. Commented Jun 24, 2010 at 16:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

12 Comments

There exist email addresses with non-latin characters and it will be more in near future. There are also tld's with more than 4 chars (e.g. .museum). See this answer for another regex example.
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).
@david: I'm not trying to get the optimal solution, just something that'll help imran while keeping it simple ^^
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
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.