PDA

View Full Version : Validation for One of Two Text Boxes


synergydata
08-01-2008, 03:04 AM
I have created a web page with a lot of required text boxes, but I need to ensure that the user completes one of two of the final text boxes but not both. I have used ASP VB2005 requiredfieldvalidator to required the other fields and have tried some other VB methods but its not working out. I was told that JavaScript wouldn't work as someone may just turn it off Java on their side.

I have posted the code below to give some insight into what I have and what I'm trying to accomplish. I would like these two text boxes checked, error thrown if neither is completed and after completion post the data. Hope I have provide enough information. The two fields only one of which is required to be complete are Job_Number and Fund.

Please provide corrected or new code and where to place it.

Thanks

ASPX Page showing some of the fields using RequiredFieldValidators and the two other two fields of which only one of the two must contain an entry.

<td valign="top" style="width: 173px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator14" runat="server" validationexpression="^[A-Z0-9\s]{4,4}$" ErrorMessage="Enter Letters in UPPER Case and the entry must be 4 Characters in Length." ControlToValidate="FUND" Display="Dynamic" CssClass="error"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator17" Enabled="false" runat="server" ErrorMessage="Required Entry" ControlToValidate="FUND" Display="Dynamic" CssClass="error" Font-Size="Small" />
<asp:TextBox ID="Fund" MaxLength="4" runat="server" ToolTip="Enter the Four (4) Character Fund. Example: CNCN"></asp:TextBox></td>

<td valign="top" style="width: 173px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator15" runat="server" validationexpression="^[A-Z0-9\s]{4,4}$" ErrorMessage="Enter Letters in UPPER Case and the entry must be 4 Characters in Length." ControlToValidate="PGMT" Display="Dynamic" CssClass="error"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator18" Enabled="false" runat="server" ErrorMessage="Required Entry" ControlToValidate="PGMT" Display="Dynamic" CssClass="error" Font-Size="Small" />
<asp:TextBox ID="PGMT" MaxLength="4" runat="server" Wrap="False" ToolTip="Enter the Four (4) Character Program. Example: CNES"></asp:TextBox></td>

<td valign="top" style="width: 173px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator16" runat="server" validationexpression="^(-)?\d+(\.\d\d)?$" ErrorMessage="Enter the Quanity \ Hours associated with this transaction." ControlToValidate="HOURS" Display="Dynamic" CssClass="error"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator19" Enabled="false" runat="server" ErrorMessage="Required Entry" ControlToValidate="HOURS" Display="Dynamic" CssClass="error" Font-Size="Small" />
<asp:TextBox ID="HOURS" runat="server" Wrap="False" ToolTip="Enter the Quanity \ Hours associated with this transaction."></asp:TextBox></td>

<td valign="top" style="width: 173px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator17" runat="server" validationexpression="^[A-Z0-9]{4,4}$" ErrorMessage="Enter Letters in UPPER Case and the entry must be 4 Characters in Length. Example X801" ControlToValidate="RPTG" Display="Dynamic" CssClass="error"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator20" Enabled="false" runat="server" ErrorMessage="Required Entry" ControlToValidate="RPTG" Display="Dynamic" CssClass="error" Font-Size="Small" />
<asp:TextBox ID="RPTG" runat="server" ToolTip="Enter the Four (4) Character Reporting Group Category. Example: X801"></asp:TextBox></td>

<td valign="top" style="width: 173px">
<asp:RegularExpressionValidator ID="RegularExpressionValidator18" runat="server" validationexpression="^(-)?\d+(\.\d\d)?$" ErrorMessage="Entry must contain a decimal + 00 and no $ sign. Example: 100.13 0r -100.13" ControlToValidate="LINE_AMOUNT" Display="Dynamic" CssClass="error"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server" ErrorMessage="Required Entry" ControlToValidate="LINE_AMOUNT" Display="Dynamic" CssClass="error" Font-Size="Small" />
<asp:TextBox ID="Line_Amount" runat="server" Wrap="False" ToolTip="Enter the Line Item Amount without a $ sign. This amount can be positive or negative. For negative enter as -100.13."></asp:TextBox></td>
</tr>
</table>
</div>

Code Behind Page showing the action of the Submit Button:

Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitBtn.Click

Dim AAID As Integer
Dim AAInfo As New AAHeader
Dim AAHeaderID As Integer
Dim AADetailID As Integer
Dim AADetail As New AADetail
Dim sessAAID As Integer

'--- SET UP SESSION ITEMS ---'
Session.Item("Requestor_LName") = Requestor_LName.Text
Session.Item("Requestor_FName") = Requestor_FName.Text
Session.Item("Docload_Sec1") = Docload_Sec1.SelectedValue
Session.Item("RACA_Team") = RACA_Team.SelectedValue
Session.Item("Requestor_Phone") = Requestor_Phone.Text
Session.Item("RRUU") = RRUU.Text
Session.Item("Header_Description") = Header_Description.Text
Session.Item("Fiscal_Month") = Fiscal_Month.Text
Session.Item("Fiscal_Year") = Fiscal_Year.Text
Session.Item("Field_Approver_Name") = Field_Approver_Name.Text
Session.Item("Field_Approved_Date") = Field_Approved_Date.Text
Session.Item("Email_Address_of_Submitter") = userid.Text

'--- INSERT AA_HEADER INFORMATION INTO TABLE AND GET THE ID ---'
AAInfo.Requestor_LName = Requestor_LName.Text
AAInfo.Requestor_FName = Requestor_FName.Text
AAInfo.Requestor_Phone = Requestor_Phone.Text
AAInfo.RRUU = RRUU.Text
AAInfo.Field_Approver_Name = Field_Approver_Name.Text
AAInfo.Field_Approved_Date = Field_Approved_Date.Text
AAInfo.Email_Address_of_Submitter = userid.Text
AAInfo.Date_Submitted = Now()

'--- INSERT AA_DETAIL INFORMATION INTO TABLE AND GET THE ID ---'
AADetail.DOCLOAD_SEC1 = Docload_Sec1.SelectedValue
AADetail.RACA_Team = RACA_Team.SelectedValue
AADetail.BV_Date = Now()
AADetail.Header_Description = Header_Description.Text
AADetail.Fiscal_Month = Fiscal_Month.Text
AADetail.Fiscal_Year = Fiscal_Year.Text
AADetail.Line_No = ""
AADetail.Trans_Code = "BV" 'Trans_Code.SelectedValue
AADetail.Trans_Type = Trans_Type.SelectedValue
AADetail.Beg_BFY = Beg_BFY.Text
AADetail.Orgn = Orgn.Text
AADetail.Job_Number = Job_Number.Text
AADetail.Fund = Fund.Text
AADetail.PGMT = PGMT.Text
AADetail.Obj_Rev_Srce = Obj_Rev_Srce.Text
AADetail.Hours = HOURS.Text
AADetail.RPTG = RPTG.Text
AADetail.Line_Amount = Line_Amount.Text
AADetail.DOCUMENT_TOTAL = Line_Amount.Text
AADetail.Agreement_No = AGREEMENT_NO.Text
AADetail.Advance_FL = ADVANCE_FL.Text
AADetail.Vendor_ID = Vendor_ID.Text
AADetail.Line_Description = Line_Description.Text
AADetail.Email_Address_of_Submitter = userid.Text
AADetail.Date_Submitted = Now()

'--- COMPLETES THE HDR_EXP_REV_GL_IND BASED ON THE TRANS TYPE ---'
If AADetail.Trans_Type = "AC" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "AD" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "AV" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "CU" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "FG" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "FI" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "GA" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "NP" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "PJ" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "R3" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "E"
ElseIf AADetail.Trans_Type = "RC" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "RE" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "RR" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "RT" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "R"
ElseIf AADetail.Trans_Type = "SU" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "G"
ElseIf AADetail.Trans_Type = "TO" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "E"
ElseIf AADetail.Trans_Type = "TW" Then
AADetail.Hdr_Exp_Rev_Gl_Ind = "E"
End If

If CStr(Session.Item("aaid")) = "" Then
'this is a first round, so insert into 1st table and second table by:
'call the two functions which do the inserts.
AAID = InsertAAHeader(AAInfo)
AADetail.AAID = AAID
AADetailID = InsertAAData(AADetail)
AAHeaderID = CStr(AAHeaderID)
sessAAID = CStr(AAID)
Session.Item("aaid") = sessAAID
'--- Send the ID from the first table insert to a hidden text box.
AAIDLabel.Text = sessAAID
AAIDLabel.Visible = True
Else
'The upper table has been inserted to, so insert only into 2nd table by:
'Call only 2nd function
AADetail.AAID = Session.Item("aaid")
AADetailID = InsertAAData(AADetail)
End If

'--- Troubleshooting the error in ID's
'--- Response.Write(Session.Item("aaid"))
'--- Session.Item("aaid") = sessAAID
'--- Response.Write(Session.Item("aaid"))

'--- CLEAR DATA FIELDs ---'
'Session.Item("LINE_NO") = Nothing
Session.Item("BEG_BFY") = Nothing
Session.Item("ORGN") = Nothing
Session.Item("JOB_NUMBER") = Nothing
Session.Item("FUND") = Nothing
Session.Item("PGMT") = Nothing
Session.Item("OBJ_REV_SRCE") = Nothing
Session.Item("HOURS") = Nothing
Session.Item("PRTG") = Nothing
Session.Item("LINE_AMOUNT") = Nothing

'Line_No.Text = ""
Beg_BFY.Text = ""
Orgn.Text = ""
Job_Number.Text = ""
Fund.Text = ""
PGMT.Text = ""
Obj_Rev_Srce.Text = ""
HOURS.Text = ""
RPTG.Text = ""
Line_Amount.Text = ""

GridView1.DataBind()

End Sub

SWI03
08-01-2008, 04:53 AM
Dim strtextbox1 as string = Trim(textbox1.Text)
Dim strtextbox2 as string = Trim(textbox2.Text)

Try
If strtextbox1 is nothing and strtextbox2 is nothing then
lbltextboxerror.ForeColor = = Drawing.Color.Red
lbltextboxerror.Text = "Please fill in one of the textboxes"
Else
(all of your code here.)
End If
Catch ex ex As Exception
(error handling code here)
End Try

Freon22
08-01-2008, 03:35 PM
One thing to keep in mind is the asp validator controls are javascript. The asp engine writes the javascript for you. Anytime you are validating controls on the clientside you are doing it with javascript. You can test these textboxes by validating them on the server side but that will call for a postback or you can run a test to see if the client has javascript enabled.

If they don't have javascript enabled then tell them they are required to enable javascript. As for me I require javascript to be enabled if not they can not go anywhere on my site. But I still do some serverside validating because anything on the clientside can be messed with.

Here are some links on checking to see if javascript in enabled.
http://www.4guysfromrolla.com/webtech/082400-1.shtml
http://aspadvice.com/blogs/azamsharp/archive/2007/09/30/Check-If-the-JavaScript-is-Enabled-on-the-Client_2700_s-Browser.aspx

synergydata
08-01-2008, 05:07 PM
Thanks for the quick response and advice. I just added all the code per your instructions but its still not stopping or firing an error. Any other idea. I can re-post all the code with your modifications if you would like.

synergydata
08-01-2008, 05:08 PM
Thanks for the for the added advice Freon22, I will review those sites.