0

I have the following Javascript code:

function checkIfValid(){
 var form = document.createuserform;
 var valid = new Array();
 for(i = 0; i < 4; i++){
 valid[i] = false;
 }
 if(form.fName.value == ""){
 form.getElementById('fNameStatus').innerHTML = "Please Enter Your First Name";
 valid[0] = false;
 }else{
 document.getElementById('fNameStatus').innerHTML = "";
 valid[0] = true;
 }
 if(form.lName.value == ""){
 valid[1] = false;
 }else{
 valid[1] = true;
 }
 if(!isValidEmail){
 valid[2] = false; 
 }else{
 valid[2] = true;
 }
 if(form.pass.value == ""){
 valid[3] = false;
 }else{
 valid[3] = true;
 }
 if(checkIfValid(valid)){
 form.submit();
 }
}
function checkIfValid(arr){
 for(i = 0; i < arr.length; i++){
 if(!arr[i]){
 return false;
 }
 }
 return true;
}
function isValidEmail(){
 var x=document.forms["createuserform"]["email"].value
 var atpos=x.indexOf("@");
 var dotpos=x.lastIndexOf(".");
 if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
 {
 return false;
 }
 return true;
}

Upon running I get the following error in firefox error console "arr is undefined line 46". An

asked Jul 27, 2011 at 18:27
2
  • you have two functions with the same name, and what about moving the var valid = new Array(); line outside of the function Commented Jul 27, 2011 at 18:31
  • Line 43 in your code example is return false. Commented Jul 27, 2011 at 18:32

3 Answers 3

2

It looks like you might be trying to first call a no-argument version of checkIfValid in which an overloaded version of the same function is called with a single argument.

JavaScript does not have function overloading.

The last definition is used whenever checkIfValid is invoked.

answered Jul 27, 2011 at 18:31
Sign up to request clarification or add additional context in comments.

2 Comments

sorry I don;t understand? Where am I overloading the method? I have it defined only once and am calling it once...I just want to pass the valid array into the checkIfValid function
The first function in your example is function checkIfValid(); the second function in your example is function checkIfValid(arr). Those names are the same, which is an error. Your first function can never be called.
0

You have the functions checkIfValid(arr) and checkIfValid(). One or the other, not both. Like the other people said, javascript doesn't support function overloading

answered Jul 27, 2011 at 18:53

Comments

0

As others have said, you can't have the same named function with different parameters in javascript and expect both to work. Only the last one defined is active. You can, however have a single function that checks to see what parameters were passed and act accordingly. So, you could replace both of your functions with this single function:

function checkIfValid(arr){
 // if array passed, check it
 if (arr) {
 for(i = 0; i < arr.length; i++){
 if(!arr[i]){
 return false;
 }
 }
 return true;
 }
 // if no array passed, check our form
 var form = document.createuserform;
 var valid = new Array();
 for(i = 0; i < 4; i++){
 valid[i] = false;
 }
 if(form.fName.value == ""){
 form.getElementById('fNameStatus').innerHTML = "Please Enter Your First Name";
 valid[0] = false;
 }else{
 document.getElementById('fNameStatus').innerHTML = "";
 valid[0] = true;
 }
 if(form.lName.value == ""){
 valid[1] = false;
 }else{
 valid[1] = true;
 }
 if(!isValidEmail){
 valid[2] = false; 
 }else{
 valid[2] = true;
 }
 if(form.pass.value == ""){
 valid[3] = false;
 }else{
 valid[3] = true;
 }
 if(checkIfValid(valid)){
 form.submit();
 }
}

Or, if your second function is really just a helper function and not intended to be an overloaded version of the first one, then just change it's name:

function checkIfValid(arr){
 // if no array passed, check our form
 var form = document.createuserform;
 var valid = new Array();
 for(i = 0; i < 4; i++){
 valid[i] = false;
 }
 if(form.fName.value == ""){
 form.getElementById('fNameStatus').innerHTML = "Please Enter Your First Name";
 valid[0] = false;
 }else{
 document.getElementById('fNameStatus').innerHTML = "";
 valid[0] = true;
 }
 if(form.lName.value == ""){
 valid[1] = false;
 }else{
 valid[1] = true;
 }
 if(!isValidEmail){
 valid[2] = false; 
 }else{
 valid[2] = true;
 }
 if(form.pass.value == ""){
 valid[3] = false;
 }else{
 valid[3] = true;
 }
 if(checkIfArrayValid(valid)){
 form.submit();
 }
}
function checkIfArrayValid(arr){
 for(i = 0; i < arr.length; i++){
 if(!arr[i]){
 return false;
 } 
 }
 return true;
}
answered Jul 27, 2011 at 20:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.