Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a generic function

Status
Not open for further replies.

greyone

Programmer
Dec 14, 2000
200
CA
i have written a generic function to check if the fields are empty or not in the page. Now the problem is that i want check for multiple empty fields. I really don't know how can i modify the below function to incorporate this. Please help


function checkIfEmpty(formNo,elementName,alertMessage,location)
{
if (document.forms[formNo].elements[elementName].value=="")
{
alert(alertMessage);
return false;
}
else{
window.location=location;
return true;
}
}
 
Hi.

When do you call this method? What event or element call this? Could you write a more detailed description about it to my email?
tibor.kircsi@scala.hu

regards, Kirilla
 
I call this onclick of the button as follows:

<input name=&quot;ldate&quot;>
<input type=&quot;button&quot; Value=&quot;Next&quot; name=&quot;next&quot; onClick=&quot;checkIfEmpty(0,'lDate','some mssg','hello.html')&quot;>
 
Hi.

I think, you have to add more variable to your function like this:

If you use &amp;&amp; operator all of your element value must be equal &quot;&quot; to send an alert.
If you use || operator then you will get alert when one of them value is equal &quot;&quot;.

function checkIfEmpty(formNo,elementName1,elementName2,...,elementNameXY,alertMessage,location)
{
if (document.forms[formNo].elements[elementName1].value==&quot;&quot; || document.forms[formNo].elements[elementName2].value==&quot;&quot; || ....)
{
alert(alertMessage);
return false;
}
else{
window.location=location;
return true;
}
}

<input name=&quot;ldate&quot;>
<input name=&quot;ldate2&quot;>
<input type=&quot;button&quot; Value=&quot;Next&quot; name=&quot;next&quot; onClick=&quot;checkIfEmpty(0,'lDate','lDate2','some mssg','hello.html')&quot;>

OR another way:

function checkIfEmpty(formNo,elementName1,elementName2,...,alertMessage,location)
{
if (document.forms[formNo].elements[elementName1].value==&quot;&quot;)
{
alert(alertMessage);
return false;
}else{
if (document.forms[formNo].elements[elementName2].value==&quot;&quot;)
{
alert(alertMessage);
return false;
}else{
window.location=location;
return true;
}
}
.....

Loop if statements.

regards, Kirilla
 
But is there no other way to count the number of elements in the function and depending on the element display an appropriate alert message.
Is there a way in which the user just has to specify the number of elements and the alert message for each element.
Will the arguments property of the function help in this case. Please let me know
 
Hi.

Here is a dynamic checking for the elements value:

<html>
<Script Language=&quot;JavaScript&quot;>

function ff()
{
var num=document.forms[&quot;mm&quot;].all.length;
var elements=document.forms[&quot;mm&quot;].all;

for(i=0;i<num;i++){
alert(elements[ i ].value);
}
}

</Script>
<body>
<Form name=&quot;mm&quot; id=&quot;mm&quot;>
<input type=text name=&quot;myElem&quot;>
<input type=text name=&quot;myElem&quot;>
<input type=text name=&quot;myElem&quot;>
<input type=text name=&quot;myElem&quot;>
</Form>
<input type=&quot;button&quot; value=&quot;kkk&quot; onclick=&quot;ff()&quot;>
</body>
</html>


regards, Kirilla
 
thanks a lot for the reply Kirilla

But i want the user to pass arguments in the function and check for empty only for those fields which is specified in the function other than the entire document.
 
the arguments you pass to a function are held in an array called arguments:


function checker(){
for(var 1=0;i<arguments.length){
//do your things
}

}


-BB &quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
well I forgot the i++, but you dig me right? &quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
Hi.

It will not check the entire document, it checks only those elements which are on the form called &quot;mm&quot; and has the &quot;myElem&quot; name. You can recognize the 1st, the 2nd and all of these elements and personalize an privat alert or anything else.

regards, Kirilla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top