Jump to content

function not working


ballhogjoni

Recommended Posts

Can someone help me with this code. I have an issue with the last function. The last function is a checkbox not a text field. For some reason It does not show the error if the checkbox is not checked.

 

<script type="text/javascript">
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
  reason += validatepassword(theForm.password);
  reason += validatepassword_confirmed(theForm.password_confirmed);
  reason += validateemail(theForm.email);
  reason += validatei_agree(theForm.i_agree);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 50)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length. The username can only have one Capital letter. 50 Characters Max.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatepassword(fld) {
    var error = "";
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "The password is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The password contains illegal characters. 30 Characters Max.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatepassword_confirmed(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a password confirmed.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "The password confirmed is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The password confirmed contains illegal characters. 30 Characters Max.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatepassword_confirmed(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a password confirmed.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "The password confirmed is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The password confirmed contains illegal characters. 30 Characters Max.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validateemail(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter an email.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 100)) {
        fld.style.background = 'Yellow'; 
        error = "The email is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The email contains illegal characters. 100 Characters Max.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatei_agree(fld) {
var error = "";
  if (document.getElementById("i_agree").checked=false) {
  	error = "You must agree to the Terms and Conditions";
  }
return error;
}
</script>

Link to comment
https://forums.phpfreaks.com/topic/63933-function-not-working/
Share on other sites

try checking if you're actually getting the object referenced:

 

function validatei_agree(fld) {
var error = "";
  alert("field passed is: "+fld+"\nfield reference is: "+document.getElementById("i_agree")+"\nchecked value is: "+document.getElementById("i_agree").checked);
  if (document.getElementById("i_agree").checked=false) {
  	error = "You must agree to the Terms and Conditions";
  }
return error;
}

 

this should at least give you some insight into what's going on with your references and values.

Link to comment
https://forums.phpfreaks.com/topic/63933-function-not-working/#findComment-318662
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.