var restriction = 2; var re = new RegExp("^([a-zA-Z]{" + restriction +"})$"); alert(re); alert(value); result = re.test(value);