how to only validate string length if checkbox is checked in c# - javascript

on my system, users have the option to "upload a doc file", "write one now" or "dont use the doc file".
I would like to validate a strng minimum using data annotations to validate the text area of "write one now", however the validation can only be triggered if "write one now" is checked.
Can this be achieved using data annotations or only with javascript?

I have one suggestion for this, use radio buttons to let user select only one, on clicking that, see if "write a row" is selected, then make the textbox or textarea as editable/ user can enter text , else let that textarea remain "Readonly". I have used this code, it is working.
<asp:RadioButton ID="rad1" runat="server" Text="upload a doc file" GroupName="vald" onchange="Check();"/>
<asp:RadioButton ID="rad2" runat="server" Text="write one now" GroupName="vald" onchange="Check();"/>
<asp:RadioButton ID="rad3" runat="server" Text="dont use the doc file" GroupName="vald" onchange="Check();"/>
<asp:TextBox TextMode="MultiLine" ID="txt" runat="server" ReadOnly="true"/>
<script type="text/javascript">
function Check()
{
if (document.getElementById("rad2").checked)
{
document.getElementById("txt").removeAttribute("readonly");
}
else
{
document.getElementById("txt").readonly = true;
}
}
</script>

Related

can a required field validator can have two validation groups?

I am working on a web page.
In that web page there are 4 text boxes and each one has required field validator.
The web page has 3 buttons.
on clicking on 1st button 1st & 2nd required field validations should fire.
and on 2nd button 2nd and 3rd required field validator should fire
just like this on 3rd button 3rd and 4th should fire.
The problem is , since the one required field validator is invoked with two buttons i cant have a validation group name .
can anybody please suggest a solution .
it will be really helpful.
You can use JS, where you validate specific groups. For each button create separate validation method.
<script type="text/javascript">
function validateGroup12() {
var g1 = Page_ClientValidate("vgroup1");
var g2 = Page_ClientValidate("vgroup2");
if (!g1 || !g2) return false;
return true;
}
</script>
.aspx edit RequiredFieldValidator and set groups name into ValidationGroup
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ValidationGroup="vgroup1,vgroup2" Display="Dynamic">
Have two validators, instead of one validator, for Textbox 2 and Textbox 3 with the validation groups properly set to the corresponding button validation group.
Even though this is going to duplicate the validator, this simplifies your logic.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ValidationGroup="vgroup1" Display="Dynamic">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ValidationGroup="vgroup1" Display="Dynamic">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox2" ValidationGroup="vgroup2" Display="Dynamic">
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="vgroup1" />
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="vgroup2" />

How to make lebel Invisible / Visible based on the Input given in the Search TextBox?

I'm searching names using a text box which are auto fillable. When I enter the name other than the list of auto fillable one's, I need to display a label dynamically like "Please Enter a Valid Name" like that. If I remove my wrong entry in the test box, then label should be invisible.
<asp:TextBox ID="tbName" caption="Enter Name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="UIRequiredFieldValidatorName"
ControlToValidate="tbName" />
<asp:Button ID="UIButtonGo" runat="server" Text="Search" />
<asp:Label ID="UILabelSearch" runat="server"></asp:Label>
</p>
Please let me know the possible ways for this one. Thanks
You should try to do this in JavaScript. Handle the onchange event and check textbox value against the auto fillable values. In your code add tbName.properties.add("onchange","validatorMethod(this);");. In JavaScript file add:

Text attribute of an asp label using JavaScript is not staying changed

I have a button:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick()"/>
that runs the following JavaScript function when it gets clicked:
function ResetClick() {
document.getElementById("<%=lblError.ClientId%>").innerHTML = "";
}
That changes the text on this label:
<asp:Label ID="lblError" runat="server" Style="color: Red;" Text=""></asp:Label>
Unfortunately, my code behind also changes the label text to an error message using the following code:
lblError.Text = "No data is available"
When I click the reset button, the text is momentarily cleared and then it fills again with the error message. Is there any way to have the code behind set a message in the label and let the JavaScript clear the message?
If you do not want the button to post back then you should add return false; to the OnClientClick attribute:
<asp:Button Text="Reset" runat="server" ID="ResetButton" OnClientClick="ResetClick(); return false;"/>
PS: adding as an answer since it sounds like it was helpful.

How to disable image button onlclick in asp.net?

I have a GridView in this GridView there is a column which I use to select date with the use of asp:CalendarExtender. I have multiple rows in GridView which are dynamically generated.
When I click the image button calender pops up and I choose my date.
In some rows I want the User to select date when he clicks image button of that row. sometime I don't want to allow him to select date.
When he tries to change the date or click on the image button, he should get alert message saying that you are not allowed to change the date.
I want to handle it from client side. So please suggest me how to do this.
this is the aspx code.
<asp:TextBox runat="server" ID="StartDateTime" CssClass="search_area_text_vm_small" onclick = "javascript:DateClick(this.id);"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/image3.jpg" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDateTime" PopupButtonID="Image1" Format="yyyy-MM-dd" ></asp:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="StartDateTime"
ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
You can make use of the OnClientDateSelectionChanged event of the CalendarExtender.
Use below code:
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDateTime" PopupButtonID="Image1" Format="yyyy-MM-dd" OnClientDateSelectionChanged="checkIfDateSelected()" >
</asp:CalendarExtender>
<javascript>
function checkIfDateSelected(){
var dateTextBox=document.getElementByid('StartDateTime');
if(dateTextBox!='')
{
alert ('Date once selected cannot be changed');
return false;
}
</javascript>
This will show the info pop up and prevent the form submission if the date has been selected already.

do postback after enter hit on changing text in textbox

How can I make postback after user changes textbox text and hits enter ?
Is only javascript real solution. Doesnt asp.net give any out from box solution for such a problem ?
Thank You for any hints.
Beside the javascript solutions, you can place the textbox with a submit button inside a panel and make use of DefaultButton property of the panel.
Below is the code example: -
Your aspx will look like this
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
Now, when you press enter inside the text box the button click event occurs, same as by clicking the button.

Categories