MrPink1138
Programmer
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...
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);">