SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two onSubmit= Events

    Hello. I need to run two validations using the onSubmit function.

    Is it possible to have both events in the same tag?

    I need to run this events:

    onSubmit="return validate_form(this)"
    onSubmit="return Mod10(document.form1.Number.value);"

    can you combine both so that it validates these conditions?

    Thanks!

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps,
    Code:
    onsubmit="return validate_form(this) && Mod10(document.form1.Number.value)"
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply. I'm afraid it didn't work...

    Any ideas?

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why didn't it work? What error messages do you get? Did you try the JavaScript debugger in Dragonfly (Opera) or Firebug (Firefox) to see what happens and where things go wrong?

    The document.form1.Number.value notation won't work in all browsers. It would be better to assign an ID to the input field and use document.getElementById().
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    onsubmit="return validate_form(this) && Mod10(document.form1.Number.value)"
    Tommy's post showed up with html entities instead of ampersands...or maybe it works with the entities?

  6. #6
    SitePoint Member
    Join Date
    May 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it executes the first event however the second is not working. I already tried both ways and didn't work.

    Here is the complete code for the events:

    #1: Checks that the required fields are filled:
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {
    with (field)
    {
    if (value==null||value=="")
    {alert(alerttxt);return false;}
    else {return true}
    }
    }

    function validate_form(thisform)
    {
    with (thisform)
    {
    if (validate_required(Number,"Por favor llena el campo N&#250;mero de Tarjeta")==false)
    {Number.focus();return false;}

    if (validate_required(Expires,"Por favor especifica la Fecha de Vencimiento de tu tarjeta")==false)
    {Expires.focus();return false;}

    if (validate_required(Cvv2Val,"Por favor llena el campo C&#243;digo CVV2")==false)
    {Cvv2Val.focus();return false;}

    }
    }
    </script>

    #2 Checks the the CC is a valid Credit Card Number:
    <script type="text/javascript">
    <!--
    /* This script and many more are available free online at
    The JavaScript Source!! http://javascript.internet.com
    Created by: David Leppek :: https://www.azcode.com/Mod10

    Basically, the alorithum takes each digit, from right to left and muliplies each second
    digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
    the multiple are then added together for a new number (1 + 2 = 3). You then add up the
    string of numbers, both unaltered and new values and get a total sum. This sum is then
    divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
    name Mod 10 or Modulus 10. */
    function Mod10(ccNumb) { // v2.0
    var valid = "0123456789" // Valid digits in a credit card number
    var len = ccNumb.length; // The length of the submitted cc number
    var iCCN = parseInt(ccNumb); // integer of ccNumb
    var sCCN = ccNumb.toString(); // string of ccNumb
    sCCN = sCCN.replace (/^\s+|\s+$/g,''); // strip spaces
    var iTotal = 0; // integer total set at zero
    var bNum = true; // by default assume it is a number
    var bResult = false; // by default assume it is NOT a valid cc
    var temp; // temp variable for parsing string
    var calc; // used for calculation of each digit

    // Determine if the ccNumb is in fact all numbers
    for (var j=0; j<len; j++) {
    temp = "" + sCCN.substring(j, j+1);
    if (valid.indexOf(temp) == "-1"){bNum = false;}
    }

    // if it is NOT a number, you can either alert to the fact, or just pass a failure
    if(!bNum){
    /*alert("Not a Number");*/bResult = false;
    }

    // Determine if it is the proper length
    if((len == 0)&&(bResult)){ // nothing, field is blank AND passed above # check
    bResult = false;
    } else{ // ccNumb is a number and the proper length - let's see if it is a valid card number
    if(len >= 15){ // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){ // LOOP throught the digits of the card
    calc = parseInt(iCCN) &#37; 10; // right most digit
    calc = parseInt(calc); // assure it is an integer
    iTotal += calc; // running total of the card number as we loop - Do Nothing to first digit
    i--; // decrement the count - move to the next digit in the card
    iCCN = iCCN / 10; // subtracts right most digit from ccNumb
    calc = parseInt(iCCN) % 10 ; // NEXT right most digit
    calc = calc *2; // multiply the digit by two
    // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
    // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
    switch(calc){
    case 10: calc = 1; break; //5*2=10 & 1+0 = 1
    case 12: calc = 3; break; //6*2=12 & 1+2 = 3
    case 14: calc = 5; break; //7*2=14 & 1+4 = 5
    case 16: calc = 7; break; //8*2=16 & 1+6 = 7
    case 18: calc = 9; break; //9*2=18 & 1+8 = 9
    default: calc = calc; //4*2= 8 & 8 = 8 -same for all lower numbers
    }
    iCCN = iCCN / 10; // subtracts right most digit from ccNum
    iTotal += calc; // running total of the card number as we loop
    } // END OF LOOP
    if ((iTotal%10)==0){ // check to see if the sum Mod 10 is zero
    bResult = true; // This IS (or could be) a valid credit card number.
    } else {
    bResult = false; // This could NOT be a valid credit card number
    }
    }
    }
    // change alert to on-page display or other indication as needed.

    if(!bResult){
    alert("This is NOT a valid Credit Card Number!");
    }
    return bResult; // Return the results
    }
    // -->
    </script>


    I need to run both, maybe you can comeup with only 1 event which includes both actions...

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Number is a class name in javascript. Could be why it's not working. Also might be that you never return true from validate_required. To be safe I'd reference the form elements like:
    formObject['elementName']

    Here is a new validate_required that also calls Mod10, so you just need to use
    Code:
    onsubmit="return validate_required(this);"
    Code:
    function validate_form(thisform)
    {
    
    	if (validate_required(thisform['Number'],"Por favor llena el campo N&#250;mero de Tarjeta")==false) {
    		thisform['Number'].focus();
    		return false;
    	}
    	
    	if (validate_required(thisform['Expires'],"Por favor especifica la Fecha de Vencimiento de tu tarjeta")==false) {
    		thisform['Expires'].focus();
    		return false;
    	}
    	
    	if (validate_required(thisform['Cvv2Val'],"Por favor llena el campo C&#243;digo CVV2")==false) {
    		thisform['Cvv2Val'].focus();
    		return false;
    	}
    	
    	if (!Mod10(thisform['Number']))  {
    		thisform['Number'].focus();
    		return false;
    	}
    	return true;
    
    }
    See if that works better for you

  8. #8
    SitePoint Member
    Join Date
    May 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately the variable Number must be named like that (CC processor variables).

    I tried using the following:

    Code JavaScript:
    onSubmit="return validate_required(this)"

    however it does not run the event. But it half works if I use:

    Code JavaScript:
    onSubmit="return validate_form(this)"

    The little problem is that it will always pop up the alert message saying "This is NOT a valid Credit Card Number!"

    This is how it looks using the new code:

    Code JavaScript:
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {
    with (field)
    {
    if (value==null||value=="")
      {alert(alerttxt);return false;}
    else {return true}
    }
    }
     
    function validate_form(thisform)
    {
     
    	if (validate_required(thisform['Number'],"Por favor llena el campo Número de Tarjeta")==false) {
    		thisform['Number'].focus();
    		return false;
    	}
     
    	if (validate_required(thisform['Expires'],"Por favor especifica la Fecha de Vencimiento de tu tarjeta")==false) {
    		thisform['Expires'].focus();
    		return false;
    	}
     
    	if (validate_required(thisform['Cvv2Val'],"Por favor llena el campo Código CVV2")==false) {
    		thisform['Cvv2Val'].focus();
    		return false;
    	}
     
    	if (!Mod10(thisform['Number']))  {
    		thisform['Number'].focus();
    		return false;
    	}
    	return true;
     
    }
    </script>
    <script type="text/javascript">
    <!--
    /* This script and many more are available free online at
    The JavaScript Source!! [url]http://javascript.internet.com[/url]
    Created by: David Leppek :: [url]https://www.azcode.com/Mod10[/url]
     
    Basically, the alorithum takes each digit, from right to left and muliplies each second
    digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
    the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
    string of numbers, both unaltered and new values and get a total sum. This sum is then
    divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
    name Mod 10 or Modulus 10. */
    function Mod10(ccNumb) {  // v2.0
    var valid = "0123456789"  // Valid digits in a credit card number
    var len = ccNumb.length;  // The length of the submitted cc number
    var iCCN = parseInt(ccNumb);  // integer of ccNumb
    var sCCN = ccNumb.toString();  // string of ccNumb
    sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
    var iTotal = 0;  // integer total set at zero
    var bNum = true;  // by default assume it is a number
    var bResult = false;  // by default assume it is NOT a valid cc
    var temp;  // temp variable for parsing string
    var calc;  // used for calculation of each digit
     
    // Determine if the ccNumb is in fact all numbers
    for (var j=0; j<len; j++) {
      temp = "" + sCCN.substring(j, j+1);
      if (valid.indexOf(temp) == "-1"){bNum = false;}
    }
     
    // if it is NOT a number, you can either alert to the fact, or just pass a failure
    if(!bNum){
      /*alert("Not a Number");*/bResult = false;
    }
     
    // Determine if it is the proper length 
    if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
      bResult = false;
    } else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
      if(len >= 15){  // 15 or 16 for Amex or V/MC
        for(var i=len;i>0;i--){  // LOOP throught the digits of the card
          calc = parseInt(iCCN) % 10;  // right most digit
          calc = parseInt(calc);  // assure it is an integer
          iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
          i--;  // decrement the count - move to the next digit in the card
          iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
          calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
          calc = calc *2;                                 // multiply the digit by two
          // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
          // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
          switch(calc){
            case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
            case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
            case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
            case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
            case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
            default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
          }                                               
        iCCN = iCCN / 10;  // subtracts right most digit from ccNum
        iTotal += calc;  // running total of the card number as we loop
      }  // END OF LOOP
      if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
        bResult = true;  // This IS (or could be) a valid credit card number.
      } else {
        bResult = false;  // This could NOT be a valid credit card number
        }
      }
    }
    // change alert to on-page display or other indication as needed.
     
    if(!bResult){
      alert("This is NOT a valid Credit Card Number!");
    }
      return bResult; // Return the results
    }
    // -->
    </script>

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake I meant to put
    onSubmit="return validate_form(this)"

    And my call to Mod10 is wrong too, instead of
    Code:
    if (!Mod10(thisform['Number']))  {
    should be
    Code:
    if (!Mod10(thisform['Number'].value))  {

  10. #10
    SitePoint Member
    Join Date
    May 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works perfectly now!

    I really appreciate your help!!! =)

  11. #11
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    Tommy's post showed up with html entities instead of ampersands...or maybe it works with the entities?
    They need to be entities, or you'll end up with invalid HTML. Entities are parsed in CDATA attributes like onsubmit, so &amp;&amp; will be seen as && by the JavaScript interpreter.

    Since validate_form() didn't have a return true statement, the return value would be undefined and the expression would fail.
    Birnam wood is come to Dunsinane

  12. #12
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They need to be entities, or you'll end up with invalid HTML.
    Good to know!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •