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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ASP.net + prototype.js email validation

    I'm fairly new to javascript and AJAX and I can't figure out why this isn't working.

    What I'm trying to accomplish here is email validation using prototype's Ajax.Request method and ASP.net. The problem is that when I call the function (validateEmail('element')) it returns an undefined value. Within the validateEmail function I have a boolean called bValid. When the function is called it makes an ajax request to validate the email address and then assign true or false to bValid then return bValid, but bValid doesn't seem to want to retain a value. I know that the request is working and returning correct values it's just bValid (that is outside the request function) that's being difficult.

    Below is the code I have. Any help would be greatly appreciated.

    Code:
    function valEmail(elementID) {
    	var bValid;
    	new Ajax.Request('../apps/valEmail.aspx', {
    		method:'get', 
    		parameters: {email: $F(elementID)},
        	onComplete: function(transport) {
          		var res = transport.responseText;
    			if (res == "false") {
    				bValid = false;
    			}
    			if (res == "true") {
    				bValid = true;
    			}
        	},
        	onFailure: function() { 
    			window.location = "../error.html";
    		}
    	});
    	return bValid;
    }
    This is how I call the function:

    Code:
    function validateEmail() {
    	if ($F('email_test') != "") {
    		if (valEmail('email_test') == true) {
    			$('error').innerHTML = "valid";
    		} else {
    			$('error').innerHTML = "invalid";
    		}
    	} else {
    		$('error').innerHTML = "empty";
    	}
    }

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    The reason bValid is undefined for the function validateEmail is because you have declared 'var bValid' in the function valEmail, giving it a local scope. bValid is only defined in the function valEmail.

    If you declare bValid globally then both functions will have access to it.


  •  

    Posting Permissions

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