regex - Regular Expression to check number length using javascript and allowing length between 6 and 8 - Stack Overflow

I want to check the number length exists between 6 and 8, I tried in many ways but it is not working fo

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
 |  Show 5 more ments

3 Answers 3

Reset to default 4

Your 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}$

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619235a4636420.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信