how to write 1 function insteaed of n functions to validate textboxes requiring same validations?
<p>This is my sample code in which i have 2 text boxes requiring same validations.How can i write a single function for both the text boxes requiring same validations. Here is my code:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;title&gt;Home&lt;/title&gt;

&lt;body style="background-color:#708090;"&gt;

&lt;script type="text/javascript" &gt;


function a() {
    var ay = document.getElementById("ayear").value;

var pattern = /\d{4}-\d{2,4}/;


    if(pattern.test(ay))

    {

    ay.style.backgroundColor="#52F40C";
    return true;
        } 
    else 
    {
        window.alert ("Enter the correct acadamic year");
    ay.style.backgroundColor="#F40C0C";
     ay.focus();
         ay.value="";        
     return false;

    }
}


function b() {

var fy=document.getElementById("fyear").value;
var fpattern = /\d{4}-\d{2,4}/;


  if(fpattern.test(fy))
    {

    fy.style.backgroundColor="#52F40C";
    return true;
        } 
    else 
    {
        window.alert ("Enter the correct financial year");
    fy.style.backgroundColor="#F40C0C";
        fy.focus();
        fy.value="";
    return false;

    }
}

function c()
{


    var n=document.getElementById("name");

    var re=/^[a-zA-Z]+ ?[a-zA-Z]*$/;

    if(re.test(n.value))
    {

               n.style.backgroundColor="#52F40C";
    }
    else
    {
        window.alert("Invalid place name");
               n.style.backgroundColor="#F40C0C";
               n.focus();
               n.value="";

    }
}


&lt;/script&gt;





&lt;/p&gt;
&lt;h3 style="font-family:Verdana"&gt;Notes&lt;/h3&gt;

&lt;h4 "style="font-family:Verdana;text-align:center;"&gt;&lt;b&gt;&lt;u&gt;Declaration&lt;/u&gt;&lt;/b&gt;&lt;/h4&gt;

&lt;form   method="post"&gt;

&lt;p&gt; This  Report is prepared for the Current Acadamic Year(&lt;input type="text" size="9"  id="ayear" onchange=a();&gt;) and the Current Financial Year (&lt;input type="text" size="9"  id="fyear" onchange=b();&gt;) on behalf of the Institution.&lt;/p&gt;




&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>`</p>

<p>In the code function a and b does same validations so can i reduce 2 functions to 1? please help me I'm a fresher need to do a bigger logic later</p>
