2

I am newbee in regex so caught on a problem, I googled but unfortunately didn't got the solution so here my problem is :
I need a regex which disallow hyphen as first character(If it is the only character in the string)

Eg: - (disallow)

But if I write some digit with it then it should allow it, and also allow fraction values and also it should not allow any alphabets

Eg:
1234 (allow)
-1234 (allow)
-1234.1234 (allow)

No alphabets should be allowed in entire strings and no repetition of hyphen.

This is what I have tried so far:

((^-?[0-9]{0,4})|(^-?[0-9]{0,4}))(.[0-9]{1,4})?

It works fine for all scenario except single hyphen ie

eg: - (it is allowed using my regex but I shouldn't)

The help is truly appreciated.. :)

Shail_bee
  • 499
  • 4
  • 24

5 Answers5

1

I don't know what's your intention is. Adding a negative lookahead at the start in your regex, won't allow only -

^(?!-$)((^-?[0-9]{0,4})|(^-?[0-9]{0,4}))(\.[0-9]{1,4})?$

DEMO

You could reduce the above regex as,

^(?!-$)(-?[0-9]{0,4})(\.[0-9]{1,4})?$
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1
^(?!-$)-?[0-9]{0,4}(?:\.[0-9]{1,4})?$

Try this.See demo.

https://regex101.com/r/cA4wE0/20

vks
  • 67,027
  • 10
  • 91
  • 124
  • I'm tempted to downvote this for doing `[0-9]` instead of `\d` alone. – pguardiario Dec 18 '14 at 09:42
  • @pguardiario in some languages, [`\d` might also match unicode digits](http://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9/16621778#16621778) therefore using `[0-9]` might be considered a best practice if you only want `0-9`. – HamZa Dec 18 '14 at 09:47
  • @hamza I think that's a really long stretch. – pguardiario Dec 18 '14 at 09:51
  • 1
    @vks Yes I do have a long list. But I also upvote answers that use good practices. – pguardiario Dec 18 '14 at 09:52
  • @pguardiario I know. Note that I'm totally with using `\d` since you would need to use the `u` modifier to match unicode digits in most languages. Also JS regex doesn't support unicode yet so `\d` would safely only match `0-9`. – HamZa Dec 18 '14 at 09:56
1

The reason that the expression allows the single hyphen is that the first part allows the hyphen without digits, and the second part is optional.

Rearrange the parts, so that you allow either a number that is at least one digit without the fraction, or a number with at least zero digits with the fraction:

^-?(([0-9]{1,4})|([0-9]{0,4}\.[0-9]{1,4}))$

Demo:

function validate(s) {
  var ok = /^-?(([0-9]{1,4})|([0-9]{0,4}\.[0-9]{1,4}))$/.test(s);
  // show result in Stackoverflow snippet
  document.write(s + ' : ' + (ok ? 'allowed' : 'not allowed') + '<br>');
}

validate('-');
validate('1234');
validate('1234.123');
validate('1234.12345');
validate('12345');
validate('.123');
validate('.12345');
validate('-1234');
validate('-1234.123');
validate('-1234.12345');
validate('-12345');
validate('-.123');
validate('-.12345');
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Why don't you do it without regex? Try something like this:

var str1 = '-';
var str2 = '-1234';
var str3 = '1234';
var str = str1;

if (str.length == 1 && str == '-')
   alert('Not Allowed');
HamZa
  • 14,671
  • 11
  • 54
  • 75
Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
  • I like this approach, note that s?he still has to check for the other requirements. Maybe [this link](http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) might help? – HamZa Dec 18 '14 at 09:52
0

It sounds like you want:

^(?!.*[a-zA-Z])([^-]|.*\d).*

That means:

  • no a-z anywhere
  • either not starts with a hyphen or contains a digit
pguardiario
  • 53,827
  • 19
  • 119
  • 159