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 checkbox

Status
Not open for further replies.

necta

Technical User
Mar 8, 2004
35
MY
How do I validate dynamic checkbox whether it is selected? How I get the name of the dynamic checkbox name?
Thank you.

<script language=javascript>
//----------
// require that at least one checkbox be checked
var selected = false;
for (i = 0; i < theForm.frmNo.length; i++)
{
if (theForm.form.checked)
selected = true;
}
if (!selected)
{
alert("Please choose a field.");
return (false);
}
}
//-->
</script>

While Not RS.EOF

<form name--------- >
<Input type=”checkbox” name=form<%=frmNo%> value="<%=RS("ID")%>"”>

RS.Movenext

Wend
 
try this
Code:
function checkone(f){
var value = false
    for (i=0;i<f.elements.length;i++)
        if (f.elements[i].name.indexOf('form') !=-1)
            if(f.elements[i].checked == true)
               value = f.elements[i].checked;

return value;
}
and

Code:
<form name--------- onsubmit="return checkone(this)" >
While Not RS.EOF

<Input type=”checkbox” name=form<%=frmNo%>  value="<%=RS("ID")%>"”>

RS.Movenext

hth

simon
 

Thank for the tips, simon, u giving me the hints to validate the checkbox fields but when i tried it on dynamic checkbox, it did not work. The error "textfields.length not an object" I tried on marks.length, it also gave the same error.What have I done wrong? I used on blur to validate each field. The 'elements' work in checkbox but not here.

Necta

<!--
function numValidate(mrk)
{
var mrk;

for (i = 0; i < mrk.textfields.length; i++)
{
// check to see if the number field is blank
if (mrk.textfields.value == "")
{
alert("Please enter a number.");
mrk.textfields.focus();
return (false);
}
}
}
//-->
</script>


Do While NOT sRS.EOF
subjNo=subjNo+1
%>

<
<input type="text" name="marks<%=subjNo%>" id="marks<%=subjNo%>" type="text" size="4" value="<%=subjNo%>" maxlength="3" onblur="return numValidate(this)">


<%
sRS.moveNext
Loop
 
What are you trying to do - in the first post you mention checkboxes, which is the what my function will validate, then you repost changing my function and changing checkboxes to text fields?

whats this?

for (i = 0; i < mrk.textfields.length; i++)

this is where your errors lie.

are you trying to validate text or checkbox or both, please be clear.

Simon
 
Sorry for the mistake. I intend to validate text field which carry the numeric input. However I have successfully validate using If (mrk.value ==""). But now the problem is how to validate the input which is a numeric input (include of -1)
 
simple - try and make your posts clearer in future
Code:
function checkone(f){
var value = true
    for (i=0;i<f.elements.length;i++)
        if (f.elements[i].name.indexOf('form') !=-1)
            if((isNaN(f.elements[i].value)) || (f.elements[i].value == null) || (f.elements[i].value.length == 0))
               value = false;

return value;
}

hth

simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top