i have a button..and i want to use javascript to validate certain fields on the button click and if the validation has passed i need to then execute the code in the code behind..any suggestions please guys!
Thanks in advance.
here is my javascript:
function validateFields() {
function checkCampaignStatus() {
var rdbCampStatus = document.all('<%=rdbCampStatus.ClientID%>');
var radio = rdbCampStatus.getElementsByTagName("input");
var isChecked = false;
for (var i = 0; i < radio.length; i++)
{
if (radio[i].checked)
{
isChecked = true;
break;
}
}
if (!isChecked)
{
showOk('Save/Close', 'Please fill in all mandatory fields');
document.getElementById('popupControl').style.display = 'block';
document.getElementById('divEntryMain').style.display = 'none';
document.getElementById('divBuilderMain').style.display = 'none';
return false;
}
return true;
}
function checkCreatedBy() {
var createdBy = document.all('<%=txtCreatedBy.ClientID%>').value;
if (createdBy == "")
{
return false;
}
}开发者_如何学Python
}
OnClientClick of the button call the validation function. If the validations are ok , return true else return false. By returning true if the validations are ok it will call the server side method.
Could you please try this:
function validateFields() {
return (checkCampaignStatus() && checkCreatedBy());
}
function checkCampaignStatus() {
var rdbCampStatus = document.all('<%=rdbCampStatus.ClientID%>');
var radio = rdbCampStatus.getElementsByTagName("input");
var isChecked = false;
for (var i = 0; i < radio.length; i++)
{
if (radio[i].checked)
{
isChecked = true;
break;
}
}
if (!isChecked)
{
showOk('Save/Close', 'Please fill in all mandatory fields');
document.getElementById('popupControl').style.display = 'block';
document.getElementById('divEntryMain').style.display = 'none';
document.getElementById('divBuilderMain').style.display = 'none';
return false;
}
return true;
}
function checkCreatedBy() {
var createdBy = document.all('<%=txtCreatedBy.ClientID%>').value;
return (createdBy == "");
}
HTH
Aspx section:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return javascriptFunction()" OnClick"Button_Click"/>
JavaScript section:
function javascriptFunction()
{
//Get the asp.net server controls using the following code
var textbox=document.getElementById('<%=txtID.ClientID %>');
var label=document.getElementById('<%=lblID.ClientID %>');
if(textbox.value=="")
{
label.innerHTML="this is a required field";
return false;// This will stop at client side and skip server side execution
}
else
{
return true;//This will execute the server side event
}
}
hope this helps...
精彩评论