Solved

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

Posted on 2007-03-27
14
2,375 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:fwsteal
  • 7
  • 7
14 Comments
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802474
looks like you are missing the "return false;"  in the "else if (theForm.txtCardHoldAddr.value=="")" ???
0
 

Author Comment

by:fwsteal
ID: 18802580
i've got that in there
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802686
try one thing...put a "return false;" in the beginning to short circuit the whole thing and see what happens.
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802792
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18802832
sorry wrong post
0
 

Author Comment

by:fwsteal
ID: 18802980
no worries; did as suggestion - sorry
0
 

Author Comment

by:fwsteal
ID: 18802989
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:fwsteal
ID: 18802993
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
 

Author Comment

by:fwsteal
ID: 18803010
my problem thus far:

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

Author Comment

by:fwsteal
ID: 18803037
my problem thus far:

alert("Invalid Card Number"); //problem here - wants to continue processing form when
                        //I enter incorrect data such as letters
0
 
LVL 2

Accepted Solution

by:
alfredwhang earned 500 total points
ID: 18803064
you need a "return":

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

Expert Comment

by:alfredwhang
ID: 18803078
think we are 'lil out of sync here. ignore my previous post again
0
 
LVL 2

Expert Comment

by:alfredwhang
ID: 18803197
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
 

Author Comment

by:fwsteal
ID: 18803269
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now