ValidationGroup in ASP.NET - javascript

I have this much
<asp:Button ID="btnSaveContest" runat="server" Text="Save & Publish Contest" OnClientClick="javascript:changeInputTexts(); return disableAfterClick();"
ValidationGroup="ContestAdd" OnClick="btnSaveContest_Click" />
Now I want to call disableAfterClick() after all validations are completed. It should be in Client Side. So that I can ensure that the user can click the button only one time.

function ValidateCreateContest() {
Page_ClientValidate();
if (Page_IsValid) {
$('#<%=btnSaveContest.ClientID%>').attr('disabled', 'disabled');
return true;
}
else {
return false;
}
}`

Related

ASP Web Forms fire code behind event on condition

I have ASP.NET WEB FORMS apps
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" />
function btnDeleteClick() {
var result = confirm("Want to delete all Deals?");
if (result) {
return true;
} else {
return false;
}
}
$("#MainContent_DeleteDealsButton").on("click", function() {
if (btnDeleteClick()) {})
On this button I have sample javascript alert to ask OK or Cancel.I want if choise OK to fire CODE BEHIND method call DeleteDealsButton_Click, if it Cancel to do nothing.
Just change button defination to,
<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" OnClientClick="return confirm('Want to delete all Deals?')" />
No need for any javascript function.
try like this:
function btnDeleteClick() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
confirm_value.value = "No";
var result = confirm("Want to delete all Deals?");
if (result) {
confirm_value.value = "Yes";
__doPostBack('DeleteDealsButton', '')
return true;
} else {
confirm_value.value = "No";
return false;
}
}
In your .Aspx file Change your button code to like this:
<asp:Button ID="DeleteDealsButton" OnClientClick=" return btnDeleteClick()" runat="server" Text="Delete" OnClick="DeleteDealsButton_Click" />
In your .Aspx.cs file
protected void DeleteDealsButton_Click(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (!String.IsNullOrWhiteSpace(confirmValue) && confirmValue.Equals("Yes"))
{
}
}

Client side custom validation with radiobuttons

Can I have an example code for this? Two radio buttons in a group name "Gender". If the user selects nothing, a message should appear saying "select one". I've done some custom validations with textboxes and dropdownlists and I became stuck with radiobuttons.
$("form").submit(function(e){
e.preventDefault();
if($(this).find("input[name=Gender]:checked").length === 0){
alert('Gender not selected.');
}
});
you can also use val() of selected radio group to check if it's undefined or a value.
jsfiddle: http://jsfiddle.net/jh8p3/
client side validation in asp.net
<asp:RadioButtonList ID="Gender" runat="server">
<asp:ListItem Text="Male" Value="Male" />
<asp:ListItem Text="Female" Value="Female" />
</asp:RadioButtonList>
<asp:Button ID="Button1"
Text="Validate"
runat="server" OnClientClick="return validate();" />
<script>
function validate() {
if (checkGender()) {
return true;
}
return false;
}
function checkGender() {
var selectedGenderRB = document.querySelector('#<%=Gender.ClientID%> input:checked');
if (selectedGenderRB) {
return true;
} else {
alert('gender not selected.');
return false;
}
}
</script>
how about a lil' fiddle
http://jsfiddle.net/bTf2R/
$('input[type=radio]').change(function(){
$('p').text('Thanks');
});

JavaScript not firing in ASP.NET

function isGoodEmail() {
var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
if (email == "Email") {
if (window.isValidEmail(email)) {
if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
alert(' valid email, but not for this site. No free service emails!');
return false;
}
return true;
}
return false;
}
Button code:
<asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
ID="Companystart" OnClick="CompanystartClick" OnClientClick="isGoodEmail" />
I am calling the above JavaScript from the button, but it is not validating the email. Even if entered gmail it is accepted.
use the following
<asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
ID="Companystart" OnClick="CompanystartClick" OnClientClick="return isGoodEmail()" />
Your are missing 'return' before your client click function call OnClientClick="return isGoodEmail()"
As you have written return true and false in your java-script so in your function call too there should be return before the function name.
For better understanding of return true and false you can go through this link
What's the effect of adding 'return false' to a click event listener?
---EDIT
function isGoodEmail() {
alert("In the beginning");
var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
alert(email);
if (email == "Email") {
if (window.isValidEmail(email)) {
if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
alert(' valid email, but not for this site. No free service emails!');
return false;
}
return true;
}
return false;
}
If you put OnClientClick="return isGoodEmail()" it will work it invokes JavaScript code.
but suppose you put "gmail" as text for email, then the if condition
if (email == "Email")
is false so nothing is executed inside or returned
hence it feels like JavaScript is not invoked
try using firebug to debug and break-point js code

Want to change image src of ImageButton using javascript on client click

I tried this first which is not working:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="minus.gif" OnClientClick="this.src='plus.gif';"/>
Another method:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="minus.gif" OnClientClick=" return changeImg(this)"/>
function changeImg(cnt)
{
if(cnt.src='minus.gif')
{
cnt.src='plus.gif';
}
else
{
if(cnt.src='plus.gif')
{
cnt.src='minus.gif';
}
}
return false;
}
</script>
The issue here is that you do not return false, on client click and you trigger the onlick on code behind. Just return false; to avoid the post back and you get what you try.
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="minus.gif" OnClientClick="this.src='plus.gif';return false;"/>
update
On your code you type if(cnt.src='plus.gif'), but you must type == , not =
To avoid this type of error is better to place first the const, eg type
if('plus.gif' == cnt.src)
and the final code
function changeImg(cnt)
{
if(endsWith(cnt.src, 'minus.gif'))
{
cnt.src='plus.gif';
}
else if(endsWith(cnt.src, 'plus.gif'))
{
cnt.src='minus.gif';
}
// to avoid post back return false
return false;
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
relative : endsWith in JavaScript
The following should work
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="minus.gif" onClick="function(){this.setAttribute("src", 'plus.gif');}"/>

Validating with JavaScript Function

I Have The Following JavaScript Function:
function validateLocality(object, args) {
if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0") {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
and the following drop down list with validator:
<asp:DropDownList
ID="ddlLocality"
runat="server"
DataSourceID="DataSourceListTowns"
DataTextField="town_name"
DataValueField="town_id"
ToolTip="The Locality Where the User Lives"
AppendDataBoundItems="True"
ViewStateMode="Disabled">
<asp:ListItem Value="0">Select Locality</asp:ListItem></asp:DropDownList>
<asp:CustomValidator
runat="server"
ControlToValidate="ddlLocality"
ErrorMessage="Select Locality"
ToolTip="Select Locality"
ID="validateLocality"
ClientValidationFunction="validateLocality()">*</asp:CustomValidator>
The Thing Is It is not validating the drop-down at all.
Thanks For Any Help Cause I wrecked My Brain about this
The ClientValidationFunction should carry only the name of the function. It is not expecting to carrry a Javascript expression. Hence your attribute should look like: ClientValidationFunction="validateLocality" note no ().
Your function is taking 2 arguments:
function validateLocality(object, args)
But when you call it, you aren't passing any in..?
ClientValidationFunction="validateLocality()"
Add an alert to your function to make sure it's being called on the submit.
function validateLocality(object, args)
{
alert("working");
if (document.getElementById("<%=ddlLocality.ClientID %>").value == "0")
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

Categories