Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question undefined message while assigning a function vlaues to a variable

    Hi

    I have the below code that does not works fine to validate a form. The issue is:

    a) if
    var b = validValues();
    is inside the valid() function, then validation is executed twice i.e., I get validation failure alerts twice on screen.
    b) if
    var b = validValues();
    is outside the valid() function, then validation is executed once, but I get "b" value as "undefined". This makes to execute only elseIF block.

    Please help me in fixing the issue. Is there any way to get the validValues return boolean value in a variable outside the function and use it in another function?

    Code:
    function valid(){
    	var b = validValues();
    var cardType=document.getElementById('cardType').value; 
    
    if(cardType=="temporary" && b){
    	alert("1");
    	return dateCheck();
    }
    else
    if(!b){
    	   alert("2");
    return validValues();
    }
    }

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by naveendk.55 View Post
    a) if is inside the valid() function, then validation is executed twice i.e., I get validation failure alerts twice on screen.
    If validValues() returns false, you're executing it twice: once to assign the value to the variable b and again when in the if (!b) statement.

    My guess is that you should be using the following:

    Code:
    else if (!b) { // b === false
    	alert("2");
    	return false; // equivalent to |return b| since |b === false|
    }
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts
    In returning validValues(), I do get an alert to enter the missing text box values. IF I only RETURN FALSE, then the alert is not displayed.

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by naveendk.55 View Post
    In returning validValues(), I do get an alert to enter the missing text box values. IF I only RETURN FALSE, then the alert is not displayed.
    Well, since you want validValues to do two different things, you should probably have two separate functions: one that returns a Boolean value and another that shows the alert. Either that, or add an argument to the function that can be used to show or suppress the alert; for example, validValues(false) could trigger an alert.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •