/    Sign up×
Community /Pin to ProfileBookmark

Checking Multipple text boxes

I ahve a form that has multiple text boxes populated from a DB through a For…Next loop. Here is the code (simplified),

<%
For x = 1 to 10
%>
<input type=”text” name=”qty_<%=x%>” size=”1″>
<%
Next
%>

Basically I have 10 text boxes named qty_1 through qty_10. I need a way using javascript to check that these boxes (at least one of them) has a value in it before the form is submitted. Here is what I have so far.

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin

//** check and uncheck all item checkboxes **
function CheckAll() {
for (var i=0; i<document.form1.elements.length;i++) {
var e = document.form1.elements[i];
var blnflag = 0;
if (e.name == ‘qty_’+[i]) {
if (e.value == “”){
blnflag = 1;
}
}
}
if (blnflag == 1){
alert (“you must filling at least one field”);
return;
}
}

// End –>
</SCRIPT>

I have onsubmit=”return CheckAll();” in the form element. I am not gettign any errors when the form loads but it does not do the needed check either.

Anyone know of a better way to check the value of these text boxes?

Thanks

Ian

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Oct 27.2004 — Merely an example:

<HTML>

<Head>

<Script Language=JavaScript>

function calculate(isForm) {

isError = false;
for (i=0; i<isForm.length-1; i++)
{if (isForm[i].value.length == 0){isError = true}}
if (isError){alert('Please complete all fields')}
else {alert('All Fields Complete');rawData.submit()}

}

</Script>

</Head>

<Body>

<Form name='rawData'>

<Table id='dynTable' cellpadding=5 cellspacing=0 border=1>

<TR><TD> Attack </TD><TD><input type=text name='attack' size=5></TD></TR>

<TR><TD> Strength </TD><TD><input type=text name='strength' size=5></TD></TR>

<TR><TD> Defence </TD><TD><input type=text name='defence' size=5></TD></TR>

<TR><TD> Ranged </TD><TD><input type=text name='ranged' size=5></TD></TR>

<TR><TD> Magic </TD><TD><input type=text name='magic' size=5></TD></TR>

</Table>

<br>

<input type=button value='Calculate' onclick="calculate(rawData)">

</Form>

<Div id='results'></Div>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@neil9999Oct 27.2004 — Try:

[code=php]<script type="text/javascript">
function CheckAll(){
var a=0;
for(var i=1;i<11;i++){
if(document.getElementById("qty_"+i).value==""){a++}
}
return a<10;
//use the line above if you don't want the alert, or the line below if you do:
if(a==10){alert("You must enter a value in atleast one textbox");return false}
}
</script>[/code]


Neil
Copy linkTweet thisAlerts:
@neil9999Oct 27.2004 — Warren86, your code won't submit at all if javascript is disabled.

Andersoni, you should use additional server side validation incase the user has javascript disabled.

Neil
Copy linkTweet thisAlerts:
@JPnycOct 27.2004 — <script type="text/javascript">

function whatever() {

var radios=document.getElementsByTagName('input');

for(var i = 0;i < radios.length;i++)

{

if(radios[i].checked==true) {



return true;

}

else {

alert ("you must filling at least one field");

return false

} }}

</script>



Untested, but it's just a general approach.
×

Success!

Help @andersoni spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...