OK. Several things you can do here :-)
1. Learn how to use the ?: operator
2. Don't use brackets around one-line
if/else
statements
3. Don't create variables simply to use them in
if
statements. Eg you don't need to create the variable "x"
4. Try to keep empty lines to a mininum.
5. Keep an eye out for refactoring as mentioned in Nirav's answer
That's all pretty much in the Javascript - your HTML is pretty good.
Here's what your code looks like after following the tips above:
<div>
Email:
<input type="text" name="email" id="txtEmail" onblur="validate('txtEmail', 'lblError1', 'Enter valid email address')">
<label id="lblError1" style="color:red"></label>
</div>
<div>
Name:
<input type="text" name="email" id="txtName" onblur="validate('txtName', 'lblError2', 'Enter name')">
<label id="lblError2" style="color:red"></label>
</div>
<script>
function validate(element, outputElement, errormessage) {
document.getElementById(outputElement).innerHTML = document.getElementById(element).value==0) ? errormessage : "";
}
</script>