javascript - Regex to allow numbers only between 1 and 20000 -


i new javascript , using regex expression validating whether values between 1 , 20000 ^(?:[1-9]|(?:[1-9][0-9])|(?:[1-9][0-9][0-9])|(?:[1-9][0-9][0-9][0-9])|(?:[1-9][0-9][0-9][0-9][0-9])|(?:20000))$

this expression tried allows 99999 valid

although i'm doubtful most-efficient solution, should trick.

/^((1[0-9]{0,4})|([1-9][0-9]{0,3})|20000)$/ 

be warned string-matching, not type-casting, things decimal places , zero-filling not matched.

regex explained:

graph

unit test used:

for (var = 0; < 100000; i++) {  	var should = > 0 && <= 20000;  	if (should !== /^((1[0-9]{0,4})|([1-9][0-9]{0,3})|20000)$/.test('' + i)) {  		console.log('failed:', i);  	}  }  console.log('done');


Comments