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
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
Share on other sites

Comparing is done via == sign not =.

function validatei_agree(fld) {
  if (fld.checked == false) { // if (!fld.checked)
    delete fld;
    return "You must agree to the Terms and Conditions";
  }
  delete fld;
  return "";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.