We help IT Professionals succeed at work.

simple one?

QPR
QPR asked
on
Medium Priority
1,657 Views
Last Modified: 2008-02-26
Hi,
As there is no validation for a check box I slid in a Javascript function to check that a checkbox has been checked and to alert the user and stop the form from submitting if they haven't.
Done this many many times in classic ASP but .NET doesn't seem to want to play.
Have a made a syntax error? Is it something to do with all the forms being server-side?

The function:
<script language="javascript" type="text/javascript">
   function AgreeCheck() {
        if(document.form1.ChkAgree.checked = false)
        {
            alert('Please tick the I agree box to continue');
            document.form1.ChkAgree.focus();
            return false;
        }
     }
   </script>

The event:
<form id="form1" runat="server" onsubmit="return AgreeCheck()">

The html server up:
<form name="form1" method="post" action="Default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">

Error:
document.form1.ChkAgree is null or not an object
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
can you tell us how you defined the ChkAgree input?
I guess that the issue is case sensitivity...
Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT

Commented:
Is the ChkAgree control within the Form tags?
QPR

Author

Commented:
<asp:CheckBox ID="ChkAgree" runat="server" />
Yes inside form tags
Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT

Commented:
Create a hidden field html field to carry the value of the checkbox. Set it when the checkbox is clicked.
Check the value of that field instead of the server control.

Commented:
First fix your operator in the if statement to "==".  Then make sure your form tag is named "form1".  Based on your comment above it looks like your <input type="checkbox"...>, when rendered, may not have a name attribute.  It should render like this (do a View Source to see):

<input type="checkbox" id="ChkAfree" name="ChkAgree".../>

The name part of the name/value pair in an HTML form comes from the name attribute in the input tag.

John
QPR

Author

Commented:
changed operator to ==
added name="form1" (visual studio put a squiggle under that but no problem)

Same error null or not an object.
It would be null if unchecked would it? Can you give a checkbox an unchecked value?
QPR

Author

Commented:
solved it myself.
I changed the checkbox from an asp server side control to a bog standard html input type.
Now all is fine!
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
PAQing question with points refunded
angelIII, page editor

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.