2012-10-26 17 views
17

ho questa funzione javascript per convalidare se un numero è maggiore di un altro numerojavascript se il numero maggiore di numero

function validateForm() { 
    var x = document.forms["frmOrder"]["txtTotal"].value; 
    var y = document.forms["frmOrder"]["totalpoints"].value; 

    if (x > y) { 
     alert("Sorry, you don't have enough points"); 
     return false; 
    } 
} 

Non funziona per qualche ragione.

Se faccio alert(x) ricevo 1300, e alert(y) dà 999

Questo funziona ....

function validateForm() { 
    var x = 1300; 
    var y = 999; 

    if (x > y) { 
     alert("Sorry, you don't have enough points"); 
     return false; 
    } 
} 
+2

Si stanno confrontando stringhe, non numeri. –

+0

@ user1689607 Il confronto delle stringhe numeriche continuerà a funzionare, vale a dire "5"> "2" // true' –

+1

@MattStone: '" 1300 ">" 999 "; // false' –

risposta

42

li si dovrebbe convertire in numero prima di confrontare.

Prova:

if (+x > +y) { 
    //... 
} 

o

if (Number(x) > Number(y)) { 
    // ... 
} 

Nota:parseFloat e pareseInt (per confrontare intero, ed è necessario specificare il radix) vi darà NaN per una stringa vuota, confronta con NaN sarà sempre false, Se non si desidera trattare la stringa vuota essere 0, è possibile utilizzarli.

1

Fare questo.

var x=parseInt(document.forms["frmOrder"]["txtTotal"].value); 
var y=parseInt(document.forms["frmOrder"]["totalpoints"].value); 
+2

Specificare che si desidera base 10 con '. ..] ["txtTotal"]. value, 10); ' – Blender

3

È possibile "cast" al numero utilizzando il costruttore numero ..

var number = new Number("8"); // 8 number 

Si può anche chiamare parseInt funzione built-in:

var number = parseInt("153"); // 153 number 
+0

Ooops, utilizzando Number come costruttore restituisce un oggetto Number e un oggetto è sempre" uguale "a se stesso, quindi' new Number ('7') = = new Number ('7') 'restituisce' false'. Penso che intendevi usarlo come una funzione, quindi 'Number ('7') == Number ('7')'. E 'parseInt' dovrebbe sempre usare una radice:' parseInt ('153', 10) '. – RobG

+0

Ohh !, hai ragione @RobG riguardo "==" !! .. Ho testato questi casi e sono andato bene, e ho supposto che l'oggetto Number chiamasse sempre "toString()" implicitamente (in realtà, penso internamente al suo valore valueOf() chiamante: new Number ('725') < nuovo numero ('800'); // Output true new Number ('91 ')> new Number ('90'); // Output true new Number ('7') new Number ('8'); // Output false new Number ('8') edrian

1

si sta confrontando stringhe. JavaScript confronta il codice ASCII per ogni carattere della stringa.

capire perché si ottiene falso, guardare ai charCodes:

"1300".charCodeAt(0); 
49 
"999".charCodeAt(0); 
57 

Il paragone è falso perché, quando si confrontano le stringhe, i codici di carattere per 1 non è superiore a quella del 9.

La correzione consiste nel trattare le stringhe come numeri. È possibile utilizzare un numero di metodi:

parseInt(string, radix) 
parseInt("1300", 10); 
> 1300 - notice the lack of quotes 


+"1300" 
> 1300 


Number("1300") 
> 1300 
Problemi correlati