Jump to content


This topic is now archived and is closed to further replies.


whats wrong?

Recommended Posts

Hi all, im trying to create an email address input validation script.

I have this:

<script type="text/javascript">
function validateSubEmail() {

    var str = subscribe.email.value;

    if (str.indexOf(".") > 2) && (str.indexOf("@") > 0) {
    } else {
        alert("Invalid Email!");

<form name="subscribe" action="subscribe.php">
<input type="text" name="email" /><input type="button" onclick="validateSubEmail()" value="Go">


... but it's not working :S .. anyone have any idea why?? .. its not saying error in the status bar at all tho... just nothing happens :S .. try it here:

[a href=\"http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert\" target=\"_blank\"]http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert[/a]

help much appreciated!

Share this post

Link to post
Share on other sites
When you say it's not working, do you mean you don't receive an alert for an invalid email address, or the form doesn't submit? The latter would likely be because you don't have an ACTION property set on your form telling it where to send the information.

Also, the above isn't very strong validation. I could use aa.@ as my email address, and it would go through. It would be better to use a regex like:

[code]var str = subscribe.email.value;

if (str.match(/\b^[a-zA-Z0-9_.]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/\b) {
    // Email address is valid
else {
    // Email address is not valid

Share this post

Link to post
Share on other sites
Hmm, I realised afterwards I hadn't included the action, but on my real page it's there..

if you could pelase have a look it's here: [a href=\"http://www.website.newbiestyle.co.uk/exec\" target=\"_blank\"]http://www.website.newbiestyle.co.uk/exec[/a]

When you click go there's an error in the stauts bar :S

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.