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

validating a textbox based on the value of a chkbox

Status
Not open for further replies.

jamica

Programmer
Apr 25, 2005
17
GB
hi can anyone help me imreally stuck dont no what im doing wrong .... what im trying to do is server side vaildation in vbscript to see if a chkbox is chked then the textbox cannot be empty... i also need to maintain the values in the fields if onsubmit there are otehr errors on the page

i have this code:
<% If Request.Form("POSTED") AND Request.Form("other_lang_chk") = "true" Then
Response.Write "<input name='other_org' type='checkbox' id='other_org' value='true' checked >"
else
Response.write "<input name='other_org' type='checkbox' id='other_org' value='true'>"
End If %>

please help im really stuck .......... your help will be much apprieciated
 
is POSTED a form element, if it is then

it woould be like this

If Request.Form("POSTED") ="true" AND Request.Form("other_lang_chk") = "true
 
Are you getting an error?

What is the value of your form's method property? If it is GET rather than POST then you read the values out of the QueryString instead of the Request.Form.

Also what is POSTED? Is that the name of one of your form elements?
 
I'm guessing POSTED is a hidden form element or an error.
 
posted is my form element .... i get as far as steven has mentioned but my problem is the actual validation once i try to put in the if statement it all goes wrong .... what I want is that if the chkbox is ticked keep it ticked on posted and also chk if there is a value in the textbox and if there isnt return error .........
 
The default value of a ticked checkbox is on so manybe you need to check for "on" rather than "true" ...

We can help more if you show us the code for your HTML form.
 
you could leave it to true if you check box is like this

Code:
<input type="checkbox" name="other_lang_chk" value="true">
 
hey guys i really appriecate this .... do u want to c the entire code in the form or just the form tag? ?
 
<FORM name="mailer" method="post" action="/folder1/page1.asp">
<input type="hidden" name="POSTED" value="true">

and all other dims here
Dim lang_error

If other_lang_chk = "Yes" AND other_lang = "" then
lang_error = "true"
errorMessage = errorMessage & "<p class=red><strong> </strong></p>"
else
errorMessage = errorMessage & "<p class=red><strong>You must enter details of your other language</strong></p>"

End If


<td width="8%">
<% If Request.Form("POSTED") AND Request.Form("other_lang_chk") = "true" Then
Response.Write "<input name='other_lang_chk' type='checkbox' value='true' checked >"
else
Response.write "<input name='other_lang_chk' type='checkbox' value='true'>"
End If %>
</td>


<table width="100%" cellspacing="1" cellpadding="5" align="center" border="0" class="bodycopy1" bgcolor="#339999">
<tr bgcolor="#DDE8EB">
<td width="42%">Other language (please advise)</td>
<td width="58%">

<% If Request.Form("POSTED") <> "true" then
Response.Write "<input name='other_lang' type='text' size='40'>"
elseif Request.Form("POSTED")= "true" AND Request.Form("other_lang_chk") = "true" AND lang_error = "true" then
Response.Write "<input name='other_lang' type='text' size='40' value="& Request.Form ("other_lang") &">"
else
Response.Write "<input name='other_lang' type='text' size='40'>"
end if%>



</form>

theres the main part of my code that im stuck with ...

the error message will not cum up wen the chkbox is ticked and the textfield is blank ....

however using this code .....


<% If Request.Form("POSTED") <> "true" then
Response.Write "<input name='other_lang' type='text' size='40'>"
elseif Request.Form("POSTED") = "true" AND Request.Form("other_lang_chk") = "true" THEN
Response.Write "<input name='other_lang' type='text' size='40' value="& Request.Form ("other_lang") &">"
end if %>

the error msg stays permanently


any1 help please
 
at a glance fix this

Code:
<td width="8%">
            <% If Request.Form("POSTED") AND Request.Form("other_lang_chk") = "true" Then

to

Code:
<td width="8%">
            <% If Request.Form("POSTED") [red]= "true"[/red] AND Request.Form("other_lang_chk") = "true" Then
 
First thing before I stare at the rest of your code...

It looks like your POSTED is hardcoded.
ie: <input type="hidden" name="POSTED" value="true">


If it is hard coded then it will always be true any time the page is submitted... there is no need to check for this.

As long as your submit button has a name, you can just use it instead. That will simplify things a bit.
 
also make sure these variables are set somewhere, i don't see them

other_lang_chk
other_lang
lang_error
 
the variables are in the code just was lazy and didnt copy them .....
 
Are all of these things on one page? Is the file specified by the action property of the <form> the same file that contains the form?
 
yeah ive added it in however the error mesage cums up and stays up still so if a user selects this option then the form will never submit
 
You can simplify by removing the hidden field.

Suppose your submit button looks like this:
<input type="submit" name="POSTED" value="Submit Form">

Then you can still check to see if you are running the page for the first time or as the result of the form submission with something like this:
IF Len(Request.Form("POSTED")) > 0 THEN

which is basically the same thing as this:
IF Request.Form("POSTED") <> "" THEN




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top