Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation function

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
I have a JS validation function that works just fine.
I want to modify it so it validates on the form submit
(which it does) but only when the submit button value is "commit" and not "save". So I added to the function:

&& (document.form1.submitButton == 'commit' )

Well now it dosen't alert when the save button is clicked
which is a good thing but it won't validate when the commit button is clicked.


The input for the commit is :
<INPUT type="submit" Value="commit" name="commit">

Code:
function validate_form()	{
    if (( document.form1.cpd3.checked == false )&& ( document.form1.cpd4.checked == false ) && ( document.form1.cpd5.checked == false ) && ( document.form1.cpd37.checked == false ) && (document.form1.submitButton == 'commit' ))
    {
        alert ( "Please check one of the Quality checkboxes." );
        valid = false;
    }
)

The function above is a lot longer than the example but its redundant info so I ommited alot of it.

Thanks

Dan
 
You need to add this to your If statement (highlighted):

Code:
function validate_form()    {
    if (( document.form1.cpd3.checked == false )&& ( document.form1.cpd4.checked == false ) && ( document.form1.cpd5.checked == false ) && ( document.form1.cpd37.checked == false ) && (document.form1.submitButton[!].value[/!] == 'commit' ))
    {


[monkey][snake] <.
 

Monsnake

Hi.


Ok I added it and its throwing and error:

Error:'document.form1.submitbutton.vale' is null or not an object
I have two buttons on the form.
Could that be the problem.

<INPUT type="submit" Value="commit" name="commit">

<INPUT type="submit" Value="Save" name="Save1">
 
My bad, change it to this:

Code:
document.form1.[!]commit[/!].value == 'commit'

[monkey][snake] <.
 
Syntax, though I don't use it is


document. (form name) . (form element name) . value of form element name.

I didn't even pay attention to the fact that you referred to the submit button as submitButton, the name you specified for it is 'commit'

[monkey][snake] <.
 
I beleive the syntax is
Code:
document.form1.submitButton.name == 'commit'

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Code:
<INPUT type="submit" [!]Value="commit" name="commit"[/!]>


1DMF, either .name or .value would work in this case cause they are the same.

Typically it's better practice to assign that to a value.


[monkey][snake] <.
 
monksnake,


HA. Ok but now it throws the same error when I hit the save button.

Error:'document.form1.submitbutton.vale' is null or not an object
 
I always thought the Value attribute for buttons was what what was displayed on the button , not an actual value like an input field - lol you learn something new every day!

Dashley : you have a type'O'

'document.form1.submitbutton.value'

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
In the error it was spelled correctly. I hand typed it into this form
 
I always thought the Value attribute for buttons was what what was displayed on the button, not an actual value like an input field

1DMF: It's both actually.


Dashey:
'document.form1.[!]submitbutton[/!].val[!]u[/!]e'

See above posts.

[monkey][snake] <.
 
In the error it was spelled correctly. I hand typed it into this form
 
actually should it be an upper case B as well?
Code:
'document.form1.submit[b]B[/b]utton.val[b]u[/b]e'

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
another thing I prefer to do is also pass it the object and work direct rather than use document. etc...

so on the form
Code:
onsubmit="function validate_form(this);"

then the validate can be...
Code:
function validate_form(obj)    {
    if (( obj.cpd3.checked == false )&& ( obj.cpd4.checked == false ) && ( obj.cpd5.checked == false ) && ( obj.cpd37.checked == false ) && (obj.submitButton.name == 'commit' )){do what ever;}
}


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I have two submit buttons on the form.

<INPUT type="submit" Value="commit" name="commit">

<INPUT type="submit" Value="Save" name="Save1">

The commit button dosen't appear until the save button is clicked for the first time. From that point the client can re-save as many times as they want. Once the commit button is clicked they can't do any more chages / Saves.

I've added the line

&& (document.form1.commit.value == 'commit'

to the function so it would only validate the forms checkboxes when the commit button was clicked and not the save button.

It seems to work execept now when the save button is clicked Im getting a:

Error:'document.form1.commit.value' is null or not an object




Code:
<form name="form1" method="post" action="31all.asp?formid=31&loc=set" onSubmit="validate_form(); return CheckAction ( );">


Code:
function validate_form()    {
    if (( document.form1.cpd3.checked == false )&& ( document.form1.cpd4.checked == false ) && ( document.form1.cpd5.checked == false ) && ( document.form1.cpd37.checked == false ) && (document.form1.commit.value == 'commit' ))
    {
        alert ( "Please check one of the Quality checkboxes." );
        valid = false;
    }
)
 
Code:
It seems to work execept now when the [!]save button is clicked[/!] Im getting a:

Error:'document.form1.[!]commit[/!].value' is null or not an object

The name of the submit button with "save" written on it is name="Save1" not "commit".

I'm trying to get you to understand the syntax, that's why I'm not just typing it out for you.

[monkey][snake] <.
 
You've got a couple really fundamental problems with your website.

First, your "save" button is a button of type submit - which means that the form is submitted each time it is clicked. Why not just make it type="button"?

Second, this condition is completely pointless because it should evaluate true every single time:
Code:
document.form1.commit.value == 'commit'

It would seem that you're definitely trying to do this "the hard way"

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
I wrote the initial function thinking there was one submit button that dynamically changed from save to commit. Then I got all skeeted.

[monkey][snake] <.
 
Monksnake,

Wow the little light just went on HA. I see what you mean.
Don't I feel dumb HA.

Kaht

I see what you mean about the condidtion being pointless but..

My origian intent was to mod the validation function.

If these 3 coditions existed

if (( document.form1.cpd31.checked == false )&& ( document.form1.cpd32.checked == false ) && ( document.form1.cpd33.checked == false )

and somebody clicked the commit button (not the save) then I wanted an alert box to pop up telling the user they have to check one of the checkboxes.

So thinking I knew what I was doing (HA) I added the line
&& (document.form1.commit.value == 'commit' )).

Thinking that was a good test because the only other choice would have been someone clicked the save button.


I do need to submit the form in either case to update data.




 
aren't you calling this via the FORM tag onsubmit?

that was the impression I got and therefore the code you need is NOT

&& (document.form1.commit.value == 'commit' ))

BUT

&& (document.form1.submitButton.name == 'commit' ))


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top