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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am trying to make this function:

Status
Not open for further replies.

unixxxx

MIS
Oct 20, 2000
17
US
I am trying to make this function:

function check()
{
for (var i=0;i<document.form1.length;i++){


if (document.form1.elements.value==&quot;&quot;)
{
alert(&quot;Please enter your name.&quot;);
document.form1.elements.focus();
break;

I used to call it document.form1.name.value but if I want to change it to elements. what is the proper syntax if there are 2 elements that I should check in the whole form?
Let say, if I there are two fields, such as name and adrress. What is the correct syntax in using elements to specify each element?

Thank you.
 
if (document.form1.elements.value==&quot;&quot;)
{
if (document.form1.elements.name==&quot;name&quot;)
{
//do what must be done.
}

}

But why would you want to loop through all of the elements if you know the names of the elements you want to check?
 
function check() {
for (var x = 0; x < document.form1.elements.length; x++){
if (document.form1.elements[x].value==&quot;&quot;) {
alert(&quot;Please enter a value.&quot;);
document.form1.elements[x].focus();
}
}
} =========================================================
if (!succeed) try();
-jeff
 
let say if I want to loop through all elements but I don't know the name of the elements that I would like to check, and I use the if .. else statement to write the condition of each element. This is how I did it, but , but it still did not alert when other elements are not filled out.
Anything is wrong with how the element is written?



Any suggestion? thx



function validateform()
{
for (var x=0;x<document.form1.elements.length;x++){

if (document.form1.elements[x].value==&quot;&quot;){
alert(&quot;Please enter your first name:&quot;);
document.form1.elements.focus();
}
else if (document.form1.elements.value==&quot;&quot; ) {
alert(&quot;Please enter your address:&quot;);
document.form1.elements.focus();
}
else
{
alert(&quot;Done&quot;);
break;
}
}
 
unixxxx,

you could simply use the name attribute of each field:
[tt]
<html>
<head>
<title></title>
<script language=&quot;javascript&quot;>
function validateForm(oForm) {
for (var x = 0; x < oForm.elements.length; x++){
if (oForm.elements[x].value==&quot;&quot;) {
alert(&quot;Please enter a &quot; + unescape(oForm.elements[x].name) + &quot;.&quot;);
oForm.elements[x].focus();
return false;
}
}
}
</script>
</head>

<body>
<form onsubmit=&quot;return validateForm(this);&quot;>
First Name:<input type=&quot;text&quot; name=&quot;First%20Name&quot; value=&quot;&quot; />
<br/>Last Name:<input type=&quot;text&quot; name=&quot;Last%20Name&quot; value=&quot;&quot; />
<p/>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot; />
</form>
</body>
</html>
[tt]
=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top