By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,648 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,648 IT Pros & Developers. It's quick & easy.

why not move to new page?

P: 1
hello

the code below checks for a valid email address and then should move to either of 2 pages but no move happens! Why?

Thanks

Geoff

[HTML]<script>
function getNextPage(){
var num = Math.random();
if (num<.5) {
alert('going to 1');
location.href='group1/group1-lab1.htm';
} else {
alert('going to 2');
location.href='group2/group2-lab1.htm';
}
}

function validateEmail ( emailField, errorMsg ) {
emailpat =
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
if( !emailpat.test( emailField.value ) ) {
alert( errorMsg);
emailField.focus();
emailField.select();
return false;
} else {
//return true;
parent.frame_top.top_value = emailField.value;
document.getElementById('mail').className='hiddenD iv';
getNextPage();
}
}

//-->
</script>
</script>
</head>
<body>

<h2>test</h2>

<div id="mail" class="visibleDiv">
<form name="emailForm" onsubmit="validateEmail( this.email , 'Please enter a valid email address')">
Please enter your email address <input type="text" name="email">
<input type="submit" value="enter">
</form>
</div>[/HTML]
May 4 '08 #1
Share this Question
Share on Google+
1 Reply


hsriat
Expert 100+
P: 1,654
Remove the duplicate script end tag.

Also, if you have to redirect the page to another page, what's the need of the email validation? I guess you want to change the action of the form instead of redirecting the browser.

For that, in the end of second function, instead of writing getNextPage(), write return getNextPage() there.

And modify your getNextPage() function like this...
Expand|Select|Wrap|Line Numbers
  1. function getNextPage(){
  2.     var num = Math.random();
  3.     if (num<0.5) {
  4.         alert('going to 1');
  5.         document.forms['emailForm'].action='group1/group1-lab1.htm';
  6.     }
  7.     else {
  8.         alert('going to 2');
  9.         document.forms['emailForm'].action='group2/group2-lab1.htm';
  10.     }
  11. return true; //important
  12. }
And don't forget this when you post here next time:
[code] --- your code --- [/code] (Use code tags)
May 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.