Solved

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

Posted on 2007-03-27
14
2,385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
List<PaisEntity> - show some 1 38
Please explain the parts of these 2 LINQ expressions 3 83
Jquery datatables integration with ASP.NET MVC, bootstrap 3 31
Json and ajax 1 20
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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