Solved

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

Posted on 2007-03-27
14
2,390 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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