0

In my current regular expression, I am negating digits:

$(function(){
  $("#somewhat").bind("keyup",
   function(event) {
     var regex = /^[\D]*$/;
     alert(regex.test($("#somewhat").val()));
  });
});

What I have in my mind is to add some special characters on which I should negate, !@#$%^&*()_+=<>.?/~`:;" , and leaving dash, apostrophe ( -' ) to the valid list. I'm still kind of dizzy on this regular expression thing. To test with, I added + on the regex,

var regex = /^[\D+]*$/;

When I test it, the alert box returns TRUE, which is not I am expecting.

planet x
  • 1,599
  • 5
  • 26
  • 44
  • What is the content of the HTML element you're matching? – Ernest Friedman-Hill Jan 17 '12 at 01:54
  • I am trying to validate name, I can't seem to use `/[a-zA-Z]*$/` since I am accepting other letters in European country such as Germany. So I go for the other way around in which I come up to negation. – planet x Jan 17 '12 at 01:56
  • 1
    See http://stackoverflow.com/questions/888838/regular-expression-for-validating-names-and-surnames – Daveo Jan 17 '12 at 02:02
  • Also to test your regex have you tried http://gskinner.com/RegExr/ allows you to hover your mouse of each portion of the regex and it explains in plain english what it will do – Daveo Jan 17 '12 at 02:04

2 Answers2

3

Inside [ ] please add all the characters you don't want to allow.

/^((?![\d!@#$%^&*()_+=<>.?/~`:;"]).)*$/

But can we rely on negating given characters ? because user will be able to enter any character other than these. If you want to allow non-English characters, I would suggest you to use Unicode ranges

see this : http://kourge.net/projects/regexp-unicode-block

Diode
  • 24,570
  • 8
  • 40
  • 51
0

[\D+] means "any character that is +, or that is not a digit"; so it's actually equivalent to plain old \D or [\D], since + itself isn't a digit.

To get the meaning of "any character that is neither + nor a digit", you'd have to write [^\d+].

(\D or [\D] is equivalent to [^\d], but its negative-ness doesn't extend beyond that.)

ruakh
  • 175,680
  • 26
  • 273
  • 307