Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<script>function Validate(valueData,errorMsg,lblID) {

           if (valueData != "") {

               document.getElementById(lblID).innerHTML = "";
           }
           else {

               document.getElementById(lblID).innerHTML = errorMsg;

           }

       }

       </script>
<div>
    

        Email:
        <input type="text" name="email" id="txtEmail" onblur="Validate(this.value,'Enter Email','lblError1')">
        <label id="lblError1" style="color:red"></label>
    </div>
    <div>
        Name:
        <input type="text" name="email" id="txtName" onblur="Validate(this.value,'Enter Name','lblError2')">
        <label id="lblError2" style="color:red"></label>
    </div>
Posted

- Use document.ready to write your function
- Use id selector instead of document.getElementById - refer to
- Use text() to set the value.
Refer to
http://jquery.com/
 
Share this answer
 
Try this:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
#errormsg
{
   color:red;
   display: none;
}
</style>
<script>
$(document).ready(function(){
  $("input").blur(function(){
    if ($(this).val() == ""){
        $(this).next().show(); 
    } else {
        $(this).next().hide(); 
    }
  });
});
</script>
</head>
<body>
<div>
Email:
   <input type="text" name="email" id="txtEmail">
   <label id="errormsg">Enter Email</label>
</div>
<div>
Name:
   <input type="text" name="name" id="txtName">
   <label id="errormsg">Enter Name</label>
</div>
</body>
</html>

Up front, set the respective erormsgs. then hide them through the css using display:none. On blur event, when the input value is empty, then show the errormsg else hide it through jQuery. You have to read out the jquery selector to understand it better: jquery_ref_selectors[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900