would any of you charming people like to help me with some javascript? (1 Viewer)

Super Dexta

New Member
Joined
Aug 15, 2002
Messages
7,841
Form validation my gee. I yoinked it from somewhere else and now I don't now how to get it to do what I want. Basically it's supposed to check that all the fields in a form have been filled in and give you a little pop-up, and it does that, but if they haven't been filled in it submits the form anyway :(

it looks like this:


<script type="text/javascript">


function validate_form ()
{
valid = true;

if ( document.form1.nam.value == "" )
{
alert ( "Please fill in the 'Your Name' box." );
valid = false;
}

if ( document.form1.skill.value == "" )
{
alert ( "Please fill in the 'Skill' box." );
valid = false;
}

if ( document.form1.cert.value == "" )
{
alert ( "Please fill in the 'Certificate' box." );
valid = false;
}

if ( document.form1.inst.value == "" )
{
alert ( "Please fill in the 'Institution' box." );
valid = false;
}
if ( document.form1.date.value == "" )
{
alert ( "Please fill in the 'Date' box." );
valid = false;
}



return valid;
}

</script>

and my form thing looks like this:
<form action="addskills.php" method="post" name="form1" onSubmit="return validate_form ( )">
 
At a guess you'll need a condition in your form action saying only to go ahead only if your valid boolean is true.
But I haven't done javascript since the 90s.
 
Works fine for me (doesn't submit the form if at least one of the fields is not filled in) in both IE and Firefox. Here's what I have :

Code:
<html>
<head>
<script type="text/javascript">

function validate_form ()
{
valid = true;

if ( document.form1.nam.value == "" )
{
alert ( "Please fill in the 'Your Name' box." );
valid = false;
}

if ( document.form1.skill.value == "" )
{
alert ( "Please fill in the 'Skill' box." );
valid = false;
}

if ( document.form1.cert.value == "" )
{
alert ( "Please fill in the 'Certificate' box." );
valid = false;
}

if ( document.form1.inst.value == "" )
{
alert ( "Please fill in the 'Institution' box." );
valid = false;
}
if ( document.form1.date.value == "" )
{
alert ( "Please fill in the 'Date' box." );
valid = false;
}

return valid;
}

</script>
</head>
<body>
<form action="addskills.php" method="post" name="form1" onSubmit="return validate_form ( )">
    <label>Name:</label><input type="text" name="nam" value=""/>
    <label>Skill:</label><input type="text" name="skill" value=""/>
    <label>Cert:</label><input type="text" name="cert" value=""/>
    <label>Institution:</label><input type="text" name="inst" value=""/>
    <label>Date:</label><input type="text" name="date" value=""/>
    <input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
 
Interesting. I'll try it tomorrow when I have the full HTML in front of me.

I spent most of the evening wrestling with Java instead. It was a fun time.
 
Java, C# and C++ are more my thing.
nerd.jpg
 

Users who are viewing this thread

Activity
So far there's no one here
Old Thread: Hello . There have been no replies in this thread for 365 days.
Content in this thread may no longer be relevant.
Perhaps it would be better to start a new thread instead.

21 Day Calendar

Lau (Unplugged)
The Sugar Club
8 Leeson Street Lower, Saint Kevin's, Dublin 2, D02 ET97, Ireland

Support thumped.com

Support thumped.com and upgrade your account

Upgrade your account now to disable all ads...

Upgrade now

Latest threads

Latest Activity

Loading…
Back
Top