asp.net - Clarification on JavaScript function to validate the Numeric -


i working on existing function validate object value numeric , accept comma character.

obj = 1,2,3,4,

however function not giving results ..

var checkok;                 if (isint == true) {                     checkok = "0123456789";                 }                 else {                     checkok = "0123456789.";                 }                   (i = 0; < checkstr.value.length; i++) {                     ch = checkstr.value.charat(i);                      (j = 0; j < checkok.length; j++) {                           if (ch == checkok.charat(j)) {                             if (isint == true && j == 0) {                                 allvalid = false;                                 break;                             }                             else {                                 break;                             }                         }                         if (j == checkok.length - 1) {                             allvalid = false;                             break;                         }                     }                      allnum += ch;                 }  if (allvalid==false)                 {                     alertsay = "please enter valid values "                     alert(alertsay);                     document.getelementbyid(obj.id).innertext="";                     obj.focus();                     return (false);                 } 

in javascript can do:

/^[0-9,]+$/g.test(checkstr); 

this returns true if string (not object) contains numbers or numbers + comma @ random places. (also returns true string '123,,6'). otherwise returns false.

if want check string number, 1 decimal point or comma, can do:

!isnan(checkstr); 

this returns true if checkstr or can converted valid number. (from post: is there (built-in) way in javascript check if string valid number?)


Comments