By ParoX


2010-09-13 00:04:25 8 Comments

$.validator.addMethod('AZ09_', function (value) { 
    return /^[a-zA-Z0-9.-_]+$/.test(value); 
}, 'Only letters, numbers, and _-. are allowed');

When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --

6 comments

@akhouri 2019-10-02 06:39:09

Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.

In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/

In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.

@Radu Simionescu 2015-10-13 07:15:34

A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression

@Máthé Endre-Botond 2010-09-13 00:23:22

The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.

  • In a server side string: \\-
  • On the client side: \-
  • In regex (covers): -

Or you can simply put at the and of the [] brackets.

@Topman 2020-06-04 01:22:13

I needed on the server side using C#. It worked. Thank you.

@Guffa 2010-09-13 00:06:51

Escaping the hyphen using \- is the correct way.

I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.

(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)

@ParoX 2010-09-13 00:18:31

Very nice. :D I prefer the readability though ( I guess \w would be easy for some experts though)

@Ross Presser 2015-06-08 17:04:21

Putting the hyphen last does NOT work with some Microsoft tools, like Microsoft SQL Server Management Studio. Escaping they hyphen does work.

@T.J. Crowder 2010-09-13 00:07:25

\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3

@Mark Byers 2010-09-13 00:07:17

Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:

/^[a-zA-Z0-9._-]+$/

@Ross Presser 2015-06-08 17:04:17

Putting the hyphen last does NOT work with some Microsoft tools, like Microsoft SQL Server Management Studio. Escaping they hyphen does work.

@Matthew 2016-07-07 14:54:20

@SabaAhang because if it's at the beginning or the end it can't be between two other characters to create a range of characters (e.g. [0-9])

@AbstractVoid 2017-01-23 13:11:13

@SabaAhang the following documentation page mentions that hyphen can be unescaped in the beginning or end of character brackets: regular-expressions.info/charclass.html

Related Questions

Sponsored Content

41 Answered Questions

[SOLVED] How to validate phone numbers using regex

76 Answered Questions

[SOLVED] Generate random string/characters in JavaScript

45 Answered Questions

[SOLVED] Detecting an "invalid date" Date instance in JavaScript

  • 2009-08-30 11:34:40
  • orip
  • 856958 View
  • 1540 Score
  • 45 Answer
  • Tags:   javascript date

61 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

53 Answered Questions

24 Answered Questions

[SOLVED] JavaScript chop/slice/trim off last character in string

  • 2009-06-04 20:31:53
  • Phill Pafford
  • 1756077 View
  • 2024 Score
  • 24 Answer
  • Tags:   javascript slice trim

17 Answered Questions

[SOLVED] Change the selected value of a drop-down list with jQuery

16 Answered Questions

[SOLVED] How does Access-Control-Allow-Origin header work?

25 Answered Questions

[SOLVED] Is Safari on iOS 6 caching $.ajax results?

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

  • 2014-05-09 16:16:57
  • Mathew MacLean
  • 243964 View
  • 2838 Score
  • 19 Answer
  • Tags:   javascript html css

Sponsored Content