PDA

View Full Version : Basic Javascript help?


trevo
2006-03-13, 03:30
Okay so it's not as advanced as some of the other code here in the programmer's nook ;) I'm starting to learn javascript in my course and just needed a little help.

How can I put all of the script under one <script> tag? When i've tried, the whole script doesn't work.

My "name" script (the first one) isn't it working right now and i'm not sure why?

When the email field is blank it produces both alerts, how would i change my code to display just the first alert when the field is empty?

<script type="text/javascript">
function validateForm () {
valid = true;
if( document.orderForm.contactName.value == "") {
alert ( "Please fill in the your name." );
valid = false;
}

valid = true;
email= new String(document.orderForm.contactEmail.value);

if(email == "") {
alert ("Please fill in your email.");
valid = false;
}

(email.indexOf(".")== -1) {
alert ("Your email address must include a 'full stop' followed by a domain, for example: (.com)");
valid = false;
}

return valid;
}

function getHelp()
{
parent.document.getElementById("bottomFrame").src="help.html"
}
</script>

Brad
2006-03-13, 03:37
I'll try to help you without directly doing your homework for you. :p

Are you familiar with "else"?

For example...

if (hungry) {
orderPizza();
} else {
postSomethingOnline();
}

This translates roughly to:

If you are hungry, order a pizza. Otherwise, post something online. However, you can not do both. "Else" is exclusive in this sense; it's strictly either one or the other.

Also, are you familiar with nesting if statements? You can put another if-else inside any other if or else statement.


if (hungry) {
if(haveMoney) {
orderPizza();
} else {
eatStaleCrackers();
}
} else {
postSomethingOnline();
}


This translates roughly to:

Are you hungry? If so, do you have money? If hungry and have money, order a pizza. If hungry but no money, eat a cracker. If not hungry at all, post something online.

Does that help? :) Hint: it should. ;)

staph
2006-03-13, 03:46
Two validateForm functions?

trevo
2006-03-13, 03:48
Two validateForm functions?

yes because i had to put it under more than one <script> tag to actually get it to work.

Thanks Brad, I'll give it a go when I get my head around what you just wrote.

Brad
2006-03-13, 03:51
yes because i had to put it under more than one <script> tag to actually get it to work.
That's very odd. You should be able to do it in one function. You should also be able to put all of the JavaScript code between one set of <script> tags if you wish.

A thought: remember that any "return" statement will immediately exit the function and prevent anything in that function after it from executing. Perhaps that was an issue somewhere?

Thanks Brad, I'll give it a go when I get my head around what you just wrote.
If there's anything I can do to clarify or if you want another if-else demonstration, please feel free to say so. :) I tried to make it a relatively simple example, but what I see as simple isn't always for everyone else.

staph
2006-03-13, 03:54
yes because i had to put it under more than one <script> tag to actually get it to work.

You really shouldn't have to do that. Why don't you have a squiz at the Javascript Console and see what it's telling you about your syntax?

It's in the Debug menu in Safari. You can enable the menu by firing up a Terminal and typing:

defaults write com.apple.Safari IncludeDebugMenu 1

trevo
2006-03-13, 04:24
:lol: Yes I love that Debug menu, just activated that this morning and forgot all about it. So.. To be honest I didn't think of that.

Anyway my JavaScript Console is only returning a parse error after I put the code back all under one <script> tag which is just a:

valid = false;


I've edited my first post which now shows how my code is looking but nothings working now. I understand the ELSE statement and what it does, however I thought it simply replaced where the IF statement was?

So instead of

if(email.indexOf.......)

It would be?

else(email.indexOf.......)

Brad
2006-03-13, 04:32
Not exactly.

However, you can do this:

if(email == "") {
...
...
...
} else {
if (email.indexOf(".")== -1) {
...
...
...
}
}

Now, you could use some shorthand here if you wanted to achieve what I think you're describing. The { } brackets are only necessary if the action following the if-else needs to do more than one command at a time. Going back to the pizza example I posted earlier, it only does one command as each result; so, it could be shortened to:

if (hungry) orderPizza();
else postSomethingOnline();

and:
if (hungry)
if(haveMoney) orderPizza();
else eatStaleCrackers();
else postSomethingOnline();


In the modification I posted above of your code, assuming that the outer else statement only contains one element (the second if-else block), you can take out the redundant brackets and have it read:

if(email == "") {
...
...
...
} else if (email.indexOf(".")== -1) {
...
...
...
}

That looks closer to what you mentioned.

I would not recommend using this shorthand as a beginner. Since you're just learning the basics, you really should include the brackets wherever possible as a reminder to yourself of the command hierarchy.

trevo
2006-03-13, 04:39
Yeah that makes sense! I think I get it :)

It's actually working now! :smokey: Thanks Brad

edit: thanks to you too staph