I had a requirement where in the text field the first character should be a alpha numeric and then i can allow a hyphen from thereafter in JavaScript.Also hyphen should not be allowed at the end
Damn you Jon, you beat me to it.
Unkwntech
2009-01-20 07:59:34
This does alow a hypohen at the end.
wvanbergen
2009-01-20 08:00:51
true,This does alow a hypohen at the end
GustlyWind
2009-01-20 08:01:17
Indeed - I didn't see that as part of the question to start with. Fixed, but I'll delete my answer as you got it right first :)
Jon Skeet
2009-01-20 08:01:46
Undeleted as I don't believe "A-z" is the same as "A-Za-z"
Jon Skeet
2009-01-20 08:05:42
@Jon: In a character class the hyphen is taken literally when you don't use it like a range delimiter. That means you put it as the first or last character, and all is well.
Tomalak
2009-01-20 08:07:28
Jon,not working i guess can you check again..Thanks
GustlyWind
2009-01-20 08:11:25
I have looked it up, and A-z is exactly the same as A-Za-z. So my answer is correct and more concise. However, as your regex is correct now as well, I have removed my downvote.
wvanbergen
2009-01-20 08:11:57
@wvanbergen I've just checked it on regextester.com, set to JavaScript, and ^[A-z]*$ matches "a_b" when ^[A-Za-z]*$ doesn't.
Jon Skeet
2009-01-20 08:13:56
@GustlyWind - have removed the backslash - should work now. If not, please give failing test.
Jon Skeet
2009-01-20 08:14:32
Another note: our regexps require a string to be at least two characters long. This may or may not be desired behavior.
wvanbergen
2009-01-20 08:15:45
(Also [\]^` are in A-z - look at the ASCII or Unicode tables.)
Jon Skeet
2009-01-20 08:16:09
@wvanbergen: Yes, the length part hasn't been specified in the question.
Jon Skeet
2009-01-20 08:16:41
@Jon: See the function I am usingfunction inputAlphaNumericHyphenOnly(e) { var key = keyFromEvent(e); var re = /^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$/; return re.test(String.fromCharCode(key))|| commonControlKey(key);}Its not allowing to press any key right now
GustlyWind
2009-01-20 08:16:49
No it does it. It does not use ASCII tables.
wvanbergen
2009-01-20 08:18:35
Shouldn't the hyphen be the first character in the class to indicate it's a hyphen'and not specifying a range i.e. [-a-zA-Z0-9]
it depends
2009-01-20 08:19:11
@it depends: No. Read my comment a few lines earlier. ;-) It just needs to be unambiguous.
Tomalak
2009-01-20 08:20:57
Hey Its still not working for me..Help me out please
GustlyWind
2009-01-20 08:23:27
The regex is formally correct as it stands now. Can you specify "not working"?
Tomalak
2009-01-20 08:25:42
@GustlyWind your code example shows that you are only testing the last character entered. You should test the whole string. The regexp shown here only returns true if a string has at least 2 characters.
wvanbergen
2009-01-20 08:26:08
Thanks WVanbergen, I am using onkeypress function that makes each char to verify.So Is there any modfication I need to do.Also the key is getting the Keycode of each key pressed.
GustlyWind
2009-01-20 08:29:03
use the complete value of the textbox: re.test(document.getElementById('input-id').value)
wvanbergen
2009-01-20 08:31:02
@wvanbergen: So do you claim that regextester.com's JavaScript dialect implementation is broken? Is this (eek) browser-dependent?
Jon Skeet
2009-01-20 08:41:54
Just tested with in Firefox on linux: var pattern=new RegExp("^[A-z]*$");document.write(pattern.test("a_c"));prints "true". With "^[A-Za-z]*$" it prints false.
Jon Skeet
2009-01-20 08:46:21
Using the regex given above for 1-length it is not allowing hyphen.Can you check it please
GustlyWind
2009-01-20 09:03:55
Whoops, my bad! I have checked it in Ruby and you're right. I have been using [A-z] for years... I guess it's time for me to do some debugging :-)
wvanbergen
2009-01-20 09:12:08
Why not use \d instead of [0-9]?
Keltia
2009-01-20 09:39:25
@Keltia: No particular reason, although I think 0-9 is slightly more readable (when in amongst other ranges) and you don't need to worry about whether or not to escape the backslash.
Jon Skeet
2009-01-20 09:54:51
+2
A:
If you do not want to match mutiple dashes after eachother:
^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
This will match: a
, a-a
, aaa-a
, aaa-a-aaa-a-aaa-a
, etc
But not: -
, -a
, a-
, a--a
, a-a-a-
, a-a--a
, etc.
wvanbergen
2009-01-20 08:00:14
Does the hyphen at the end of the middle bit need escaping? I wasn't sure.
Jon Skeet
2009-01-20 08:03:53
A hyphen does not need escaping as long as it is the last character in a character set. I am not sure about the underscore however.
wvanbergen
2009-01-20 08:08:25
+3
A:
Here is the POSIX + look-akead variant of doing it:
^[[:alnum:]](?:[[:alnum:]-](?!-$))*$
This also allows just one character as a match. It is not so readable, though. ;-)
Note that [[:alnum:]]
is a shorthand predefined character class equivalent to [a-zA-Z0-9]
, being more efficient, but otherwise interchangeable. Not every regex flavor knows these POSIX classes, use the traditional form if you like.
Here is one that does not allow multiple consecutive hyphens, and it is shorter:
^(?:[[:alnum:]]+(?:-(?!$))?)+$
and it's non-POSIX form:
^(?:[a-zA-Z0-9]+(?:-(?!$))?)+$
Tomalak
2009-01-20 08:16:12
I really hope for GustlyWind's sake that he doesn't need to support single characters. That's becoming radio static :)
Jon Skeet
2009-01-20 08:56:52
The latter variant is probably what he needs. A more readable form: ^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
wvanbergen
2009-01-20 09:14:30
+2
A:
I would propose:
^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$
This also matches strings of the length 1.
Gumbo
2009-01-20 09:14:46
Thanks But this is not allowing Hypen in javascript.Can you chek it please
GustlyWind
2009-01-20 09:22:29
@GustlyWind: Looks fine to me: var pattern=new RegExp("^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$");document.write(pattern.test("a-b-c"));
Jon Skeet
2009-01-20 09:58:03