slipperyfish Posted April 9, 2006 Share Posted April 9, 2006 Hi all, im trying to create an email address input validation script.I have this:[code]<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>[/code]... 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! Quote Link to comment Share on other sites More sharing options...
Vorotaev Posted April 9, 2006 Share Posted April 9, 2006 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}[/code] Quote Link to comment Share on other sites More sharing options...
slipperyfish Posted April 9, 2006 Author Share Posted April 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.