473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

If Other is Selected From a dropdown list make a comment text field required.

I am trying to create a custom validator to validate that if "Other" is selected from a drop downdown list then a textbox is displayed and a comment is required. The display textbox piece is working. I have searched the web and have come up with the following code. However it is not working, Please see the code below, I have broken it up into three sections, 1. The Web Page, 2. The VB Code Behind, 3. the Javascript function.


1. Web Page:

<tr>
<td style="border-bottom: black 1px solid;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td nowrap="nowrap" style="width: 30%"><em class="special">*</em>Operating System:<asp:RequiredFieldValidator ID="RequiredFieldValidator11"
runat="server" ControlToValidate="ddlOpSys" ErrorMessage="Select Operating System"
InitialValue="-1" ForeColor="WhiteSmoke">*</asp:RequiredFieldValidator></td>
<td width="70%"><asp:DropDownList runat="server" ID="ddlOpSys"></asp:DropDownList>
<br />
<asp:Label ID="OtherOpSys_Label" runat="server" width="25px" Text="*Other:"></asp:Label>
<asp:CustomValidator ID="IfOtherValidator" Runat="server" OnServerValidate="IfOtherValidator_ServerValidate" ClientValidationFunction="IfOtherRequiredValidator _ClientValidator">*</asp:CustomValidator>
<asp:TextBox ID="OtherOpSys_Other" runat="server"></asp:TextBox>
</td>
</tr>


2. VB Code behind:

Sub IfOtherValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
'ddlOpSys <> Other or Text <> ""
args.IsValid = (ddlOpSys.SelectedValue <> "Other" Or OtherOpSys_Other.Text <> "")

End Sub


3. Javascript Function:

//this function validates should be text entered in other textbox if other is selected from dropdown lists
function IfOtherRequireValidator_ClientValidate(source, args)
{
if ((document.getElementById(ddlOpSys).options[ddlOpSys.selectedIndex].text) != 'Other') || (document.getElementById(OtherOpSys_Other) != '')) {
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Jul 19 '07 #1
8 3103
gits
5,390 Expert Mod 4TB
hi ...

in your js-validation-function you have to compare the value of the textbox, so

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(OtherOpSys_Other)
  2.  
should be:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('OtherOpSys_Other').value
note: you have to pass the id as a string-value to getElementById so don't forget the quotes ;)

kind regards
Jul 19 '07 #2
hi ...

in your js-validation-function you have to compare the value of the textbox, so

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(OtherOpSys_Other)
  2.  
should be:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('OtherOpSys_Other').value
note: you have to pass the id as a string-value to getElementById so don't forget the quotes ;)

kind regards

I made that change and I still get an error on that line of code we changed. Also my code for displaying the textbox and label if "Other" is selected no longer displays. I am including both functions in this reply. Thank you for your help so far.

//this function displays text box and label if other is selected from dropdown lists
Expand|Select|Wrap|Line Numbers
  1. function displayOther(objElem,strID) {
  2.         var strOtherID = "ctl00_cphMain_"+strID+"_Other"; //default for other textbox name
  3.         var strLabelID = "ctl00_cphMain_"+strID+"_Label";  //default for other label name
  4.  
  5.   if(objElem.options[objElem.selectedIndex].text == "Other") //check for other selection
  6.     {
  7.        document.getElementById(strOtherID).style.display = "inline"; //if other selected show textbox and label
  8.        document.getElementById(strLabelID).style.display = "inline"; 
  9.        document.getElementById(strOtherID).focus();  //set focus in other textbox
  10.  
  11.    }
  12.    else
  13.    {
  14.      document.getElementById(strOtherID).style.display = "none";  //if not other selection hide textbox and label
  15.      document.getElementById(strLabelID).style.display = "none";
  16.  
  17.     }
  18.    }
//this function validates should be text entered in other textbox if other is selected from dropdown lists
Expand|Select|Wrap|Line Numbers
  1. function IfOtherRequireValidator_ClientValidate(source, args) 
  2.  {
  3.   if ((document.getElementById(ddlOpSys).options[ddlOpSys.selectedIndex].text) != 'Other') || (document.getElementById('OtherOpSys_Other').value != '')) {
  4.           args.IsValid = true;
  5.      }
  6.     else 
  7.     {
  8.          args.IsValid = false;
  9.    }
  10.  }
Jul 19 '07 #3
gits
5,390 Expert Mod 4TB
hmmm ... that confuses me a little bit. i assume the js-function is executed at the client ... right? the ids of the elements are 'ddlOpSys' and 'OtherOpSys_Other' ... right too? ... getElementById requires a string as arg so when OtherOpSys_Other and ddlOpSys are no global variables and have the correct ids assigned it shouldn't work, so you have to put them in quotes? what errors do you get?
Jul 19 '07 #4
hmmm ... that confuses me a little bit. i assume the js-function is executed at the client ... right? the ids of the elements are 'ddlOpSys' and 'OtherOpSys_Other' ... right too? ... getElementById requires a string as arg so when OtherOpSys_Other and ddlOpSys are no global variables and have the correct ids assigned it shouldn't work, so you have to put them in quotes? what errors do you get?

I do store my functions in a separate (includes) page, if that's what you mean by global. I am new to developing. I did not have quotes around ddlOpSys so I added that. I also noticed when I was calling the function in my html page I had the function spelled incorrectly, I fixed that also. The error message I get is "invalid syntax for that particular line we have been working with."

Thanks,
Jul 19 '07 #5
gits
5,390 Expert Mod 4TB
hmmm ... let me give you a simple example for the validation you want to do:

[HTML]<html>
<head>
<script>
function validate_textbox() {
var textbox = document.getElementById('textbox_id');
var selection = document.getElementById('selection_id');

if (selection.value == 'other' && textbox.value == '') {
alert('input required');
textbox.focus();
}
}
</script>
</head>
<body>
<select id="selection_id">
<option value="normal">normal opt</option>
<option value="other">other</option>
</select>
<br/>
<input type="text" id="textbox_id"/>
<br/>
<br/>
<input type="button" value="Validate" onclick="validate_textbox();"/>
</body>
</html>
[/HTML]

may be its of some help to you? can you post the html (not the asp-page) ... when loaded to the browser and you go to view the page-source ... we would see the generated html-page ... copy the code and paste it here in case it is not too much code ... otherwise send it to me through a pm-message from your control-panel and i try to extract the relevant things?

kind regards
Jul 19 '07 #6
hmmm ... let me give you a simple example for the validation you want to do:

[HTML]<html>
<head>
<script>
function validate_textbox() {
var textbox = document.getElementById('textbox_id');
var selection = document.getElementById('selection_id');

if (selection.value == 'other' && textbox.value == '') {
alert('input required');
textbox.focus();
}
}
</script>
</head>
<body>
<select id="selection_id">
<option value="normal">normal opt</option>
<option value="other">other</option>
</select>
<br/>
<input type="text" id="textbox_id"/>
<br/>
<br/>
<input type="button" value="Validate" onclick="validate_textbox();"/>
</body>
</html>
[/HTML]

may be its of some help to you? can you post the html (not the asp-page) ... when loaded to the browser and you go to view the page-source ... we would see the generated html-page ... copy the code and paste it here in case it is not too much code ... otherwise send it to me through a pm-message from your control-panel and i try to extract the relevant things?

kind regards
//this function validates should be text entered in other textbox if other is selected from dropdown lists
Expand|Select|Wrap|Line Numbers
  1.  function IfOtherRequireValidator_ClientValidate(source, args) 
  2.  {
  3.  var obj;
  4.  var obj_label;
  5.  obj=document.getElementById("ctl00_cphMain_ddlOpSys");
  6.  obj_label=document.getElementById("ctl00_cphMain_OtherOpSys_Other");
  7.   if ((obj.options[obj.selectedIndex].text != "Other") || (obj_label.text != "")) {
  8.           args.IsValid = true;
  9.      }
  10.     else 
  11.     {
  12.          args.IsValid = false;
  13.    }
  14.  }
I made the following changes to the function and I don't get any syntax errors. When other is selected though it doesn't fire the custom validator. I am going to have to do more research in regards to using the custom validator.

Thank you for all your help.
Jul 20 '07 #7
//this function validates should be text entered in other textbox if other is selected from dropdown lists
Expand|Select|Wrap|Line Numbers
  1.  function IfOtherRequireValidator_ClientValidate(source, args) 
  2.  {
  3.  var obj;
  4.  var obj_label;
  5.  obj=document.getElementById("ctl00_cphMain_ddlOpSys");
  6.  obj_label=document.getElementById("ctl00_cphMain_OtherOpSys_Other");
  7.   if ((obj.options[obj.selectedIndex].text != "Other") || (obj_label.text != "")) {
  8.           args.IsValid = true;
  9.      }
  10.     else 
  11.     {
  12.          args.IsValid = false;
  13.    }
  14.  }
I made the following changes to the function and I don't get any syntax errors. When other is selected though it doesn't fire the custom validator. I am going to have to do more research in regards to using the custom validator.

Thank you for all your help.

I finally got it to work, I have this javascript in my page. Thanks again for all your help. This function is called by the custom validator.


//this function validates should be text entered in other textbox if other is selected from dropdown lists
Expand|Select|Wrap|Line Numbers
  1.  function IfOtherRequireValidator_ClientValidate(source, args) 
  2.  {
  3.   var obj;
  4.   var obj_label;
  5.   obj=document.getElementById("ctl00_cphMain_ddlOpSys");
  6.   obj_label=document.getElementById("ctl00_cphMain_OtherOpSys_Other");
  7.   //
  8.   if (obj.options[obj.selectedIndex].text == "Other") 
  9.   {
  10.     if (obj_label.value == null)
  11.     {
  12.      args.IsValid = false;
  13.     }
  14.     else 
  15.     {
  16.      args.IsValid = true;
  17.    }
  18.  }
  19. }
Jul 20 '07 #8
gits
5,390 Expert Mod 4TB
hi ;)

glad you got it working ... come back every time you have questions ...

kind regards
Jul 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
1
by: Dom Nicholas | last post by:
Hi, I have a table (inside a form) that is dynamically created ie the user can add / remove rows dynamically. Each table row has 4 columns : a selection pull down list a text field for...
8
by: Lyn | last post by:
Hi, Can anyone tell me how the initial value displayed in Combo Box is determined when a form is opened? I am loading the dropdown from one field ("CategoryName") of a table, with "ORDER BY ". ...
0
by: E . | last post by:
I seem to be getting nowhere with this problem here is the situation. I have a webform with a data grid populated with the results of a table that has only 3 fields. Then below that are 1...
1
by: Kenneth Keeley | last post by:
Hi, Is it possible to have a dropdown list that when the page is first visited has a selected value of something like "Choose an Option". After the user has selected an option the the user should...
1
by: namanhvu | last post by:
Hi everyone, I'm trying to create a form where the radio button is automatically selected when the input text field beside it is clicked. I know I need to use "onClick" somewhere but I don't...
2
by: SF | last post by:
Hi, I am new to ASP.NET. I have started to create a new ASP.NET web. I can succesfully insert a table into a form but my form does not look good becuase some filed (from table) are foreigh key...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
2
by: henryrhenryr | last post by:
Hi I have the following query: SELECT user_id_pk, first_name, last_name, email FROM users LEFT JOIN status ON user_id_pk=user_id_fk WHERE live='Y' AND (status=1 OR status IS NULL)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.