tags:

views:

1324

answers:

4

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

+4  A: 
^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$
Jon Skeet
Damn you Jon, you beat me to it.
Unkwntech
This does alow a hypohen at the end.
wvanbergen
true,This does alow a hypohen at the end
GustlyWind
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
Undeleted as I don't believe "A-z" is the same as "A-Za-z"
Jon Skeet
@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
Jon,not working i guess can you check again..Thanks
GustlyWind
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
@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
@GustlyWind - have removed the backslash - should work now. If not, please give failing test.
Jon Skeet
Another note: our regexps require a string to be at least two characters long. This may or may not be desired behavior.
wvanbergen
(Also [\]^` are in A-z - look at the ASCII or Unicode tables.)
Jon Skeet
@wvanbergen: Yes, the length part hasn't been specified in the question.
Jon Skeet
@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
No it does it. It does not use ASCII tables.
wvanbergen
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
@it depends: No. Read my comment a few lines earlier. ;-) It just needs to be unambiguous.
Tomalak
Hey Its still not working for me..Help me out please
GustlyWind
The regex is formally correct as it stands now. Can you specify "not working"?
Tomalak
@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
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
use the complete value of the textbox: re.test(document.getElementById('input-id').value)
wvanbergen
@wvanbergen: So do you claim that regextester.com's JavaScript dialect implementation is broken? Is this (eek) browser-dependent?
Jon Skeet
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
Using the regex given above for 1-length it is not allowing hyphen.Can you check it please
GustlyWind
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
Why not use \d instead of [0-9]?
Keltia
@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
+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
Does the hyphen at the end of the middle bit need escaping? I wasn't sure.
Jon Skeet
A-z allows non-alpha characters (e.g. _)
Jon Skeet
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
Thanks for the hyphen bit - fixed.
Jon Skeet
+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
I really hope for GustlyWind's sake that he doesn't need to support single characters. That's becoming radio static :)
Jon Skeet
The latter variant is probably what he needs. A more readable form: ^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
wvanbergen
Typo: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
wvanbergen
@wvanbergen: Yours is good. You should put it in your own answer.
Tomalak
+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
Thanks But this is not allowing Hypen in javascript.Can you chek it please
GustlyWind
@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