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 =开发者_C百科 "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
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.
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
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;
}
精彩评论