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

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 12-15-2006, 04:09 AM   PM User | #1
prajakta
New to the CF scene

 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
prajakta is an unknown quantity at this point
on blur validation

hi,
In my program have 2 fields. and that fiels must not be null. when i write the code both field onBlur it goes in loop.

code

function firstonblur()
{
if(document.getElementById('a').value.length<=0)
{
alert("Field A must be enter");
document.getElementById('a').focus();
}
}

function secondonblur()
{
if(document.getElementById('b').value.length<=0)
{
alert("Field B must be enter");
document.getElementById('b').focus();
}
}

plz send the perfect solution for this problem.
prajakta is offline   Reply With Quote
Old 12-15-2006, 05:32 AM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Validating onblur is not the best option. Firstly it will put the user into a loop which you have discovered and 2nd it will force a console error in Firefox because of autocomplete. The preferred method is to validate in the end just before the form is submitted.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function doValidate()
{
	if(document.getElementById('a').value.length <= 0)
	{
		alert("Field A cannot be blank!");
		document.getElementById('a').focus(); 
		return false;
	}
	else if(document.getElementById('b').value.length <= 0)
	{
		alert("Field B cannot be blank!");
		document.getElementById('b').focus();
		return false; 
	}
}
</script>
</head>

<body>
<form action="#" method="post" onsubmit="return doValidate()">
<label for="a">Input A: <input type="text" name="a" id="a"></label><br>
<label for="b">Input B: <input type="text" name="b" id="b"></label><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ 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 01:49 AM.


Advertisement
Log in to turn off these ads.