简体   繁体   中英

How to print the numbers in alert box?

This code is showing the alert box when the two numbers entered are same,now I want show the numbers in the alert box it is showing only one number,but I want to show each number which is same for example,if I enter 83 in two input boxes and 85 in other two input boxes,the alert box should show 83 and 85,you can not enter these numbers more than once.
function validateForm() {

    for (var x = 0; x < 81; x++) {
        for (var y = x + 1; y < 81; y++) {
            if (document.forms["myForm"]['pk' + x].value == document.forms["myForm"]['pk' + y].value) {
                if (document.forms["myForm"]['pk' + x].value == "") {
                    return true;
                } else {
                    alert('You can not enter a number more than once');
                    return false;
                }
            }
        }
    }
    return true;
}

A completely different take on it, 81 iterations only, rather than 3000+

function validateForm() {
    var q = {}, a = [];
    for (var i=0; i<81; i++) {
        var value = document.forms["myForm"]['pk'+i].value;
        if (value !== "") {
            if (q[value]) {
                if (q[value] < 2) {
                    a.push(value);
                }
                q[value] ++;
            }
            else {
                q[value] = 1;
            }
        }
    }
    if(a.length) {
        alert(a.join(', ') + ' You can not enter a number more than once');
        return false;
    }
    return true;
}

This loops through all the values a single time, keeping a tally of how many of each value there is (var q) - if a tally of a value hits 2, the value is added to the array (var a). So, triples, quadruples etc, will only be reported once.

If a.length > 0, the alert is shown and false is returned

Do not use return inside the loop, else declare a variable say validate and store true and false values in it and return it at the end, else the function execution will stop at return. Modified code is:

function validateForm() {
    var validate = true;
    for (var x = 0; x < 81; x++) {
        for (var y = x + 1; y < 81; y++) {
            if (document.forms["myForm"]['pk' + x].value == document.forms["myForm"]['pk' + y].value) {
                if (document.forms["myForm"]['pk' + x].value == "") {
                } else {
                    alert('You can not enter a number more than once. Duplicate number is:'+ document.forms["myForm"]['pk' + x].value);
                        validate =  false;
                    }
                }
            }
        }
        return validate;
    }

From your code, this can help you

 <script>
            function validateForm() {

        var val1,val2;
            for (var x=0; x<81; x++) {
                for (var y=x+1; y<81; y++) {
        val1 = document.forms["myForm"]['pk'+x].value;
        val2 = document.forms["myForm"]['pk'+y].value
                  if (document.forms["myForm"]['pk'+x].value==document.forms["myForm"]['pk'+y].value) {

                   if ( document.forms["myForm"]['pk'+x].value=="") {
                   return true;
                   }
                   else {
                    alert(val1 + 'and'+ val2 +',You can not enter a number more than once');
                    return false;
                    }
                  }else{
                   alert(val1 + 'and'+ val2 +',You can not enter a number more than once');
                    return false;
}


                }


              }


              return true;
            }



            </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM