6

i am trying only to allow numerals and special chars like '.' and ',' to be allowed in my text string. for that i have tried following code

var pattern = /[A-Za-z]/g;
var nospecial=/[\(#\$\%_+~=*!|\":<>[\]{}`\\)';@&?$]/g;
if (!ev.ctrlKey && charCode!=9 && charCode!=8 && charCode!=36 && charCode!=37 && charCode!=38 && (charCode!=39 || (charCode==39 && text=="'")) && charCode!=40) {
    console.log(text);
    if (!pattern.test(text) && !nospecial.test(text)) {
        console.log('if');
        return true;
    } else {
        console.log('else');
        return false;
    }
}

but not getting the desired output. tell me where i am wrong.

Smern
  • 18,746
  • 21
  • 72
  • 90
Garry
  • 306
  • 1
  • 7
  • 24
  • 2
    Seeing @smerny's answer, it seems that you want something slightly different from what you first asked. Otherwise, you'd just be able to do `/^[0-9.,]*$/`. Can you update the question so others can help (without trying to make the selected answer more efficient by guessing)? – Sam Oct 22 '14 at 22:56
  • 1
    So, if we are talking ascii, just make a class of characters that you want. Btw, Control chars range from `x00-x1f`, or `x7f`, so the tests for char codes 9 and 8 will never be reached as they are part of control codes. –  Oct 24 '14 at 19:11
  • In general, it's a bad idea to do this with a regex. What if you need to support non-US English locales where the decimal point and thousands separator use different characters? You also don't specify whether you need to allow a leading positive or negative symbol. – b4hand Oct 29 '14 at 20:49
  • Duplicate: http://stackoverflow.com/a/2811058/1307166 – b4hand Oct 29 '14 at 20:56
  • @b4hand, actually the OP specified that he wants to allow numerics, dots, and commas in his text string. That's why the answer to the question is simply `^[0-9.,]*$` which I provided... I must just have been bored that day because I went on to provide something more complex. If OP wanted to support different formats and no answer provided it, I suppose he would have mentioned that. – Smern Oct 29 '14 at 21:07
  • No, the OP says "special chars like '.' and ','", but there can be many characters "like" a "." and ",". I see nothing special about those symbols versus any other symbol unless the OP describes the intent of what is being done. Is this for currency, telephone number, etc.? There's no context. – b4hand Oct 29 '14 at 22:38
  • The OP isn't asking at all for any sort of format... it doesn't even mention format. Just says to allow some optional characters... even if there were some extra characters, it's not hard to just add them to the character set... and again, the OP probably would have mentioned it if he wanted a specific format. – Smern Oct 30 '14 at 00:42

5 Answers5

24

Forget trying to blacklist, just do this to allow what you want:

var pattern = /^[0-9.,]*$/;

Edit: Also, rather than just checking for numbers, commas, and dots. I'm assuming something like this do even more than you were hoping for:

var pattern = /^(0|[1-9][0-9]{0,2}(?:(,[0-9]{3})*|[0-9]*))(\.[0-9]+){0,1}$/;

Demo

enter image description here

Smern
  • 18,746
  • 21
  • 72
  • 90
  • `if (pattern.test(text)) { console.log('if'); return true; } else { console.log('else'); return false; }` i am using above code along with your pattern but not working – Garry Apr 17 '13 at 12:41
  • 1
    What does "not working" mean? Error messages? What input did you try and what did you expect it to do? – JJJ Apr 17 '13 at 12:47
  • this solution worked but i am looking for more efficient solution. @smerny – Garry Oct 22 '14 at 21:41
  • 2
    @Garry, what do you mean more efficient? What problem are you having with this? – Smern Oct 23 '14 at 00:06
  • 1
    That diagram was just about the most helpful thing I've ever seen explaining regex. – TKoL Oct 29 '14 at 20:51
5

So why don't you try /^[0-9,.]*$/ instead of negating the test?

Dio F
  • 2,458
  • 1
  • 22
  • 39
2

You can try this:

/([0-9]+[.,]*)+/

It will matche number with or withot coma or dots.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Kumar Gaurish
  • 99
  • 1
  • 4
0
^(?!.*[^0-9.,\n]).*$

Not sure of what you mean by efficient but this fails faster though it takes long to match correct string.See demo.

http://regex101.com/r/aK2zV7/1

vks
  • 67,027
  • 10
  • 91
  • 124
0

You could also just use the solution from this answer:

parseFloat(text.replace(',',''));
Community
  • 1
  • 1
b4hand
  • 9,550
  • 4
  • 44
  • 49
  • This only replace the first occurrence... so "123,456,789.123" would become simply "123456". So at the very least you would want to use `replace(/,/g,'')`... and since OP is looking to verify and do something depending on the outcome rather than convert a string to a number... maybe check for NaN? But even then, parseFloat will coerce it into a number as long as it starts that way ("243*#jfa32" would become 243). – Smern Oct 30 '14 at 01:24