1

Hey guys. I am currently using a very inefficient script to validate my forms. The code is massive.

The idea with this is if an input box is blank the input label is highlighted red and a div at the top of the form show's information on the error.

function new_receiver(){


if (document.getElementById("RecieversName").value ==""){ //First Name

        var txt=document.getElementById("error_receiver");
        txt.innerHTML="<p><font color=\"#FF0000\">You need to enter a Name!</font></p>";    
        window.document.getElementById("RecieversName_label").style.color = '#FF0000';

        //Reset
        window.document.getElementById("receiver_check_label").style.color = '#000000';
        window.document.getElementById("RecieversNumber_label").style.color = '#000000';
        window.document.getElementById("RecieversEmail_label").style.color = '#000000';

}else if (document.getElementById("RecieversNumber").value ==""){ //First Name

        var txt=document.getElementById("error_receiver");
        txt.innerHTML="<p><font color=\"#FF0000\">You need to enter a Phone Number!</font></p>";    
        window.document.getElementById("RecieversNumber_label").style.color = '#FF0000';

        //Reset
        window.document.getElementById("receiver_check_label").style.color = '#000000';
        window.document.getElementById("RecieversName_label").style.color = '#000000';
        window.document.getElementById("RecieversEmail_label").style.color = '#000000'

}else if (document.getElementById("RecieversNumber").value ==""){ //First Name

        var txt=document.getElementById("error_receiver");
        txt.innerHTML="<p><font color=\"#FF0000\">You need to enter an Email!</font></p>";  
        window.document.getElementById("RecieversEmail_label").style.color = '#FF0000';

        //Reset
        window.document.getElementById("receiver_check_label").style.color = '#000000';
        window.document.getElementById("RecieversName_label").style.color = '#000000';
        window.document.getElementById("RecieversNumber_label").style.color = '#000000';


}else{
from.receiver.submit();

}'

Any ideas or methods to making this process easy as some of my forms have up to 9 input boxes and this validation method is massive!

Cheers Guys!!!

Samuel.

4 Answers 4

1

I suggest something like this:

function new_receiver() {
    var inputs = [
        {
            id: "RecieversName",
            name: "a Name"
        },
        {
            id: "RecieversNumber",
            name: "a Phone Number"
        }
        // add more here
    ],
    length = inputs.length,
    error = document.getElementById("error_receiver"),
    hasError = false,
    i;

    // reset
    for (i = 0; i < length; i++) {
        document.getElementById(inputs[i].id + "_label").style.color = "#000000";
    }
    error.innerHTML = "";

    for (i = 0; i < length; i++) {
        if (document.getElementById(inputs[i].id).value == "") {
            error.innerHTML = "<p><font color=\"#FF0000\">You need to enter " + inputs[i].name + "!</font></p>";    
            document.getElementById(inputs[i].id + "_label").style.color = "#FF0000";
            hasError = true;
            break;
        }
    }

    if (!hasError) {
        from.receiver.submit();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can start by trying something like this

 $('input').blur(function() {
     if($(this).val()==""){
            $("#error_receiver").html("<p><font color=\"#FF0000\">This field is required!");
            $("label[for=' + this.attr("id") + ']").css('color', '#FF0000');
         }
  });

Comments

1

Here is a suggestion without jQuery

<form onsubmit="return new_receiver()">
.
.

var formFields = {
  "RecieversName": "You need to enter a Name!",
  "RecieversNumber":"You need to enter a Number!",
  "RecieversEmail":"You need to enter an Email!"   
}
function new_receiver(){
  var txt=document.getElementById("error_receiver");
  //Reset
  txt.innerHTML="";
  for (var o in formFields) document.getElementById(o+"_label").style.color = '#000000';

  for (var o in formFields) {
    if (document.getElementById(o).value ==""){ 
      txt.innerHTML="<p><font color=\"#FF0000\">"+formFields[o]+"</font></p>";    
      window.document.getElementById(o+"_label").style.color = '#FF0000';
      return false;
    }
  }
  return true
}  

Comments

0

you can use jquery validation plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/

3 Comments

It would require him to be using jQuery too, which it looks like he isn't.
@alex He has mentioned jQuery in the title
Cheers mahesh. I'm trying to avoid using more plug ins as I already have a head but that's still an excellent option for the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.