Page_ClientValidate('') can i do this at a control level - javascript

Does anyone know the way i can do similar to Page_ClientValidate in a way that not all of the controls on the page will be validated against? i.e. how i can just validate a particular custom validator is valid?

You can specify a ValidationGroup to group your controls. Then specify that string to the Page_ClientValidate function like so
<asp:TextBox id="text" runat="server" ValidationGroup="vld1" />
<asp:CheckBox id="check" runat="server" ValidationGroup="vld2" />
Page_ClientValidate('vld1');

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" />

Server tag not evaluated in server control

On my ASPX page, I need a server control checkbox to pass a backend variable as a parameter in its onclick JS function. But it doesn't seem to work as expected.
Please refer the two checkboxes below:
<input type="checkbox" id="Checkbox1" onclick="ToggleMyOnly('<%=gsListID %>');" />
<input type="checkbox" id="Checkbox2" runat="server" onclick="ToggleMyOnly('<%=gsListID %>');" />
Here the Checkbox1 evaluates the value of gsListID as expected. However, Checkbox2 just passes it as is.
The only difference between these two controls is that Checkbox2 is a server control.
I have searched for a solution for this issue across many sites, but did not get an answer.
I also tried converting the Checkbox1 to an ASP checkbox as follows:
<asp:CheckBox ID="CheckboxASP" runat="server" Text="test" onclick="ToggleMyOnly('<%=gsListID %>');" />
But this also did not evaluate the server tag.
Can anyone help me know how to use the server tag in "onclick" for a server control input element? (Avoiding an ASP:checkbox preferred).
You should write like below:
<asp:CheckBox runat="server" ID="chk" Text="Test" onchange="return ToggleMyOnly('<%=gsListID %>');" />
if you dont want to postback then return false from ToggleMyOnly function.
It's working from myside.

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:

ASPNET get ClientID in onchange event using Javascript

I use:
onchange="alert('<%= AbcControl.ClientID %>')"
but Unfortunately it will translate to:
onchange="alert('<%= AbcControl.ClientID %>')"
May I know what is the best solution for this other than adding the onchange even during server side Page_Load even?
Thanks in Advance.
You can use this.id here
onchange="alert(this.id);"
Try this, if you want to see only Id (Client side generated id) of the AbcControl
<asp:TextBox runat="server" ID="AbcControl" />
<input type="text" onchange='<%= "alert('"+ AbcControl.ClientID +"');" %>' />
OR
Try this, if you want to get the client side object of AbcControl
<asp:TextBox runat="server" ID="AbcControl" />
<input type="text" onchange='<%= "alert("+ AbcControl.ClientID +");" %>' />

How disable RequiredFieldValidator in script

I have TextBox with RequiredFieldValidator on my page.
I also have link that calls some simple javascript.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" />
<asp:LinkButton ID="Link1" runat="server" OnClientClick="DoSomething(); return false;" CausesValidation="false"Text="Do" />
function DoSomething() {
textbox1.val("blah"); }
When user type something into textbox and then delete that text and focus next control - then validator fires.
Then user can use link that adds text using javascript. TextBox1 text is no longer empty but RequiredFieldValidator still shows error message. How to prevent that?
you can use javascript ValidatorEnable function
ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
I would recommend a CustomValidator here since you do NOT want the normal RequiredFieldValidator behavior. The CustomValidator client method will only run on postback.
The whole point of the validators is that they are to be used for both server side and client side validation.
If you only want to validate on the server side, don't use a validator and simply check that the values are valid and report an error.

Categories