PDA

View Full Version : return values from other functions



miranda
Jul 27th, 2005, 02:23 PM
I have three javascript functions that return a value true or false. I would like to check for these inside another javascript function. Right now I can get the value of the 1st function returned then it stops. I tried using an if statement but it didnt work. How do you get this to show whether all three are true or false? here is what i have so far, the commented out code is what was tried that didn't work. (obviously javascript is not my forte)


String.prototype.isEmail = function(){
return /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(this);
}
String.prototype.isPhone = function(){
return /^\(?[1-9]\d{2}\)?\s?\-?[1-9]\d{2}\-\d{4}$/.test(this);
}

function validateMe(theForm){
var form1 = document.forms[0];
//var valid = true;
//if(return checkPhone(form1.txtTelephone) == false){
// valid= false;
//}
//if(!return checkPhone(form1.txtTelephone)){
// valid= false;
//}
return checkPhone(form1.txtTelephone)
return checkEmail(form1.txtEmail);
return checkName(form1.txtName);
//return valid;
}
function checkEmail(theField){
var form1 = document.forms[0];
var valid = true;
if (form1.txtEmail.value == ""){
form1.errEmail.value = "Required!";
valid = false;
}else if (!form1.txtEmail.value.isEmail()){
form1.errEmail.value = "Invalid email address!";
valid = false;
}else{
form1.errEmail.value = "";
}
return valid;
}
function checkName(theField){
var form1 = document.forms[0];
var valid = true;
if (form1.txtName.value == ""){
form1.errName.value = "Required!";
valid = false;
}else{
form1.errName.value = "";
}
return valid;
}
function checkPhone(theField){
var form1 = document.forms[0];
var valid = true;
if (form1.txtTelephone.value == ""){
form1.errPhone.value = "Required!";
valid = false;
}else if (!form1.txtTelephone.value.isPhone()){
form1.errPhone.value = "Invalid Telephone Number!";
valid = false;
}else{
form1.errPhone.value = "";
}
return valid;
}

shyam
Jul 27th, 2005, 02:57 PM
they do not work because when a return statment is reach further execution of the code is terminated. so u function shoulda looked like this


function validateMe(theForm){
var form1 = document.forms[0];
var valid = true;
var valid = checkPhone(form1.txtTelephone) && checkEmail(form1.txtEmail) && checkName(form1.txtName);
return valid;
}