Trying to check input against a regular expression.
The field should only allow alphanumeric characters, dashes and underscores and should NOT allow spaces.
However, the code below allows spaces.
What am I missing?
var regexp = /^[a-zA-Z0-9\-\_]$/; var check = "checkme"; if (check.search(regexp) == -1) { alert('invalid'); } else { alert('valid'); }
Is a dash an alphanumeric character? The login name must start with an alphabetic character and can contain only alphanumeric characters and the underscore ( _ ) and dash ( – ) characters. Full name can contain only letters, digits, and space, underscore ( _ ), dash ( – ), apostrophe ( ' ), and period ( . ) characters.
In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.
Just escape the dashes to prevent them from being interpreted (I don't think underscore needs escaping, but it can't hurt). You don't say which regex you are using.
Alphanumeric characters by definition only comprise the letters A to Z and the digits 0 to 9. Spaces and underscores are usually considered punctuation characters, so no, they shouldn't be allowed. If a field specifically says "alphanumeric characters, space and underscore", then they're included.
You can match alphanumeric with a \w, which is the same as [A-Za-z0-9_] in JavaScript (other languages can differ). That leaves - and spaces, which can be combined into a matching set such as [\w\- ]. However, you may want to consider using \s instead of just the space character ( \s also matches tabs, and other forms of whitespace)
Bonus Points: This regex does not match non-ASCII alphas. Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that. For languages/platforms that do support named character sets, you can use /^ [\p {Letter}\d\_\-\s]+$/
However, you may want to consider using \s instead of just the space character ( \s also matches tabs, and other forms of whitespace) (Note that the + indicates that there must be at least one character for it to match; use a * instead, if a zero-length string is also ok)
Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that. For languages/platforms that do support named character sets, you can use /^ [\p {Letter}\d\_\-\s]+$/ Show activity on this post. Show activity on this post.
However, the code below allows spaces.
No, it doesn't. However, it will only match on input with a length of 1. For inputs with a length greater than or equal to 1, you need a +
following the character class:
var regexp = /^[a-zA-Z0-9-_]+$/; var check = "checkme"; if (check.search(regexp) === -1) { alert('invalid'); } else { alert('valid'); }
Note that neither the -
(in this instance) nor the _
need escaping.
You shouldn't use String.match but RegExp.prototype.test (i.e. /abc/.test("abcd")
) instead of String.search() if you're only interested in a boolean value. You also need to repeat your character class as explained in the answer by Andy E:
var regexp = /^[a-zA-Z0-9-_]+$/;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With