Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-27
14
Medium Priority
?
2,399 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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