PDA

View Full Version : Helped Needed: JavaScript Basics


Darkstar
25-06-2004, 19:42:24
Hello!

I need to JavaScript a web page so that it won't let users leave the page when they click the "Submit" button if 3 specific text input fields (normal text boxes) are empty (popping up a "Naughty User! Fill in the required fields!" would be nice as well). You JavaScript experienced are welcome to post example code or links to such.

Thanks for code, info, or links in advance.

Vincent
25-06-2004, 21:42:57
Did you say COBOL?

Sir Penguin
25-06-2004, 21:59:30
Yes, please do it in COBOL.

SP

Darkstar
25-06-2004, 22:15:51
COBOL doesn't run client-side on old Macs. So I need it in JavaScript.

Honestly, I expected to have seen a link from our webbies on their favorite sources by now. But it seems football is currently holding all their attention.

I don't mind doing some reading and figuring matters out on my own (I am a programmer, after all)... but I need to get this whacked out by the end of Monday or Tuesday at the latest to get it all tested so it can be approved for an "emergency" roll out (meaning, we publish the page update and don't tell anyone).

And posting this here (asking for help/links) let's me keep CG firmly planted in the "work related" list of sites. ;)

Sir Penguin
25-06-2004, 22:21:45
I recommend www.google.ca.

SP

Darkstar
25-06-2004, 22:38:34
Well, I intend to use www.google.com Monday. And maybe some msdn.microsoft.com if I *have* to (depending on what my MSDN DVD Library spits up). But honestly, with the amount of webbies we have here, I expected a quick pointer to a good "basic" or "beginner" javascript site. Or even a couple of code snippets. Unlike some people and their C code requests, I'm not asking people to write and debug it for me...

Now, if you, SP, don't know, that's fine. You are more a Python person, aren't you? Venom is a webbie, but I don't recall what flavors. Mikie is mostly a PHP webbie, isn't he?

zmama
26-06-2004, 08:12:10
Try here...and quit whining
http://www.javascriptworld.com/scripts/index.html

If that isn't what you're looking for then ask again and maybe someone will help, but not likely on a weekend.

Venom
26-06-2004, 13:23:12
Yeah, ask again on Monday. I'm getting ready to leave but I'll help then.

Nav
26-06-2004, 13:29:45
put this code inbetween the <head></head> tags.


<script language="JavaScript" type="text/javascript">

function checkform ( form )
{
if (form1.company.value == "") {
alert( "Please enter your Company Mame." );
form1.company.focus();
return false ;
}
if (form1.contact.value == "") {
alert( "Please enter your Contact Name." );
form1.contact.focus();
return false ;
}
return true ;
}
</script>


Then make sure that the form tag is like this:


<form action="yourscript.php" method="post" name="form1" onsubmit="return checkform(this);">


just make sure the field names match in the form. There's slightly more involved for checking a dropdown is selected. I don't have that code to hand.

Venom
26-06-2004, 13:31:20
Here. Couple of ways to do it. This one's pretty simple and quick Try this.

function chkdes(){
if (document.groupform.RequiredField.value==""){
alert("Please enter a Designer First Name.");
document.groupform.RequiredField.focus;
return false;
}
if (document.groupform.RequiredField2.value==""){
alert("Please enter a Designer Last Name.");
document.groupform.RequireField2.focus;
return false;
}
}

and to execute that code in the form tag that matches with the submit button use something like this:

form method="POST" action="whatever.asp" name="groupform" onsubmit="return chkdes();"

Hopefully that helps. If not, well, there's a reason I'm unemployed, isn't there?

Venom
26-06-2004, 13:32:21
God damn you nav. And I tried on that. And yours is probably still better.

Nav
26-06-2004, 13:33:57
ha beat you. :lol:

yeah and edit out that less than one shit (<1). ;)

Venom
26-06-2004, 13:35:22
Yeah, that was retarded.

Venom
26-06-2004, 13:37:35
I was half thinking checking the length of the string as that's actually faster than comparing to a blank.

Nav
26-06-2004, 14:35:17
is it? not that you'd notice really.

Venom
27-06-2004, 00:28:06
Supposedly. But as you said, you'd never notice on an order of magnitude this small.

Darkstar
28-06-2004, 18:02:33
Thanks gang! :D

Darkstar
28-06-2004, 18:53:23
Worked like a charm. Thanks again! Especially to Nav and Venom!

And this makes another tick mark for CG in the "Work related" column for me. :cool:

Venom
28-06-2004, 20:49:35
You used Nav's, didn't you? Goddamn I'm a failure.

Darkstar
28-06-2004, 20:59:15
Bit of both, actually. And you guys really helped out. Thanks again! :D

I had a flood of "examples" come in from various teams and groups at work today, as word went out I was going to need a ~simple~ JavaScript example to do that. And they all sent these monster files (200 Kb is the smallest!) of their pages to do such a thing. And interesting links... but not to any JavaScripting sites. Which is par for the course around here. ;)

Venom
28-06-2004, 21:02:20
When you need simple, come to the simplest. Me!

Darkstar
28-06-2004, 21:16:11
I'll remember that for the future. :)

Venom
28-06-2004, 21:21:24
A little sample of the goods. 2+2=4

Immortal Wombat
28-06-2004, 21:23:50
var NumberInput = 2;
for(i = 0; i < 1; i = i ++){
NumberInput = NumberInput + NumberInput;
}
return NumberInput;