Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2007, 08:49 PM   PM User | #1
phoedaddy
New to the CF scene

 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
phoedaddy is an unknown quantity at this point
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";
	}
}
phoedaddy is offline   Reply With Quote
Old 09-12-2007, 04:16 AM   PM User | #2
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
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.
rwedge is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:35 AM.


Advertisement
Log in to turn off these ads.