using javascript in asp.net 2.0 c# -- OnClientClick

asp.net page
<script src="includes/form.js" language="javascript" type="text/javascript"></script>
</head>
<body onLoad="return textfocus()">
<form id="frmCCDetails" name="frmCCDetails" runat="server">
<asp:TextBox ID="txtCustName" runat="server" TabIndex="4" MaxLength="25"></asp:TextBox>
<br />
<asp:Button CssClass="formbtn" ID="cmdCheckIsValid" runat="server" Text="Validate"
  TabIndex="23" OnClientClick="cmdCheckIsValid_onclick(document.frmCCDetails)" />
</form>


form.js
function cmdCheckIsValid_onclick(theForm)
{
  if (theForm.txtCustName.value == "")
    {
          alert("Please enter a value for the \"Customer Name\" field.");
          theForm.txtCustName.focus();
          return false;
    }
  else if (theForm.txtCardHoldAddr.value=="")
      {
      alert("Please enter a value for the \"Address\" field.");
      theForm.txtCardHoldAddr.focus();
      }
  return true;
}

When I click the validate button the js works (prompts and places focus)
but the form continues as though it is being processed. How do I get all
action to stop and not allow the form to continue until the validation
is complete?
fwstealAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alfredwhangCommented:
looks like you are missing the "return false;"  in the "else if (theForm.txtCardHoldAddr.value=="")" ???
0
fwstealAuthor Commented:
i've got that in there
0
alfredwhangCommented:
try one thing...put a "return false;" in the beginning to short circuit the whole thing and see what happens.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

alfredwhangCommented:
0
alfredwhangCommented:
sorry wrong post
0
fwstealAuthor Commented:
no worries; did as suggestion - sorry
0
fwstealAuthor Commented:
here is the entire js file:
-----------------------------------
// JScript File

//set form focus
function textfocus() {
  document.all('txtAuthName').focus();
  document.all('txtAuthName').select();
return false;
}

//
function cmdCheckIsValid_onclick(theForm)
{
      var isValid = true;
      var validator = new Validate();
      var selectedMonth = theForm.ddlMonth.options[theForm.ddlMonth.selectedIndex].value;
      var selectedYear = theForm.ddlYear.options[theForm.ddlYear.selectedIndex].value;
      
      if (theForm.txtCustName.value == "")
    {
          alert("Please enter a value for the \"Customer Name\" field.");
          theForm.txtCustName.focus();
          return false;
    }
      else if (theForm.txtCardHoldAddr.value=="")
      {
      alert("Please enter a value for the \"Address\" field.");
      theForm.txtCardHoldAddr.focus();
      return false;
      }
      else if (theForm.txtCardHoldCity.value=="")
      {
      alert("Please enter a value for the \"City\" field.");
      theForm.txtCardHoldCity.focus();
      return false;
      }
      else if (theForm.txtCardHoldZip.value=="")
      {
      alert("Please enter a value for the \"Zip\" field.");
      theForm.txtCardHoldZip.focus();
      return false;
      }
      else if (theForm.txtInvNum1.value=="")
      {
      alert("Please enter a value for the \"Invoice Number\" field.");
      theForm.txtInvNum1.focus();
      return false;
      }
      else if (theForm.txtAmtNum1.value=="")
      {
      alert("Please enter a value for the \"Amount\" field.");
      theForm.txtAmtNum1.focus();
      return false;
      }
      else if (theForm.txtCCNum.value=="")
      {
      alert("Please enter a value for the \"Card Number\" field.");
      theForm.txtCCNum.focus();
      return false;
      }
      else if (theForm.txtCCHolderName.value=="")
      {
      alert("Please enter a value for the \"Card Holder\" field.");
      theForm.txtCCHolderName.focus();
      return false;
      }
      else if (validator.isValidCreditCardExpiry(selectedMonth,selectedYear) == false)
      {
         isValid = false;
         alert("Invalid Expiry Date of " + selectedMonth + ", " + selectedYear);
         return false;
      }
          else
          {
              var cardType = getSelectedRadioValue(theForm.rblTypeCCard);
              var cardNumber = theForm.txtCCNum.value;
              isValid = validator.isValidCreditCardNumber(cardNumber, cardType)
              if (!isValid)
                 {
                    alert("Invalid Card Number"); //problem here - wants to continue processing form when
                        //I enter incorrect data such as letters
                    theForm.txtCCNum.focus();
                       return false;
                 }
             else
                 {
                    var userInput = confirm ("Thank You, all of your information is correct. You may now submit your payment.");
                    if (userInput == true)
                    {
                    javascript:toggleT('divt1','s')
                    return true;
                 }
            else
                {
                alert ("We're sorry, your form was not processed!");
                return false;
                }
          }
      }
      return true;
}

//
function Validate() {}
//This function validates the expiration date of the credit card first followed by
Validate.prototype.isValidCreditCardExpiry = function(selectedMonth, selectedYear)
    {
          var isValid = true;
          var nowDate = new Date();
          if ((selectedMonth < (nowDate.getMonth() + 1) && selectedYear <=
                nowDate.getFullYear())  )
          {
                isValid = false;
          }
          else if ((selectedYear < nowDate.getFullYear()))
          {
                isValid = false;
          }
          return isValid;
    }

Validate.prototype.isValidCreditCardNumber = function(cardNumber, cardType)
    {
          var isValid = false;
          var ccCheckRegExp = /[^\d]/;
          isValid = !ccCheckRegExp.test(cardNumber);

          if (isValid)
          {
                var cardNumberOnly = cardNumber.replace(/ /g, "");
                var cardNumberLength = cardNumberOnly.length;
                var lengthIsValid = false;
                var prefixIsValid = false;
                var prefixRegExp;

              switch (cardType)
                {
                    case "2":
                          lengthIsValid = (cardNumberLength == 16);
                          prefixRegExp = /^5[1-5]/;
                          break;

                    case "1":
                          lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
                          prefixRegExp = /^4/;
                          break;

                    case "3":
                          lengthIsValid = (cardNumberLength == 15);
                          prefixRegExp = /^3/;
                          break;

                    case "4":
                          lengthIsValid = (cardNumberLength == 16);
                          prefixRegExp = /^6/;
                          break;

                    default:
                          prefixRegExp = /^$/;
                          alert ("Please Select Card Type");
                }
          prefixIsValid = prefixRegExp.test (cardNumberOnly);
          isValid = prefixIsValid && lengthIsValid;
          }

      
          if (isValid && (cardType != 'amex'))
          {
             var numberProduct;
             var numberProductDigitIndex;
             var checkSumTotal = 0;
             for (var digitCounter = cardNumberLength -1; digitCounter > 0; digitCounter--)
             {
                checkSumTotal += parseInt(cardNumberOnly.charAt(digitCounter));
                digitCounter--;
                numberProduct = new String((cardNumberOnly.charAt(digitCounter) * 2));
                for (var productDigitCounter = 0; productDigitCounter < numberProduct.length;
                      productDigitCounter++)
                {
                   checkSumTotal +=
                   parseInt(numberProduct.charAt(productDigitCounter));
                }      
              }
              isValid = (checkSumTotal % 10 == 0);
           }
      return isValid;
}

//
function getSelectedRadioValue(radioGroup)
    {
          var selectedRadioValue = "";
          var radIndex;
          for (radIndex = 0; radIndex < radioGroup.length; radIndex++)
          {
              if (radioGroup[radIndex].checked)
              {
                  selectedRadioValue = radioGroup[radIndex].value;
                  break;      
                }
          }
    return selectedRadioValue;
    }

//
function toggleT(_w,_h)
    {
          i=1;
          if (document.all) { // is IE
          if (_h=='s' && i%2==1) eval("document.all."+_w+".style.visibility='visible';");
          if (_h=='s' && i%2==0) eval("document.all."+_w+".style.visibility='hidden';");
                            }
          else { // is NS or other
          if (_h=='s' && i%2==1) eval("document.layers['"+_w+"'].visibility='show';");
          if (_h=='s' && i%2==0) eval("dicument.layers['"+_w+"'].visibility='hide';");
              }
    }
0
fwstealAuthor Commented:
my buttons:
-----------------
          <asp:Button CssClass="formbtn" ID="btnClear" runat="server" Text="Clear" TabIndex="24" />
          &nbsp;&nbsp;
          <asp:Button CssClass="formbtn" ID="cmdCheckIsValid" runat="server" Text="Validate"
          TabIndex="23" OnClientClick="return cmdCheckIsValid_onclick(document.frmCCDetails)"  />
          <span id="divt1" style="visibility:hidden;position:relative;top:0;left:0">
          <asp:Button CssClass="formbtn" ID="B1" runat="server" Text="Submit" />
          </span>
0
fwstealAuthor Commented:
my problem thus far:

alert("Invalid Card Number"); //problem here - wants to continue processing form when
                        //I enter incorrect data such as letters
0
fwstealAuthor Commented:
my problem thus far:

alert("Invalid Card Number"); //problem here - wants to continue processing form when
                        //I enter incorrect data such as letters
0
alfredwhangCommented:
you need a "return":

OnClientClick="return cmdCheckIsValid_onclick(document.frmCCDetails)"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alfredwhangCommented:
think we are 'lil out of sync here. ignore my previous post again
0
alfredwhangCommented:
if you are not using FireFox i would suggest you to get it soon...then you can use FireBug to step through and debug your javascript.  it's very useful.

not sure what the problem is.  looks strange. you might want to try setting UseSubmitBehavior="false" for your button to see if it helps.
0
fwstealAuthor Commented:
if I enter a visa number that is 16 characters is should not be invalid.

<span class="bodyheadings">Type of Credit Card:&nbsp;</span>&nbsp;
                     <input name='rblTypeCCard' Type='radio' Value='1'>Visa
              &nbsp; <input name='rblTypeCCard' Type='radio' value='2'>Mastercard
              &nbsp; <input name='rblTypeCCard' Type='radio' value='3'>American Express
              &nbsp; <input name='rblTypeCCard' Type='radio' value='4'>
              Discover      

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.