...

View Full Version : Javascript validation not working on Form



developerlili
01-04-2011, 07:12 PM
Hello:

I am using javascript validation for submitting a form. All the other fields are working except for the Terms of Use checkbox. It sends and alert and says it wasn't checked whether or not you check it. It also submits the form whether or not it is checked immediately after the alert. The field is called df_Terms. Can anyone see anything I am missing?



<script>

//submit after validation functionality
function submit_onclick() {

var proceed = false;
if (typeof validateForm == "function") {

proceed = validateForm();
}
else {

proceed = true;
}
return proceed;
}

//validation functionality

function validateForm() {





if ((document.form1.df_Plans[0].checked == false) && (document.form1.df_Plans[1].checked == false)) {
alert("Please select Your Plan");
document.form1.df_Plans[0].focus();
return false;
}


if ("" == document.form1.df_first_Name1.value) {

alert("Please enter your First Name");
document.form1.df_first_Name1.focus();
return false;
}

if ("" == document.form1.df_last_Name1.value) {

alert("Please enter your Last Name.");
document.form1.df_last_Name1.focus();
return false;
}

if ("" == document.form1.df_email1.value) {

alert("Please enter your email.");
document.form1.df_email1.focus();
return false;
}

validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
strEmail = document.form1.df_email1.value;

if (strEmail.search(validRegExp) == -1) {
alert("A valid e-mail address is required.");
document.form1.df_email1.focus();
return false;
}

if (!document.form1.df_Terms.checked == 1) {

alert("Please agree to the Terms of Use.");
document.form1.df_Terms.focus();
return false;
}


}

//reset field functionality
function reset_onclick() {

for (i = 0; i < document.form1.elements.length; i++) {

if (document.form1.elements[i].type == "text") {

document.form1.elements[i].value = "";
}
if (document.form1.elements[i].type == "checkbox") {

document.form1.elements[i].checked = false;
}
if (document.form1.elements[i].type == "radio") {

document.form1.elements[i].checked = false;
}
if (document.form1.elements[i].type == "select-one") {

document.form1.elements[i].selectedIndex = 0;
}
}
}

//redirect functionality
function redirect_onclick(urlString) {

window.open("http://" + urlString);
}

</script>

Philip M
01-04-2011, 07:44 PM
if (!document.form1.df_Terms.checked == 1) {

Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

developerlili
01-04-2011, 07:55 PM
if (!document.form1.df_Terms.checked == 1) {

Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

Hi Thanks for the response. Can you please assist with what the proper syntax should be?

developerlili
01-04-2011, 09:14 PM
I have read the forums and tried a couple of solutions but i am still having no luck.

I changed the following line.



if (document.form1.df_Terms.checked == false) {

alert("Please agree to the Terms of Use.");
document.form1.df_Terms.focus();
return false;
}

Logic Ali
01-04-2011, 09:36 PM
I have read the forums and tried a couple of solutions but i am still having no luck.

I changed the following line.



if (document.form1.df_Terms.checked == false) {

alert("Please agree to the Terms of Use.");
document.form1.df_Terms.focus();
return false;
}


Technically there's nothing wrong with if (!document.form1.df_Terms.checked == 1) since boolean true evaluates to 1.
Can you confirm:
There are no console errors?
Your checkbox is actually part of the form that you haven't shown?
The referenced element name is a case-sensitive match?

developerlili
01-04-2011, 09:43 PM
Technically there's nothing wrong with if (!document.form1.df_Terms.checked == 1) since boolean true evaluates to 1.
Can you confirm:
There are no console errors?
Your checkbox is actually part of the form that you haven't shown?
The referenced element name is a case-sensitive match?


Hi:
This is the html code for the form


<td height="30"><input value="true" type="checkbox" name="df_Terms" /><input type="hidden" name="df_Terms" />
<td height="30" align="left">I agree to the Terms of Use.<strong><span style="color: red"> *</span></strong></td>

Logic Ali
01-04-2011, 10:00 PM
Hi:
This is the html code for the form


<td height="30"><input value="true" type="checkbox" name="df_Terms" /><input type="hidden" name="df_Terms" />
<td height="30" align="left">I agree to the Terms of Use.<strong><span style="color: red"> *</span></strong></td>
Now that you've shown the form markup, the problem is blatantly obvious. You have two elements with the same name.

developerlili
01-04-2011, 10:06 PM
Now that you've shown the form markup, the problem is blatantly obvious. You have two elements with the same name.

You were absolutely right! Not sure why i didn't catch that one. This issue was escalated to me from another developer who wrote the html code. Thanks for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum