1

I am using javascript regex to test a string. It should fail the text string but somehow its passing it. Any clue, what is wrong with this code?

<script>
var format = "^[a-zA-Z\.\-' ]*[a-zA-Z]+[a-zA-Z\.\-' ]*";
var testingValue = "FN306716";
var regex = new RegExp(format);
if (regex.test(testingValue) == false) { 
alert('validation failed');
}
else {
alert('validation passed');
}
</script> 
9
  • 2
    why do you expect it should fail? Commented Jun 28, 2013 at 7:52
  • what do you expect the regex to do ? Commented Jun 28, 2013 at 7:53
  • 2
    Your backslashes need to be escaped, otherwise your setup causes an error. This wouldn't happen if you were using a regex literal Commented Jun 28, 2013 at 7:55
  • 1
    @JamesAllardice but the range . to ' (.-') is not valid, which results in an error. Commented Jun 28, 2013 at 7:57
  • 1
    @JamesAllardice That's not my point, but still true. The - could be put at the end, but to me it's more readable if it's escaped (no matter where it is). As for the . - I can't see a reason why you'd need to escape it, but I think jslint complains if you don't (just saying) Commented Jun 28, 2013 at 7:58

2 Answers 2

1

Just guessing that you are missing $ at the end of your regex to test full string.

var regex = /^[a-zA-Z\.\-' ]*[a-zA-Z]+[a-zA-Z\.\-' ]$/;

With this regex, your input wouldn't pass, because it contains numbers.

EDIT: I have updated it to use regex literal as pointed out in comments.

Sign up to request clarification or add additional context in comments.

5 Comments

This is still not a valid RegExp, either write /^[a-zA-Z\.\-' ]*[a-zA-Z]+[a-zA-Z\.\-' ]$/ to get a RegExp directly or escape the \ because the string is another layer to the way to the RegExp otherwise you have a range .-' which is not valid.
Just a note: The new RegExp is not required there. The /something/i is a shortcut for new RegExp("something","i"). I don't want to say that writing it that way is wrong, just want to note that it is not required.
No this is not working... I want a regex that allow only following set of character in my string. [a-z] [A-Z] and following four special character [.]dot [']apostrophe [ ]space [-]hyphen. Regex should check that at least one character a-z or A-Z is entered otherwise it should fail.
I want a regex that allow only following set of character in my string. [a-z] [A-Z] and following four special character [.]dot [']apostrophe [ ]space [-]hyphen. Regex should check that at least one character a-z or A-Z is entered otherwise it should fail.
@MichalKlouda Your edit still didn't fix the regex. With a literal, you don't instantiate it with RegExp - it's by itself. When you use RegExp, you put the regex in a string, and you have to escape all backslashes
0

You're missing $ (end of input anchor):

Regex should be:

var format = "^[a-zA-Z.' -]*[a-zA-Z]+[a-zA-Z.' -]*$";

Also remember that - need not be escaped when used at fist and last position in character class and dot . also need not be escaped inside character class.

Live Demo: http://www.rubular.com/r/Q26cgQVJTm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.