Reputation: 43
parseInt("09", 10) // 9 in this way it will just remove the leading 0
but i want to create a program that if the user input a number with leading zeros , the output will be invalid
Upvotes: 0
Views: 334
Reputation: 104790
// You can validate '0', '0.2' or '2.00' with this regular expression:
function validDigits(str){
return /^(0|[1-9]\d*)(\.\d+)?$/.test(str)? 'valid':false;
}
//test:
['100', '1.0','0.5','05','0','2.0','002','2.00'].map(function(itm){
return itm+': '+ validDigits(itm);
}).join('\n')
/* returned value: (String)
100: valid
1.0: valid
0.5: valid
05: false
0: valid
2.0: valid
002: false
2.00: valid
*/
Upvotes: 1
Reputation: 1206
function test(input) {
return !/^0.*[0-9]/.test(input);
}
// false
console.log( test("01") );
// true
console.log( test("1") );
// false
console.log( test("00") );
// true
console.log( test("0") );
Upvotes: 1