admin管理员组文章数量:1025278
I want to check the number length exists between 6 and 8, I tried in many ways but it is not working for me. Can any one help me with these?
My code:
Method 1:
regex = RegExp(/[-]{0,1}[\d{6,8}]*[\.]{0,1}[\d{6,8}]+/g),
Method 2:
regex = RegExp(/[-]{0,1}[\d]{6,8}*[\.]{0,1}[\d]{6,8}+/g),
I have tried above two ways but nothing helped me in achieve this. Please help me with the solution.
I want to check the number length exists between 6 and 8, I tried in many ways but it is not working for me. Can any one help me with these?
My code:
Method 1:
regex = RegExp(/[-]{0,1}[\d{6,8}]*[\.]{0,1}[\d{6,8}]+/g),
Method 2:
regex = RegExp(/[-]{0,1}[\d]{6,8}*[\.]{0,1}[\d]{6,8}+/g),
I have tried above two ways but nothing helped me in achieve this. Please help me with the solution.
Share Improve this question edited Apr 29, 2019 at 5:29 Kingsley 15k5 gold badges35 silver badges59 bronze badges asked Apr 29, 2019 at 5:24 prasanthprasanth 752 silver badges11 bronze badges 10- Why people are downvoting the question is correct and the OP showed his tries? – Maheer Ali Commented Apr 29, 2019 at 5:25
- With your regex in post, do you want to allow six digits before the decimal and after the decimal as well? Also, please add some valid/invalid samples. – Pushpesh Kumar Rajwanshi Commented Apr 29, 2019 at 5:26
- @MaheerAli: Downvoting regex posts seems like a fashion, as I've been seeing since few months since I am active here. I didn't downvote though. – Pushpesh Kumar Rajwanshi Commented Apr 29, 2019 at 5:27
- Please give some examples of valid strings. – some Commented Apr 29, 2019 at 5:30
- @PushpeshKumarRajwanshi: I dont need decimal validation here ,I just want to get the string with length between 6 and 8 digits – prasanth Commented Apr 29, 2019 at 5:35
3 Answers
Reset to default 4Your description doesn’t match what your attempts are doing.
Generally You’re using character classes unnecessarily. Try this, which matches numbers between 6 and 8 digits long, optionally preceded by a -
:
regex = RegExp(/-?\d{6,8}/g);
If there are other patterns you want to match, describe them, and add examples.
If you want to detect strings which have just 8 digits, anywhere, in them, then consider this approach:
var number = "314159.32";
if (/^-?[0-9]+(?:\.[0-9]+)?$/.test(number) &&
/^[0-9]{6,8}$/.test(number.replace(/[.-]/g, ""))) {
console.log("valid");
}
else {
console.log("invalid");
}
The difficulty of using a single regex here is the optional decimal ponent, which may or may not appear. But, assuming the input is a number, and has no decimal ponent, then the regex pattern needed is easy.
Your question is not very clear, please provide some samples of valid strings. But not I can suggest you to use this regex:
^\d{6,8}$
I want to check the number length exists between 6 and 8, I tried in many ways but it is not working for me. Can any one help me with these?
My code:
Method 1:
regex = RegExp(/[-]{0,1}[\d{6,8}]*[\.]{0,1}[\d{6,8}]+/g),
Method 2:
regex = RegExp(/[-]{0,1}[\d]{6,8}*[\.]{0,1}[\d]{6,8}+/g),
I have tried above two ways but nothing helped me in achieve this. Please help me with the solution.
I want to check the number length exists between 6 and 8, I tried in many ways but it is not working for me. Can any one help me with these?
My code:
Method 1:
regex = RegExp(/[-]{0,1}[\d{6,8}]*[\.]{0,1}[\d{6,8}]+/g),
Method 2:
regex = RegExp(/[-]{0,1}[\d]{6,8}*[\.]{0,1}[\d]{6,8}+/g),
I have tried above two ways but nothing helped me in achieve this. Please help me with the solution.
Share Improve this question edited Apr 29, 2019 at 5:29 Kingsley 15k5 gold badges35 silver badges59 bronze badges asked Apr 29, 2019 at 5:24 prasanthprasanth 752 silver badges11 bronze badges 10- Why people are downvoting the question is correct and the OP showed his tries? – Maheer Ali Commented Apr 29, 2019 at 5:25
- With your regex in post, do you want to allow six digits before the decimal and after the decimal as well? Also, please add some valid/invalid samples. – Pushpesh Kumar Rajwanshi Commented Apr 29, 2019 at 5:26
- @MaheerAli: Downvoting regex posts seems like a fashion, as I've been seeing since few months since I am active here. I didn't downvote though. – Pushpesh Kumar Rajwanshi Commented Apr 29, 2019 at 5:27
- Please give some examples of valid strings. – some Commented Apr 29, 2019 at 5:30
- @PushpeshKumarRajwanshi: I dont need decimal validation here ,I just want to get the string with length between 6 and 8 digits – prasanth Commented Apr 29, 2019 at 5:35
3 Answers
Reset to default 4Your description doesn’t match what your attempts are doing.
Generally You’re using character classes unnecessarily. Try this, which matches numbers between 6 and 8 digits long, optionally preceded by a -
:
regex = RegExp(/-?\d{6,8}/g);
If there are other patterns you want to match, describe them, and add examples.
If you want to detect strings which have just 8 digits, anywhere, in them, then consider this approach:
var number = "314159.32";
if (/^-?[0-9]+(?:\.[0-9]+)?$/.test(number) &&
/^[0-9]{6,8}$/.test(number.replace(/[.-]/g, ""))) {
console.log("valid");
}
else {
console.log("invalid");
}
The difficulty of using a single regex here is the optional decimal ponent, which may or may not appear. But, assuming the input is a number, and has no decimal ponent, then the regex pattern needed is easy.
Your question is not very clear, please provide some samples of valid strings. But not I can suggest you to use this regex:
^\d{6,8}$
本文标签:
版权声明:本文标题:regex - Regular Expression to check number length using javascript and allowing length between 6 and 8 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619233a2159475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论