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

Validate blank text field

Status
Not open for further replies.

nrastogi

Programmer
Jul 19, 2002
52
US

Hi guys,

In my form, I have input fields for entering qunatities (these fields generated dynamically depending upon the number of line items in the order). All the fields has same name assigned to it. So, there is no way to differentiate them.

Now my question comes, I want to display the message when user leave/make one or more fields "blank" and want to place the curson on the first blank field.

Please suggest me how could I do this.

Thanks in advance.

NRastogi

PS: There is one more input field on the page that displays "Date" and is under another form tag.

 
why not assign a unique name to each field using the combination of a comman base name with an index. that way you can differentiate between fields. for example, you could have a base name of "quantity" and a variable set to 1 that is incremented 1 for each new field to display, so you'd have names like quanity1, quanity2, quantity3, etc.

glenn
 
You can validate all text fields as elements of form array in a loop:
[tt]
function validate(theform) {
flag = false;
for (n=0; n<theform.elements.length; n++)
if (theform.elements[n].type == &quot;text&quot; && theform.elements[n].value == &quot;&quot;)
flag = true;

if (flag) {
alert('Some fields are not filled');
return false;
}
else
return true;
}
[/tt]
This form validation should be called as this:
<form onsubmit=&quot;return validate(this)&quot;>

But, it is always better to name each form element with unique name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top