Regex to Validate String Having Only Characters (Not Special Characters), Blank Spaces and Numbers

Regex to validate string having only characters (not special characters), blank spaces and numbers

There are a couple ways of doing this. If you only want to allow ASCII word characters (no accented characters like Ê or letters from other alphabets like Ӕ or ל), use this:

/^[a-zA-Z\d\s]*$/

If you want to allow only numbers and letters from other languages for Ruby 1.8.7, use this:

/^(?:[^\W_]|\s)*$/u

If you want to allow only numbers and letters from other languages for Ruby 1.9.x, use this:

^[\p{Word}\w\s-]*$

Also, if you are planning to use 1.9.x regex with unicode support in Ruby on Rails, add this line at the beginning of your .rb file:

# coding: utf-8

Regex for letters and no spaces

You need to add anchors and also add + next to the charcater class.

var re = /^[A-Za-z]+$/;

^ - start

[A-Za-z]+ - One or more letters

$- End

Regex to validate strings having only characters (without special characters but with accented characters), blank spaces and numbers

I wrote the ^(?:[^\W_]|\s)*$ answer in the question you referred to (which actually would have been different if I'd known you wanted to allow _ and -). Not being a Ruby guy myself, I didn't realize that Ruby defaults to not using Unicode for regex matching.

Sorry for my lack of Ruby experience. What you want to do is use the u flag. That switches to Unicode (UTF-8), so accented characters are caught. Here's the pattern you want:

^[\w\s-]*$

And here it is in action at Rubular. This should do the trick, I think.

The u flag works on my original answer as well, though that one isn't meant to allow _ or - characters.

regex to match any characters and numbers but without space and @ symbol

You could use a negated character class.

^[^\s@]+$

Explanation:

^             # the beginning of the string
[^\s@]+ # any character except: whitespace, '@' (1 or more times)
$ # before an optional \n, and the end of the string

regex to allow space and restrict special characters at the beginning of a non empty string

You can use

^(?! *[=+@-]| +$).*

Or, to match any kind of whitespaces:

^(?!\s*[=+@-]|\s+$).*

Details:

  • ^ - start of string
  • (?!\s*[=+@-]|\s+$) - a negative lookahead that fails the match if, immediately to the right from the current location, there is
    • \s*[=+@-] - zero or more whitespaces and then =, +, @, or -
    • | - or
    • \s+$ - one or more whitespaces till end of string
  • .* - zero or more chars other than line break chars as many as possible.

Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)

You simply need to specify your current RE, followed by a letter/number followed by your current RE again:

^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$

Since you've now stated they're Javascript REs, there's a useful site here where you can test the RE against input data.

If you want lowercase letters as well:

^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$

Matching special characters and letters in regex

Add them to the allowed characters, but you'll need to escape some of them, such as -]/\

var pattern = /^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/

That way you can remove any individual character you want to disallow.

Also, you want to include the start and end of string placemarkers ^ and $

Update:

As elclanrs understood (and the rest of us didn't, initially), the only special characters needing to be allowed in the pattern are &-._

/^[\w&.\-]+$/

[\w] is the same as [a-zA-Z0-9_]

Though the dash doesn't need escaping when it's at the start or end of the list, I prefer to do it in case other characters are added. Additionally, the + means you need at least one of the listed characters. If zero is ok (ie an empty value), then replace it with a * instead:

/^[\w&.\-]*$/

Validate string to have only one occurrence of `-` or `.` special characters inside it

Your current regex does not allow a single char input while it is possible to have a. You may fix it using a grouping construct with a ? quantifier set to it:

var regex = new Regex(@"^[a-zA-Z]+(?:[.-][a-zA-Z]+)?$");
// or, using the case insensitive modifier
var regex = new Regex(@"^[A-Z]+(?:[.-][A-Z]+)?$", RegexOptions.IgnoreCase);

Details

  • ^ - start of string
  • [a-zA-Z]+ - 1 or more ASCII letters
  • (?:[.-][a-zA-Z]+)? - one or zero repetitions of . or - followed with 1 or more ASCII letters
  • $ - end of string (or \z in case you do not want to allow a match before a trailing \n).

See the regex demo.



Related Topics



Leave a reply



Submit