1

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;
        }
    }
}
1
  • what is the error? Is it in js functions Commented Jun 17, 2011 at 11:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.