2

I have a standard expression that is not working correctly. This expression is supposed to catch if a string has invalid characters anywhere in the string. It works perfect on RegExr.com but not in my tests.

The exp is: /[a-zA-Z0-9'.\-]/g

It is failing on : @#@# but passing with : aa@#@#

It should fail both times, what am I doing wrong?

Also, /^[a-zA-Z0-9'.\-]$/g matches nothing...

//All Boxs
        $('input[type="text"]').each(function () {

            var text = $(this).prop("value")
            var textTest = /[a-zA-Z0-9'.\-]/g.test(text)

            if (!textTest && text != "") {
                allFieldsValid = false
                $(this).css("background-color", "rgba(224, 0, 0, 0.29)")
                alert("Invalid characters found in " + text + " \n\n Valid characters are:\n A-Z a-z 0-9 ' . -")
            }
            else {
                $(this).css("background-color", "#FFFFFF")
                $(this).prop("value", text)
            }
        });

edit:added code

Josh G
  • 53
  • 7

3 Answers3

2

UPDATE AFTER QUESTION RE-TAGGING

You need to use

var textTest = /^[a-zA-Z0-9'.-]+$/.test(text)
                                  ^^

Note the absence of /g modifier and the + quantifier. There are known issues when you use /g global modifier within a regex used in RegExp#test() function.

You may shorten it a bit with the help of the /i case insensitive modifier:

var textTest = /^[A-Z0-9'.-]+$/i.test(text)

Also, as I mention below, you do not have to escape the - at the end of the character class [...], but it is advisable to keep escaped if the pattern will be modified later by less regex-savvy developers.

ORIGINAL C#-RELATED DETAILS

Ok, say, you are using Regex.IsMatch(str, @"[a-zA-Z0-9'.-]"). The Regex.IsMatch searches for partial matches inside a string. So, if the input string contains an ASCII letter, digit, ', . or -, this will pass. Thus, it is logical that aa@#@# passes this test, and @#@# does not.

If you use the second one as Regex.IsMatch(str, @"^[a-zA-Z0-9'.-]$"), only 1 character strings (with an optional newline at the end) would get matched as ^ matches at the start of the string, [a-zA-Z0-9'.-] matches 1 character from the specified ranges/sets, and $ matches the end of the string (or right before the final newline).

So, you need a quantifier (+ to match 1 or more, or * to match zero or more occurrences) and the anchors \A and \z:

Regex.IsMatch(str, @"\A[a-zA-Z0-9'.-]+\z")
                     ^^              ^^^

\A matches the start of string (always) and \z matches the very end of the string in .NET. The [a-zA-Z0-9'.-]+ will match 1+ characters that are either ASCII letters, digits, ', . or -.

Note that - at the end of the character class does not have to be escaped (but you may keep the \- if some other developers will have to modify the pattern later).

And please be careful where you test your regexps. Regexr only supports JavaScript regex syntax. To test .NET regexps, use RegexStorm.net or RegexHero.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0
/^[a-zA-Z0-9'.-]+$/g

In the second case your (/[a-zA-Z0-9'.-]/g) was working because it matched on the first letter, so to make it correct you need to match the whole string (use ^ and $) and also allow more letters by adding a + or * (if you allow empty string).

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
0

Try this regex it matches any char which isn't part of the allowed charset

/[^a-zA-Z0-9'.\-]+/g

Test

>>regex = /[^a-zA-Z0-9'.\-]+/g
/[^a-zA-Z0-9'.\-]+/g
>>regex.test( "@###dsfdfjsakldfj")
true
>>regex.test( "dsfdfjsakldfj")
false
ashishmohite
  • 1,120
  • 6
  • 14