3

I have the following code that is for validating a for field as you are inputting values in the field (red for invalid and green if the field becomes valid):

function FormValidation(){
    var fn=document.getElementById("firstName").value;
    if(fn == ""){
        document.getElementById("firstName").style.borderColor = "red";
        return false;
    }else if (/^[0-9]+$/.test(document.getElementById("firstName").value)) {
        document.getElementById("firstName").style.borderColor = "red";
        return false;
    }else if(fn.length <=2){
        document.getElementById("firstName").style.borderColor = "red";
        return false;
    }else{
        document.getElementById("firstName").style.borderColor = "#679f30";
    }
}

These validations will also be required for other fields.

I tried a for loop, but it doesn't work to validate all of the forms as the code above does for the one field:

function FormValidation(){
    var array = ["firstName", "middleName", "lastName"];
    for(i=0; i < array.length; i++){
        var fn=document.getElementById(array[i]).value;
        if(fn == ""){
            document.getElementById(array[i]).style.borderColor = "red";
            return false;
        }else if (/^[0-9]+$/.test(document.getElementById(array[i]).value)) {
            document.getElementById(array[i]).style.borderColor = "red";
            return false;
        }else if(fn.length <=2){
            document.getElementById(array[i]).style.borderColor = "red";
            return false;
        }else{
            document.getElementById(array[i]).style.borderColor = "#679f30";
        }
    }
}

So my question is am I doing something wrong? Or is there a way to validate more than one form in one function without having to write the same code over and over.

Any help is much appreciated!

Thank you, Al

1
  • On which particular event of the form are you calling the function FormValidation?? It's important to know this. Commented Apr 18, 2015 at 6:07

2 Answers 2

4

Pass the id as an argument for the function and access the respected parameter. So instead of writing the entire code again and again you can call the function with your specific argument. function FormValidation(id){ var fn=document.getElementById(id).value; if(fn == ""){ document.getElementById(id).style.borderColor = "red"; return false; }else if (/^[0-9]+$/.test(document.getElementById(id).value)) { document.getElementById(id).style.borderColor = "red"; return false; }else if(fn.length <=2){ document.getElementById(id).style.borderColor = "red"; return false; }else{ document.getElementById(id).style.borderColor = "#679f30"; } }

Call the function with parameter as the id you need to validate. eg return FormValidation("firstName")

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I can't believe I didn't think of this..SMH
@Baraa Welcome but be carefull while validating names. Its tricky.. Awsme to hear it helped you.
0

Give them the same class and then use

getElementByClass

And cycle through them

1 Comment

I thought there isn't a getElementByClass but only a getElementByClassName ? and wouldn't this have the same effect as my array above? which doesn't work as intended.

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.