Click here to Skip to main content
15,888,061 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<div>
        Email:
        <input type="text" name="email" id="txtEmail" onblur="emailform()">
        <label id="lblError1" style="color:red"></label>
    </div>
    <div>
        Name:
        <input type="text" name="email" id="txtName" onblur="nameform()">
        <label id="lblError2" style="color:red"></label>
    </div>

    <script>


 function emailform()
 {
        var x = document.getElementById("txtEmail").value;
            if (x==0) {


                document.getElementById("lblError1").innerHTML = "Enter valid email Address";

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

            }
            }
    function nameform()
{
 var x = document.getElementById("txtName").value;
            if (x==0) {
                 document.getElementById("lblError2").innerHTML = "Enter Name";
            }
            else {

                document.getElementById("lblError2").innerHTML = "";
            }


        }

        </script>


Eleminate redundant code ,with same funtionality
Posted
Updated 26-May-14 21:46pm
v2

TRY THIS,, :)

JavaScript
function Validate(valueData,errorMsg,lblID) {

          if (valueData != "") {

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

              document.getElementById(lblID).innerHTML = errorMsg;

          }

      }



HTML
<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>


You can validate multiple textboxes from only one javascript function after passing three parameters (this.value,'Error Message','ID of Label')
 
Share this answer
 
v2
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:

XML
<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>
 
Share this answer
 
v2
Comments
amanpavanker 27-May-14 5:08am    
Thank you,but This code is not working.

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