...

View Full Version : Validation for One of Two Text Boxes



synergydata
08-01-2008, 12:10 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 can not control this using the ASP VB2005 requiredfieldvalidator. Someone told me that I should try using JavaScript but I am new to JavaScript. 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.

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

ninnypants
08-01-2008, 12:17 AM
You're going to want to figure out how to do it in ASP because if you don't people can turn off your validation by just turning JavaScript off in their browser, so I would suggest posting this in the ASP forum

synergydata
08-01-2008, 02:00 AM
Thanks for the quick response and guidance.

Philip M
08-01-2008, 03:52 PM
You're going to want to figure out how to do it in ASP because if you don't people can turn off your validation by just turning JavaScript off in their browser, so I would suggest posting this in the ASP forum

Yes, but that is true of any JavaScript validation.
JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation simply by disabling JavaScript.

Here is the JavaScript (client-side)code for what you need:-



<p>Complete only one text box - not both.</p>

<input type = "text" name = "txt1" id = "txt1" size = "20" onblur = "toggle()"><br>
<input type = "text" name = "txt2" id = "txt2" size = "20" onblur = "toggle()"><br>

<script type = "text/javascript">
function toggle() {
document.getElementById("txt1").disabled = false;
document.getElementById("txt2").disabled = false;
var box1 = document.getElementById("txt1").value
var box2 = document.getElementById("txt2").value;
if (box1.length > 0) {
document.getElementById("txt2").disabled = true;
}
if (box2.length > 0) {
document.getElementById("txt1").disabled = true;
}
}
</script>


“In ancient times they had no statistics, so they had to fall back on lies” — Stephen B Leacock



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum