views:

27

answers:

1

Hi,

I'm trying to set validation for an image alternate text, and here's what I think should be validated so far. It's a pretty simple RegEx, but I'm yet to start learning that topic..

  • Double quotes
  • < and > characters to prevent HTML input

Is there anything else you would add to this? Would text length ever be an issue?

I appreciate your help and if someone could provide this simple RegEx I'd be really grateful :)

+2  A: 

That sounds like a good place to start for me. Max size: pick something sane, unless you want it to be valid to post a dissertation as an alt text - though it is probably possible. As for the regex to validate it's okay:

/^[^"<>&\\]{0,XXX}$/

where XXX is the maximum size you want. Or get rid of the {0,XXX} altogether and replace it with * to mean "zero or more". Syntax depends on language, of course.

Also found this, looked interesting:

http://www.cs.tut.fi/~jkorpela/html/alt.html

Update:

Yeah, you two make a good point. As long as the quotes used around the alt-text aren't themselves single-quotes, then they should be fine.

And as per other answers below, possibly also & and . Though you may need to be careful with how many slashes, whether they are before things that matter. And also, whether   and such things are allowed in the text itself.

eruciform
Wow - that's a wicked article. I can't believe how much is being covered just on Alt text. Incredible. Back to my question though, I think it's safe to allow single quotes, otherwise -Hello I'm Marko- won't validate.. If I remember correctly, setting the length of the string is done via {1, 100} after the square brackets?
Marko
Not knowing the context, I wouldn't rule out `'` so easily, since people use it in contractions, and quite a few names contain it.
Larry Wang
Also, I'm using this inside of Umbraco, so I think the script should contain the "valid" characters as opposed to the invalid characters. Can this be rewritten in that way?
Marko
it's not easy to put that directly into the html in some way that i know of, unless you edit it at display-time with javascript. but you can put it in your validator and suit to taste as you go.
eruciform
are you trying to do the validation of length within the regex as well? if you don't need to, i'd recommend doing that with a simple string length check. ant with an elephant gun and all that... :-)
eruciform
Marko
updated regex :-)
eruciform
Larry Wang
@kaestur: good catch! i forgot the ^ inside the [] - updated!
eruciform
That's it! Cheers guys!
Marko