Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
I need help to resolve the issue because the code fails due to numLength[0]:
<html>
<body>
<script type="text/javascript">
function xyz(){
var predefLength = "5,5";
var number = "20";
var numLength = predefLength.split(",");
var result = /^[-+]?\d{0,numLength[0]}(\.\d{0,numLength[1]})?$/.test(number);
document.write(result);
}
</script>
</body>
</html>
Any help will appreciated.
Use the RegExp
-constructor, like:
var pattern = new RegExp('pattern as a string', 'flags as a string');
Or:
var result = new RegExp('^[-+]?\d{0,' + numLength[0] + '}(\.\d{0,' + numLength[1] + '})?$').test(number);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With