Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphanumeric, dash and underscore but no spaces regular expression check JavaScript

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'); } 
like image 639
Tom Avatar asked May 04 '11 17:05

Tom


People also ask

Is dash part of alphanumeric?

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.

How do you escape a hyphen in regex?

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.

Do you need to escape underscore in regex?

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.

Is alphanumeric with space?

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.

How to match alphanumeric characters with spaces in JavaScript?

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)

Does this regex match non-ASCII Alphas?

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]+$/

How do I match the space character in a string?

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)

Is there a way to use named character sets in regex?

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.


2 Answers

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.

like image 69
Andy E Avatar answered Sep 21 '22 22:09

Andy E


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-_]+$/; 
like image 25
sapht Avatar answered Sep 21 '22 22:09

sapht