English 中文(简体)
javascript in IE7, number (integer) accreditation don t work
原标题:javascript in IE7, number (integer) validation doesn t work
function isInteger(char) {
    return (char.toString().search(/^-?[0-9]+$/) == 0 )
}
function validateQuan() {
var minExist = "false";
    for (i=0; i<3; i++) {
        var j = document.getElementById("ProductQuantity_"+i).value; 
        if (isInteger(j) && j > 0) {minExist = "true";alert("oi");} 
    }
    if (minExist == "true") {$("#pvs_form").submit();} else {alert("Please enter at least one valid quantity");}
}

Above is my code, it s working on FF 3.6 and IE8, but not with IE7. At IE7 "if (isInteger(j) && j > 0)" couldn t lead to "true". Even i remove "j > 0" and use "[1-9]" on regex.

任何更好的解决办法?

<><>>:> or,reg,任何编号<代码>> 9也属于“真实”。

edit : big sorry to all, i just found that the main problem actually because i got other form at the same page with the same id "ProductQuantity_"+i, and likely it turn the IE7 to only look at the first element with that id found on page. Maybe my original code was working actually, but thx to remind me that the regex is not as expected and to get better code. Thx and sorry once more.

最佳回答
   var isInteger = function( str ) { 
      return  (str|0) == str;
    };

2. 减去点数后再确保点起点起点起伏数或数点被替换为绝缘

isInteger("3.4") // false
isInteger("abc") // false => "abc"|0 = 0
isInteger("3") // true

现在:

function validateQuan() {
    var minExist = false ;
    for (var i = 0; i < 3; i++ ){
        var j = document.getElementById("ProductQuantity_"+i).value; 
        if ( isInteger( j ) && j > 0 ) {
            minExist = true; 
            alert("oi");
            break; // goal achieved, break loop
        } 
    }
    if ( minExist ) {
        $("#pvs_form").submit();
    } else {
        alert("Please enter at least one valid quantity");
    }
}
问题回答

Try:

function validateQuan() {
    var minExist = "false";
    for( i=0; i<3; i++) {
        var j = parseInt(document.getElementById("ProductQuantity_"+i).value);
        if( !isNaN(j) && j > 0) {minExist = "true"; alert("oi");}
    }
    if( minExist == "true") {...} else {...}
}

Try:

function isInteger(char) {
    return Number(char) == parseInt(char);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签