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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically un-check a checkbox? 1

Status
Not open for further replies.

MrPink1138

Programmer
Jul 10, 2003
34
US
Forgive me if this is s stupid question but I do 99% of my scripting on the server side so I'm not up on my client side javascript.

I have a form where users can enter in a bunch of email addresses and hit a checkbox for wether or not that person should be contacted. I want to have it so that if you check a checkbox and the correspoding text field isn't valid, it'll warn you and un-check the box. I'm 90% of the way there. I can get the warning to popup but can't get the box to uncheck.

The number of contact fields on the form is dynamically generated so I can't specifically specify the form element. I need to pass it when the checkbox is clicked.
Any ideas?

Here's my code so far...

Code:
<script language="javascript">
	function CheckEmail(field, strEmail) {
                //if user checked box
		if (field.checked == true) {
                        //Check to see if email is valid
			if (!isValidEmail(strEmail)) {
                                //Uncheck box and display alert
				field.checked == false;
				alert("You must enter a valid email address before checking this box");
			}
		}
	}

	function isValidEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
	return (true)
	}
	return (false)
	}
	</script>	


<input name="email1" type="text" maxlength="150" size="40" value=""> <input type="radio" name="send_email1" value="yes" onclick="CheckEmail(this, document.contacts.email1.value);">

<input name="email2" type="text" maxlength="150" size="40" value=""> <input type="radio" name="send_email1" value="yes" onclick="CheckEmail(this, document.contacts.email2.value);">
 
Have you tried removing one of the equal signs in this:
Code:
field.checked == false;
so that it looks like
Code:
field.checked = false;
?
you only need one equal sign when assigning values, two when checking for equality.

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Do'h!! I knew it was something silly like that. Thanks a million. Everything works perfect now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top