Jump to content


Photo

whats wrong?


  • Please log in to reply
2 replies to this topic

#1 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 09 April 2006 - 12:29 AM

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

I have this:

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

    var str = subscribe.email.value;

    if (str.indexOf(".") > 2) && (str.indexOf("@") > 0) {
        subscribe.submit();
    } else {
        alert("Invalid Email!");
    }
}
</script>
</head>

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

</html>

... 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!

#2 Vorotaev

Vorotaev
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationCanada

Posted 09 April 2006 - 12:58 AM

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:

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
}


#3 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 09 April 2006 - 05:20 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users