Javascript based Validation

They have: 330 posts

Joined: Apr 2000

This will be very easy for someone I'm sure...

How would I tell the form not to submit if a certain fields is equal to 1? I need to do this with Javascript which I'm sure is very simple.

<?php
<head>
&
lt;script language=\"javascript\"&gt;
function isValidResolution() {
if document.theForm.Resolution == 1;
{
alert('You must select a valid resolution');
return false;
}
}
&lt;/script&gt;
</head>
?>

Am I on the right track with that idea?

Jack Michaelson's picture

He has: 1,733 posts

Joined: Dec 1999

&lt;script language="javascript"&gt;
function isValidResolution()
{
if (document.theForm.Resolution == "1");
{
alert('You must select a valid resolution');
}
}
&lt;/script&gt;
'

I think...

Shakespeare: onclick || !(onclick)

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi artsapimp,

As Jack implies, you need to surround your condition with parentheses. Also, if Resolution is a text field, you are better off putting the value you are comparing in quotes.

In addition, your form tag must be either using the onSubmit property:

In either case, you need to return false as you had originally.

Vinny

Where the world once stood
the blades of grass cut me still

They have: 330 posts

Joined: Apr 2000

I would not have known that. Thank you.

Art

They have: 330 posts

Joined: Apr 2000

I know I did something wrong, but I can't find it.

Here's the top of my code...

<?php
<!-- #Include File = \"cnn.inc\" -->
<%
'Define SQL statements
strResultSQL = \"SELECT * FROM Result ORDER by Result\"
strDowngradedSQL = \"SELECT * FROM Downgraded ORDER by Downgraded\"
strPerceptionSQL = \"SELECT * FROM Perception\"
strChurnedSQL = \"SELECT * FROM Churned ORDER by Churned\"
?>

<script language=\"javascript\">
function isValidResolution()
{
If (document.theForm.Result.value == \"8\");
{
alert('You must select a result');
return false;
}
}
</script>

Outbound Tracking

 

Number: 

 

Mailbox Setup? 

Yes
No

 

Result of Call: 

<?php
rst
.Open strResultSQL, cnn
Do Until rst.EOF
?>

<?php
= rst(\"Result\")
?>

<?php
rst
.MoveNext
Loop
rst
.Close
?>

?>

Thanks for any help.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi artsapimp,

My mistake Sad , not yours Smiling

tag should read:

Vinny

They have: 330 posts

Joined: Apr 2000

I'm getting an error "object expected" and then it submits the forms. I tried a few things but it's still not working. What is wrong with this?

<?php
&lt;script language=\"javascript\"&gt;
function isValidResolution()
{
If (document.theForm.Result.value == \"8\");
    {
    alert('You must select a result');
    return false;
    }
}
&lt;/script&gt;

<link rel=\"stylesheet\" type=\"text/css\" href=\"/webday/style.css\">

&lt;script language=\"javascript\" src=\"../../validate.js\"&gt;
&lt;/script&gt;

</head>
<body bgcolor=\"#000000\">
<center>
<form action=\"Submit1.asp\" method=\"Post\" name=\"theForm\" onSubmit=\"return isValidResolution()\">
?>

Thanks again for any help.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi artsapimp,

1)
If (document.theForm.Result.value == "8");

should be:

if .....

(javascript is case-sensitive)

"Result" is a selection list? If so,

if (document.theForm.Result.options[document.theForm.Result.selectedIndex].value == '8');

Vinny

Where the world once stood
the blades of grass cut me still

They have: 330 posts

Joined: Apr 2000

It is a select box but I still got an error. I changed it to just lower case i and it worked as the first example.

Thanks for your help.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi artsapimp,

You're welcome, though I'm not sure what you mean and glad it's up and running Laughing out loud

Vinny

They have: 330 posts

Joined: Apr 2000

I kept it in this format...

if (document.theForm.Result.value == "8");

with the lowercase i and in "if" and it worked.

I thought you were saying in a drop-down box I would need to change the format to the 2nd example above but that didn't work for me.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.