Forum Moderators: open

Message Too Old, No Replies

call function when leave the textbox

how to call function when leave textbox

         

Yuen

8:36 am on Apr 9, 2009 (gmt 0)

10+ Year Member


any one can help

/*this code this when click submit this program will check the validation ,but i want after fill in the textbox when leave the program will check the validation..any one know about that..thanks..*/

<html>
<style>
.error {
font-family: Tahoma;
font-size: 8pt;
color: red;
margin-left: 50px;
display:none;
}

</style>

<script>
function checkForm() {
name = document.getElementById("name"[smilestopper]).value;
email = document.getElementById("email"[smilestopper]).value;
comment = document.getElementById("comment"[smilestopper]).value;

if (name == ""[smilestopper]) {
hideAllErrors();
document.getElementById("nameError"[smilestopper]).style.display = "inline";
document.getElementById("name"[smilestopper]).select();
document.getElementById("name"[smilestopper]).focus();
return false;
} else if (email == ""[smilestopper]) {
hideAllErrors();
document.getElementById("emailError"[smilestopper]).style.display = "inline";
document.getElementById("email"[smilestopper]).select();
document.getElementById("email"[smilestopper]).focus();
return false;
} else if (comment == ""[smilestopper]) {
hideAllErrors();
document.getElementById("commentError"[smilestopper]).style.display = "inline";
document.getElementById("comment"[smilestopper]).select();
document.getElementById("comment"[smilestopper]).focus();
return false;
}
return true;
}

function hideAllErrors() {
document.getElementById("nameError"[smilestopper]).style.display = "none"
document.getElementById("emailError"[smilestopper]).style.display = "none"
document.getElementById("commentError"[smilestopper]).style.display = "none"
}
</script>

<body>
<form onsubmit="return checkForm();" method=post action="">
<span class=label>Name:</span><input type=text value="" id=name><br>
<div class=error id=nameError>Required: Please enter your name<br></div><br>
<span class=label>Email:</span><input type=text value="" id=email><br>
<div class=error id=emailError>Required: Please enter your email address<br></div><br>
<span class=label>Comment:</span><textarea type=text value="" id=comment></textarea><br>
<div class=error id=commentError>Required: Please enter a comment<br></div><br>
<input type=submit value=Submit style="margin-left: 50px">
</form>
</body>
</html>

whoisgregg

9:07 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the onblur() event to trigger a function when an element loses focus.

If you run into trouble attaching that event, please just post back and we'll do our best to help. :)