PDA

View Full Version : help with a check word form



keyboard
05-19-2011, 04:57 AM
Hi every one.
I'm fairly new to javascript and I need some help with a script

This is what i've found be mashing together some scripts from a few different sites.

<HTML>
<HEAD>

<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "fred")
alert("You entered: " + myTextField.value);
else
var answer = alert("Correct")
if (answer){

}
else{
alert("Bye bye!")
window.location = "http://www.google.com/";
}
}
</script>

</HEAD>
<BODY>

<input type='text' id='myText' />
<input type='button' onclick='notEmpty()' value='Form Checker' />

</BODY>
</HTML>


I would like it if when you enter random text it comes up with an elert saying what you just entered. And when you enter the special word.... fred ...... it says correct and then redirects you to another page, in this case google.

It is nearly doing this however when you enter text that isn't fred it comes up with the alert like it's meant to, but then redirects you to google any way.


Any help on this would be appreciated

keyboard
05-20-2011, 01:33 AM
Actually I've edited it down a bit more

<HTML>
<HEAD>


<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "fred")
alert("You entered: " + myTextField.value);

else

alert("Correct")
window.location = "http://www.google.com/";

}
</script>


</HEAD>
<BODY>



<input type='text' id='myText' />
<input type='button' onclick='notEmpty()' value='Form Checker' />




</BODY>
</HTML>


But I still get the same problem. Any help is appreciated.

JoeDaStudd
05-20-2011, 08:51 AM
Its because you have line breaks in the code in the wrong locations.

As a rule of thumb I always use {} when using loops or conditions. As it avoids this sort of issue.

The below code will work.

<HTML>
<HEAD>
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "fred"){
alert("You entered: " + myTextField.value);
}
else{
alert("Correct");
window.location = "http://www.google.com/";
}
}
</script>
</HEAD>
<BODY>
<input type='text' id='myText' />
<input type='button' onclick='notEmpty()' value='Form Checker' />
</BODY>
</HTML>

keyboard
05-21-2011, 07:54 AM
Thanks!

keyboard
05-22-2011, 07:33 AM
Hi everyone,
I've got two more questions.
Is there a way to make the code either fred or george? I mean if the user entered fred, they would be redirected, but if they entered george they would also be redirected.

Also is there a way to make it so if they enter fred, it directs them to one page, but if they enter george, it directs them to a different page.

Thanks for any help